[英]how to use timer in PYQT5 for loop
這是第一本問書。
我想制作一個貨幣程序。
一切正常,沒有循環。 我使用了“ while”,但它凍結了GUI。 所以我尋找解決方案,然后在pyqt5中找到了計時器。 但我不知道該怎么用 如果您能幫助我,對我來說真的很榮幸。 對不起,我的英語不好。
這是我的完整代碼
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.此程序使用美湯檢查貨幣。
2.並與我輸入的值進行比較
3.然后彈出消息,
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
....
在您的示例中,您可以使用next:
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.