Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pyqt中按时间更改标签_Python_Multithreading_Pyqt - Fatal编程技术网

Python 在pyqt中按时间更改标签

Python 在pyqt中按时间更改标签,python,multithreading,pyqt,Python,Multithreading,Pyqt,我正在用UI编写简单的程序,我们只有一个窗口,有几个按钮和标签,每秒钟都会改变它的值。我不知道怎么做。我可以在一个线程中更改值,但窗口不会更新它,因为它只初始化一次标签值 import P4 import time from datetime import datetime import traceback import os import sys import threading from PyQt5 import QtGui from PyQt5.QtWidgets import * fro

我正在用UI编写简单的程序,我们只有一个窗口,有几个按钮和标签,每秒钟都会改变它的值。我不知道怎么做。我可以在一个线程中更改值,但窗口不会更新它,因为它只初始化一次标签值

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()
对于所有感兴趣的人。以下是工作代码部分:

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_())

使用
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毫秒发出一个信号。只需将该信号连接到您自己的插槽/可调用插槽,它将在每次发出信号时运行。

非常感谢!它起到了帮助作用)