我的代码几乎就是我想要的代码。我希望每分钟刷新一次这个脚本,并且还有一个手动刷新的按钮。按钮已经存在,但似乎没有按预期工作。我希望它重绘或刷新小部件中的信息。
按钮代码:
bottom2 = QtGui.QPushButton("Refresh")
#bottom2.setFrameShape(QtGui.QFrame.StyledPanel)
#bottom2.setWordWrap(True)
bottom2.clicked.connect(InfoCenter)
计时器代码:
timer = QtCore.QTimer()
QtCore.QTimer.connect(timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("func()"))
timer.start(6000)
完整代码:
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt, QTimer
from PyQt4.QtGui import QWidget, QApplication, QSplitter, QLabel, QVBoxLayout, QColor, QPixmap, QIcon
import Adafruit_DHT
import urllib2
from wunderground import high_0, low_0, conditions_0
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 InfoCenter(QtGui.QWidget):
def __init__(self):
super(InfoCenter, self).__init__()
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
self.outsidetemp = QLabel("Todays High:{}\nTodays Low:{}\nConditions:{}".format(high_0,low_0,conditions_0),self)
self.outsidetemp.setFrameShape(QtGui.QFrame.StyledPanel)
self.insidetemp = QLabel('Temperature:{:0.1f}F\nHumidity:{:0.1f}%'.format(temperature,humidity),self )
self.insidetemp.setFrameShape(QtGui.QFrame.StyledPanel)
self.mlbnews = QLabel("Redsox News \nTitle: %s\nSummary: %s " % (title.text, desc.text), self)
self.mlbnews.setFrameShape(QtGui.QFrame.StyledPanel)
self.mlbnews.setWordWrap(True)
self.button = QtGui.QPushButton("Refresh")
self.button.clicked.connect(InfoCenter)
splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
splitter1.addWidget(outsidetemp)
splitter1.addWidget(insidetemp)
splitter2 = QtGui.QSplitter(QtCore.Qt.Vertical)
splitter2.addWidget(splitter1)
splitter2.addWidget(mlbnews)
splitter2.addWidget(button)
hbox.addWidget(splitter2)
self.setLayout(hbox)
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QtGui.QSplitter')
self.show()
#color widget code
palette = self.palette()
role = self.backgroundRole()
palette.setColor(role, QColor('black'))
self.setPalette(palette)
outsidetemp.setStyleSheet("QLabel {color:yellow}")
insidetemp.setStyleSheet("QLabel {color:yellow}")
mlbnews.setStyleSheet("QLabel {background-color: black; color:white}")
@QtCore.pyqtSlot()
def refreshLabels(self):
self.outsidetemp.setText('Temperature:{:0.1f}F\nHumidity:{:0.1f}%'.format(temperature,humidity),self )
timer = QtCore.QTimer()
self.timer.timeout.connect(self.refreshLabels)
timer.start(5000)
print 'done'
def main():
app = QtGui.QApplication(sys.argv)
ex = InfoCenter()
app.exec_()
if __name__ == '__main__':
main()
更新代码
答案 0 :(得分:1)
首先,您需要存储对小部件的引用(注意self
)
self.outsidetemp = QLabel(...)
您应该对所有小部件执行此操作,具体取决于您正在运行的对象和环境,不存储对小部件的python引用可能导致它被垃圾回收,并且您可能会收到错误。
您还可以使用更清晰的新式信号语法
self.timer.timeout.connect(self.refreshLabels)
然后创建一个更新标签的函数,将timeout
信号连接到该标签。
@QtCore.pyqtSlot()
def refreshLabels(self):
self.outsidetemp.setText("Todays High:{}\nTodays Low:{}\nConditions:{}".format(high_0,low_0,conditions_0))
我还要添加强制性警告,在这里使用全局变量可能不是最好的方法。你是如何更新全局变量的?而不是使用QTimer,为什么不只是让更新全局变量的过程更新小部件?