简体   繁体   English

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

[英]how to use timer in PYQT5 for loop

this is the first writing for asking.. 这是第一本问书。

I would like to make a program for currency. 我想制作一个货币程序。
everything works well without looping. 一切正常,没有循环。 I used 'while' but it frozen the GUI. 我使用了“ while”,但它冻结了GUI。 so I searched for solution, and I found timer in pyqt5. 所以我寻找解决方案,然后在pyqt5中找到了计时器。 but I don't know how to use. 但我不知道该怎么用 If you can help me, it is really honor to me. 如果您能帮助我,对我来说真的很荣幸。 sorry for my bad English. 对不起,我的英语不好。

Here's the my full code 这是我的完整代码

    import sys
from PyQt5.QtWidgets import *
from bs4 import BeautifulSoup
import requests
from requests.compat import urljoin
import time
from PyQt5 import QtCore
global Currency

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setupUI()
        self.Searching_Currency()
    def setupUI(self):
        self.setGeometry(800, 200, 300, 100)
        self.label1 = QLabel("Currency: ")
        self.label2 = QLabel("Searching for")
        self.label3 = 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)


        layout = QGridLayout()

        layout.addWidget(self.label1, 0, 0)
        layout.addWidget(self.label3, 0, 1)
        layout.addWidget(self.pushButton1, 0, 2)

        layout.addWidget(self.label2, 1, 0)
        layout.addWidget(self.lineEdit2, 1, 1)
        layout.addWidget(self.pushButton2, 1, 2)
        layout.addWidget(self.pushButton3, 2, 0)
        layout.addWidget(self.pushButton4, 2, 1)
        self.setLayout(layout)

    def btn1_clicked(self):
        global Currency
        self.Searching_Currency()
        self.label3.setText(str(Currency))

    def btn2_clicked(self):
        global Currency
        textboxValue = float(self.lineEdit2.text())
        if textboxValue > Currency:
            QMessageBox.question(self, 'alert', "done. it is  " + str
            (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.timer.start(5000)
        QMessageBox.question(self, 'alert', "test message  " + str
            (textboxValue), QMessageBox.Ok, QMessageBox.Ok)      
         #trigger every minute    
        # while textboxValue < Currency:
        #         time.sleep(3)
        #         Currency=Currency-0.10
        #         break
        # QMessageBox.question(self, 'alert', "it is under the " + str(textboxValue), QMessageBox.Ok, QMessageBox.Ok)    


    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)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mywindow = MyWindow()
    mywindow.show()
    app.exec_()

1.this program check currency using Beautiful soup. 1.此程序使用美汤检查货币。

2.and compare with the value I put 2.并与我输入的值进行比较

3.and pop up the message, 3.然后弹出消息,

The QtCore.QTimer class provides repetitive and single-shot timers. QtCore.QTimer类提供重复和单次计时器。 You wrote everything practical. 您写的一切实用。 Add the called slot (showTime). 添加被调用的广告位(showTime)。

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

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

In your example you can use next: 在您的示例中,您可以使用next:

  1. Click "Search" 点击“搜索”
  2. Click "to get alert" 点击“获取警报”
  3. Look on the process for 1-2 minutes 查看过程1-2分钟

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_()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM