PyQt4不断更新文本

时间:2014-02-11 02:44:44

标签: python pyqt

我制作了一个获得奥运会得分和显示加拿大排名和奖牌数量的计划。我如何在PyQt4的GUI窗口中显示这个并让它不断更新?

以下是代码:

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        self.text = '?????'

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Draw text')
        self.setWindowIcon(QtGui.QIcon('web.png'))
        self.show()

    def medal_update(self):
        html = urllib.request.urlopen("http://olympics.cbc.ca/medals/index.html")
        soup = BeautifulSoup(html)
        Rank = soup.find(cns='Canada').find_previous('td').find_previous('td').text
        Rank = int(Rank)
        Rankend='st'
        if Rank==1:
            Rankend='st'
        elif Rank==2:
            Rankend='nd'
        elif Rank==3:
            Rankend='rd'
        elif Rank > 3:
            Rankend='th'
        else:
            Rankend='dey more dan 10th'
        self.Gold = soup.find(text='CAN').find_next('td').text
        Silver = soup.find(text='CAN').find_next('td').find_next('td').text
        Bronze = soup.find(text='CAN').find_next('td').find_next('td').find_next('td').text
        print ("Canada is in " + str(Rank) + str(Rankend) + ' Place')
        print ('They Have ' + Gold + ' Gold')
        print('They Have ' + Silver + ' Silver')
        print('They Have ' + Bronze + ' Bronze')


    def paintEvent(self, event):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawText(event, qp)
        qp.end()

    def drawText(self, event, qp):

        qp.setPen(QtGui.QColor(200, 100, 3))
        qp.setFont(QtGui.QFont('Decorative', 20))
        qp.drawText(event.rect(), QtCore.Qt.AlignCenter, self.text)        


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:1)

像这样编辑medal_update的结尾:

    self.text = ''
    self.text += ("Canada is in " + str(Rank) + str(Rankend) + ' Place' + '\n')
    self.text += ('They Have ' + Gold + ' Gold' + '\n')
    self.text +=('They Have ' + Silver + ' Silver' + '\n')
    self.text +=('They Have ' + Bronze + ' Bronze' + '\n')
    self.update()

然后将medal_update置于某种无限循环中。可能有某种延迟\睡眠时间。

就像这样:

    self.my_timer = QtCore.QTimer()
    self.my_timer.timeout.connect(self.medal_update)
    self.my_timer.start(60000) #1 min intervall

完整代码:

import sys
from PyQt4 import QtGui, QtCore
# from PySide import QtGui, QtCore
import urllib
from bs4 import BeautifulSoup

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        self.text = '?????'
        self.medal_update()
        self.my_timer = QtCore.QTimer()
        self.my_timer.timeout.connect(self.medal_update)
        self.my_timer.start(60000) #1 min intervall

        self.setGeometry(300, 300, 400, 200)
        self.setWindowTitle('Draw text')
        self.setWindowIcon(QtGui.QIcon('web.png'))
        self.show()

    def medal_update(self):
        html = urllib.urlopen("http://olympics.cbc.ca/medals/index.html")
        soup = BeautifulSoup(html)
        Rank = soup.find(cns='Canada').find_previous('td').find_previous('td').text
        Rank = int(Rank)
        Rankend='st'
        if Rank==1:
            Rankend='st'
        elif Rank==2:
            Rankend='nd'
        elif Rank==3:
            Rankend='rd'
        elif Rank > 3:
            Rankend='th'
        else:
            Rankend='dey more dan 10th'
        Gold = soup.find(text='CAN').find_next('td').text
        Silver = soup.find(text='CAN').find_next('td').find_next('td').text
        Bronze = soup.find(text='CAN').find_next('td').find_next('td').find_next('td').text
        self.text = ''
        self.text += ("Canada is in " + str(Rank) + str(Rankend) + ' Place' + '\n')
        self.text += ('They Have ' + Gold + ' Gold' + '\n')
        self.text +=('They Have ' + Silver + ' Silver' + '\n')
        self.text +=('They Have ' + Bronze + ' Bronze' + '\n')
        self.update()


    def paintEvent(self, event):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawText(event, qp)
        qp.end()

    def drawText(self, event, qp):

        qp.setPen(QtGui.QColor(200, 100, 3))
        qp.setFont(QtGui.QFont('Decorative', 20))
        qp.drawText(event.rect(), QtCore.Qt.AlignCenter, self.text)        


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

答案 1 :(得分:0)

建议使用QTimer进行间隔更新。参考:http://pyqt.sourceforge.net/Docs/PyQt4/qtimer.html