我正在做非常基本的PyQt所以请原谅可怜的优化和缺乏课程导向。
我试图在PyQt中制作一个计时器。我在UI上有3个LCD,一小时LCD(hourLcd),一分钟LCD(minuteLcd)和第二个LCD(secondLcd)
import sys
import time
from PyQt4 import QtGui
from PyQt4 import uic
app = QtGui.QApplication(sys.argv)
#window = QtGui.QMainWindow()
ui = uic.loadUi("clock.ui")
#ui.setupUi(Window)
ui.show()
这设置了PyQt4
def hourUp():
count = ui.hourLcd.value()
if count < 24:
count = count + 1
ui.hourLcd.display(count)
def hourDown():
count = ui.hourLcd.value()
if count > 0:
count = count - 1
ui.hourLcd.display(count)
def minuteUp():
count = ui.minuteLcd.value()
if count < 59:
count = count + 1
ui.minuteLcd.display(count)
def minuteDown():
count = ui.minuteLcd.value()
if count > 0:
count = count - 1
ui.minuteLcd.display(count)
def secondUp():
count = ui.secondLcd.value()
if count < 59:
count = count + 1
ui.secondLcd.display(count)
def secondDown():
count = ui.secondLcd.value()
if count > 0:
count = count - 1
ui.secondLcd.display(count)
这些是将UI按钮连接到动作的功能。例如如果您按下UI上的按钮,它会将一个添加到计数器
def start(stop):
loop = False
secondcounter = ui.secondLcd.value()
minutecounter = ui.minuteLcd.value()
hourcounter = ui.hourLcd.value()
while loop == False:
time.sleep(1)
secondcounter = secondcounter - 1
print(secondcounter)
ui.secondLcd.display(secondcounter) ****
if secondcounter == 0:
if minutecounter != 0:
minutecounter = minutecounter - 1
print(minutecounter)
ui.minuteLcd.display(minutecounter) ****
secondcounter = secondcounter + 59
ui.secondLcd.display(secondcounter) ****
if secondcounter == 0:
if minutecounter == 0:
if hourcounter == 0:
loop = True
if minutecounter == 0:
if secondcounter == 0:
if hourcounter != 0:
hourcounter = hourcounter - 1
print(hourcounter)
ui.hourLcd.display(hourcounter) ****
minutecounter = minutecounter + 59
print(minutecounter)
ui.minuteLcd.display(minutecounter) ****
secondcounter = secondcounter + 59
print(secondcounter)
ui.secondLcd.display(secondcounter) ****
else:
continue
使计时器工作的算法。
基本上,代码都完美无缺。它从给定的任何时间开始倒数到0,但是我无法将代码的打印输出链接到UI,特别是与它们旁边的星号相关的行。 我不明白为什么代码在计数器改变时拒绝更新LCD数字(例如counter = counter-1,因为它在其他功能中起作用)。
谢谢!
答案 0 :(得分:0)
找到解决方案。
while loop == False:
app.processEvents() #This was the line of code that I needed
time.sleep(1)
secondcounter = secondcounter - 1
app.processEvents() #and this
print(secondcounter)
ui.secondLcd.display(secondcounter)
基本上,在调用你的ui
之前添加app.processEvents()