如何在PYQT5中使用定时器进行循环

时间:2018-03-23 05:58:42

标签: python user-interface pyqt5

这是第一次提问......

我想制作货币计划 一切顺利,没有循环。我使用'while'但它冻结了GUI。 所以我搜索了解决方案,我在pyqt5中找到了计时器。但我不知道如何使用。如果你能帮助我,我真的很荣幸。 抱歉我的英语不好。

这是我的完整代码

ionic cordova run ios -l -c -s

1.这个程序使用美丽的汤检查货币。

2.与我投入的值进行比较

3.弹出消息,

1 个答案:

答案 0 :(得分:0)

QtCore.QTimer类提供重复和单次定时器。 你写的一切都很实用。添加被调用的插槽(showTime)。

...
def setupUI(self):
    ....
    self.timer = QtCore.QTimer(self)
    self.timer.timeout.connect(self.showTime)
    self.timer.start(1000)
    ...

def showTime(self):
    # do something
    ....        

在您的示例中,您可以使用下一个:

  1. 点击"搜索"
  2. 点击"获取提示"
  3. 查看过程1-2分钟
  4. import sys
    import time
    
    from bs4 import BeautifulSoup
    import requests
    from requests.compat import urljoin
    
    from PyQt5.QtWidgets import *
    from PyQt5 import QtCore
    from PyQt5 import Qt                                            # +++
    
    global Currency
    
    class MyWindow(QWidget):
        def __init__(self):
            super().__init__()
    
            self.setupUI()
            self.Searching_Currency()
    
        def setupUI(self):
            self.setGeometry(800, 200, 350, 170)                    # -+
            self.label1 = QLabel("Currency: ")
            self.label2 = QLabel("Searching for")
            self.label3 = QLabel("")
            self.label4 = QLabel("")                                # +++
    
            self.lineEdit2 = QLineEdit()
            self.pushButton1= QPushButton("Search")
            self.pushButton2= QPushButton("to get alert")
            self.pushButton3= QPushButton("reset")
            self.pushButton4= QPushButton("quit")
            self.pushButton1.clicked.connect(self.btn1_clicked)
            self.pushButton2.clicked.connect(self.btn2_clicked)
            self.pushButton3.clicked.connect(self.btn3_clicked)
            self.pushButton4.clicked.connect(self.btn4_clicked)
    
            # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            self.lcd = Qt.QLCDNumber()                              # +++
            self.lcd.setDigitCount(7)
            self.lcdTime = Qt.QLCDNumber()  #self
            self.lcdTime.setSegmentStyle(Qt.QLCDNumber.Filled)   
            self.lcdTime.setDigitCount(8)  
            self.timer1 = Qt.QTimer(self)
            self.timer1.timeout.connect(self.showTime)
            self.timer1.start(1000)
    
    
            layout = QGridLayout()
            layout.addWidget(self.lcdTime, 0, 0, 1, 3)              # +++
            layout.addWidget(self.label1, 1, 0)
            layout.addWidget(self.label3, 1, 1)
            layout.addWidget(self.pushButton1, 1, 2)
    
            layout.addWidget(self.label2, 2, 0)
            layout.addWidget(self.lineEdit2, 2, 1)
            layout.addWidget(self.pushButton2, 2, 2)
            layout.addWidget(self.pushButton3, 3, 0)
            layout.addWidget(self.pushButton4, 3, 1)
            layout.addWidget(self.label4, 3, 2)                     # +++
            layout.addWidget(self.lcd, 4, 0, 1, 3)                  # +++
            self.setLayout(layout)
    
            self.timer = QtCore.QTimer()                            # +++
            self.timer.timeout.connect(self.show_textboxValue)      # +++
    
        def btn1_clicked(self):
            global Currency
            self.Searching_Currency()
            self.label3.setText(str(Currency))
    
        def btn2_clicked(self):
            global Currency
            try:                                                    # +++
                self.textboxValue = float(self.lineEdit2.text())
            except:
                self.textboxValue = Currency
                self.label4.setText(str(Currency))
    
            ##if self.textboxValue > Currency:
            ##    QMessageBox.question(self, 'alert', "done. it is  " + str
            ##    (self.textboxValue), QMessageBox.Ok, QMessageBox.Ok)    
            #   #### I don't know how to use timer.. to check every 1mins.   
            # self.timer = QtCore.QTimer()
            # self.timer.timeout.connect(self.update)
    
            self.num = 0                                            # +++  
            self.timer.start(1000)                                  # +++
    
            ##QMessageBox.question(self, 'alert', "test message  " + str
            ##    (self.textboxValue), QMessageBox.Ok, QMessageBox.Ok)      
             #trigger every minute    
            # while self.textboxValue < Currency:
            #         time.sleep(3)
            #         Currency=Currency-0.10
            #         break
            # QMessageBox.question(self, 'alert', "it is under the " + str(self.textboxValue), QMessageBox.Ok, QMessageBox.Ok)    
        def show_textboxValue(self):
            global Currency
            self.num +=  1
            self.lcd.display(str(self.num))
            if self.textboxValue < Currency:
                #Currency = round(Currency - 0.10, 4)
                QMessageBox.information(self, "QMessageBox.information",
                    "textboxValue`{}` < Currency`{}`".format(str(self.textboxValue), str(Currency)))
            elif self.textboxValue > Currency:
                self.label4.setText(str(Currency))
                QMessageBox.warning(self, "QMessageBox.warning!",
                    "textboxValue`{}` < Currency`{}`".format(str(self.textboxValue), str(Currency)))
            else:
                pass    
            self.textboxValue = Currency #float(self.label4.text())            
    
            if not self.num % 30:
                self.Searching_Currency()
    
    
        def btn3_clicked(self):
            self.label3.clear()
    
        def btn4_clicked(self):
            sys.exit(0)
    
        def Searching_Currency(self):
            base_url = "https://www.finanzen100.de/waehrungen/euro-britisches-pfund-eur-gbp-_H1677059499_11341217/?ac=1"
            header   = {'User-Agent': 'Mozilla/5.0'}
            r        = requests.get(base_url,headers=header).text
            soup     = BeautifulSoup(r,'html.parser')
    
            MyCurrency = soup.select(".quote__price__price")[0]
            MyCurrency = MyCurrency.get_text()
            MyCurrency = MyCurrency.replace(',','.')
            global Currency
            Currency = float(MyCurrency)
    
        def showTime(self):
            time = Qt.QTime.currentTime()
            text = time.toString("hh:mm:ss")           
            if ((time.second() % 2) == 0):
                text = text[0:2] + ' ' + text[3:5] + ' ' + text[6:]
            self.lcdTime.display(text)
    
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        mywindow = MyWindow()
        mywindow.show()
        app.exec_()