Chris Cunningham
Chris Cunningham

Reputation: 19

Update window every 5 minutes in Qt

Im looking to update the temp and humidity in this script.

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QWidget, QApplication, QSplitter, QLabel, QVBoxLayout, QColor
import Adafruit_DHT
import urllib2
from BeautifulSoup import BeautifulSoup



sensor_args = { '11': Adafruit_DHT.DHT11,
                '22': Adafruit_DHT.DHT22,
                '2302': Adafruit_DHT.AM2302 }

humidity, temperature = Adafruit_DHT.read_retry(11, 4)

temp = 'Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity)

soup = BeautifulSoup(urllib2.urlopen('http://partner.mlb.com/partnerxml/gen/news/rss/bos.xml').read())

title = soup.find('item').title
desc = soup.find('item').description
url = soup.find('item').guid


temperature = temperature * 9/5.0 + 32
class MyWidget(QWidget):

    def __init__( self, parent = None ):
        super(MyWidget, self).__init__(parent)

        # create widgets
        a = QLabel('Humidity:{:0.1f}%'.format(humidity),self )
        a.setMinimumSize(100, 100)
        b = QLabel('Temperature:{:0.1f}F'.format(temperature),self )
        b.setMinimumSize(100, 100)
        c = QLabel("Redsox News \nTitle: %s\nSummary: %s " % (title.text, desc.text), self)    
        c.setWordWrap(True)
        c.setMinimumSize(280, 200)



        d = QLabel("This is some bullshit wordwrap and i cant get it tow work", self)
        d.setWordWrap(True)
        d.setMinimumSize(180, 300)
        for lbl in (a, b, c, d):
            lbl.setAlignment(Qt.AlignLeft)

        # create 2 horizontal splitters
        h_splitter1 = QSplitter(Qt.Horizontal, self)
        h_splitter1.addWidget(a)
        h_splitter1.addWidget(b)


        h_splitter2 = QSplitter(Qt.Horizontal, self)
        h_splitter2.addWidget(c)
        h_splitter2.addWidget(d)

        h_splitter1.splitterMoved.connect(self.moveSplitter)
        h_splitter2.splitterMoved.connect(self.moveSplitter)

        self._spltA = h_splitter1
        self._spltB = h_splitter2

        # create a vertical splitter
        v_splitter = QSplitter(Qt.Vertical, self)
        v_splitter.addWidget(h_splitter1)
        v_splitter.addWidget(h_splitter2)

        layout = QVBoxLayout()
        layout.addWidget(v_splitter)
        self.setLayout(layout)

        #color widget code
        palette = self.palette()
        role = self.backgroundRole()
        palette.setColor(role, QColor('black'))
        self.setPalette(palette)
        a.setStyleSheet("QLabel {color:yellow}")
        b.setStyleSheet("QLabel {color:yellow}")
        c.setStyleSheet("QLabel {background-color: black; color:white}")
        d.setStyleSheet("QLabel {background-color: black; color:white}")
        #self.setWindowFlags(Qt.CustomizeWindowHint)

        timer=self.QTimer()
        timer.start(5000)
        timer.timeout.connect(self.temp.update)

    def moveSplitter( self, index, pos ):
        splt = self._spltA if self.sender() == self._spltB else self._spltB
        splt.blockSignals(True)
        #splt.moveSplitter(index, pos)
        splt.blockSignals(False)

if ( __name__ == '__main__' ):
    app = QApplication([])
    widget = MyWidget()
    widget.show()
    app.exec_()

Ive been learning a lot about pyQt and all the ins and outs of it. Slow going i might add as i am very new to python.

What I would like is to have it so that this updates the temp and humidity every 5 minutes. I have tried this..

    timer=self.QTimer()
    timer.start(300)
    timer.timeout.connect(self.temp.update)

This does not seem to work for me. I get the error no attribute QTimer.

Upvotes: 0

Views: 443

Answers (1)

hyde
hyde

Reputation: 62777

(Note, I'm not really familiar with pyqt, so if this is wrong, please let me know and I'll delete the answer...)

The line

timer=self.QTimer()

is wrong. this is a QWidget subclass, which does not have QTimer attribute. In fact, QTimer is a regular Qt class, so that line should simply be:

timer = QTimer()

You also need the right import, of course, which I think is:

from PyQt4.QtCore import QTimer

Upvotes: 1

Related Questions