grenfunday grenfunday - 1 month ago 13
Python Question

Change of label by the time in pyqt

I'm writing easy programm with UI, where we have just one window with several buttons and label, which change it's value every second. I don't get how to make it. I can make change of value in one thread, but the window don't update it because it initialize label value just one time.

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


For everyone, who is interested. Here is the working 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_())

Answer

Use a 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)

The QTimer emits a signal every 1000 milliseconds. Just connect that signal to your own slot/callable, which will get run every time the signal is emitted.