0
我正在用UI編寫簡單的程序,我們只有一個帶有幾個按鈕和標籤的窗口,它每秒都會更改它的值。我不知道該怎麼做。我可以在一個線程中更改值,但窗口不更新它,因爲它只是初始化標籤值一次。在pyqt中更改標籤的時間
import P4
import time
from datetime import datetime
import traceback
import os
import sys
import threading
from PyQt5 import QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QCoreApplication
p4 = P4.P4()
seconds = 0
p4.port="p4-1:1666"
p4.user=""
cur_time=datetime.strftime(datetime.now(), "%d.%m %H:%M:%S")
def time_change():
while True:
cur_time=datetime.strftime(datetime.now(), "%d.%m %H:%M:%S")
return cur_time
def center(widget):
qr = widget.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
widget.move(qr.topLeft())
def user_name():
app = QApplication(sys.argv)
user_name_wdgt=QWidget()
user_name_wdgt.resize(280,150)
user_name_wdgt.move(300,300)
user_name_wdgt.setWindowTitle('User name')
lblName = QLabel(cur_time,user_name_wdgt)
lblName.move(100,10)
enterName=QLineEdit(user_name_wdgt)
enterName.move(75,40)
qbtn = QPushButton('Continue',user_name_wdgt)
qbtn.clicked.connect(QCoreApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(100, 75)
center(user_name_wdgt)
qe = QEvent()
text, ok = QInputDialog.getText(user_name_wdgt, 'User name window', 'Enter your name(same as workspace):')
if ok:
p4.user=str(text)
user_name_wdgt.show()
sys.exit(app.exec_())
#p4.connect()
#p4.run("sync")
def clock(seconds):
minutes = 0
while True:
seconds=seconds+1
time.sleep(1)
if t2.isAlive()==False:
print("Completed!! Connection ended at",datetime.strftime(datetime.now(), "%d.%m %H:%M:%S"))
print("It took ",minutes," minutes and",seconds," seconds.")
stop_program = input("Press any key")
sys.exit()
break
if seconds==60:
seconds=0
minutes=minutes+1
def just():
print("Connection and sync started. Wait")
print("Connection started at ",datetime.strftime(datetime.now(), "%d.%m %H:%M:%S"))
try:
#p4.connect()
p4.run("sync")
except:
print("Some mistakes occured:")
Type, Value, Trace = sys.exc_info()
print(Value)
t1 = threading.Thread(target=clock, args=(0,))
t2 = threading.Thread(target=just)
t3 = threading.Thread(target=user_name)
t4 = threading.Thread(target=time_change)
t1.start()
t2.start()
t3.start()
t4.start()
對於每個人,誰有興趣。這裏是工作codepart:
def user_name():
app = QApplication(sys.argv)
user_name_wdgt=QWidget()
user_name_wdgt.resize(280,150)
user_name_wdgt.move(300,300)
user_name_wdgt.setWindowTitle('User name')
lblName = QLabel(cur_time,user_name_wdgt)
lblName.move(100,10)
enterName=QLineEdit(user_name_wdgt)
enterName.move(75,40)
qbtn = QPushButton('Continue',user_name_wdgt)
qbtn.clicked.connect(QCoreApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(100, 75)
center(user_name_wdgt)
text, ok = QInputDialog.getText(user_name_wdgt, 'User name window', 'Enter your name(same as workspace):')
if ok:
p4.user=str(text)
def update_label():
cur_time = datetime.strftime(datetime.now(), "%d.%m %H:%M:%S")
lblName.setText(cur_time)
timer = QTimer()
timer.timeout.connect(update_label)
timer.start(1000)
user_name_wdgt.show()
sys.exit(app.exec_())
非常感謝!它有幫助) – grenfunday