2016-10-19 38 views
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_()) 

回答

0

使用QTimer

lblName = QLabel(cur_time,user_name_wdgt) 

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) 

QTimer发出的信号每1000毫秒。只需将该信号连接到您自己的插槽/可调用信号,每当信号发出时它就会运行。

+0

非常感谢!它有帮助) – grenfunday