2016-10-19 2 views
0

나는 여러 개의 단추와 레이블이있는 창이 하나 뿐인 UI를 통해 쉬운 프로그램을 작성하고 있는데 매초마다 값이 바뀝니다. 나는 그것을 만드는 법을 모르겠다. 한 스레드에서 값을 변경할 수는 있지만 레이블 값을 한 번만 초기화하므로 창이 업데이트되지 않습니다.시간의 레이블 변경 peqt

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