我需要建议。这是我的程序,我们在设置的时间内从perforce获取最新文件。但是我发现,我们只能启动一次线程,因此,这只能运行一次。我知道,我可以在一个def中编写所有内容并且它会没问题,但是也许有人知道简单的方法使它与线程一起工作吗?
这是代码:
import P4
import time
from datetime import datetime,date
import traceback
import os
import sys
import threading
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QCoreApplication, QTimer
p4 = P4.P4()
seconds = 0
p4.port="p4-1:1666"
p4.user=""
zero_time=QtCore.QTime(0,00,00)
cur_time=QtCore.QTime.currentTime()
now = QtCore.QTime.currentTime()
sync_time=QtCore.QTime(3,00,00)
syncTime=zero_time.addSecs(now.secsTo(sync_time))
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.")
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)
def center(widget):
qr = widget.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
widget.move(qr.topLeft())
def user_name():
def set_sync_time():
sync_time.setHMS(userTime.time().hour(),userTime.time().minute(),userTime.time().second())
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("Time until sync "+syncTime.toString(),user_name_wdgt)
lblName.move(90,10)
userTime =QTimeEdit(user_name_wdgt)
userTime.move(120,40)
qbtn = QPushButton('Set sync time',user_name_wdgt)
qbtn.clicked.connect(set_sync_time)
qbtn.resize(qbtn.sizeHint())
qbtn.move(110, 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 = QtCore.QTime.currentTime()
syncTime=zero_time.addSecs(cur_time.secsTo(sync_time))
lblName.setText("Time until sync "+syncTime.toString())
if syncTime==zero_time:
t2.start()
t1.start()
timer = QTimer()
timer.timeout.connect(update_label)
timer.start(1000)
user_name_wdgt.show()
sys.exit(app.exec_())
t1 = threading.Thread(target=clock, args=(0,))
t2 = threading.Thread(target=just)
t3 = threading.Thread(target=user_name)
t3.start()
这是工作部分,希望它能帮到某人:
def update_label():
cur_time = QtCore.QTime.currentTime()
syncTime=zero_time.addSecs(cur_time.secsTo(sync_time))
lblName.setText("Time until sync "+syncTime.toString())
if syncTime==zero_time:
global t2,t1
t2 = threading.Thread(target=just)
t1 = threading.Thread(target=clock, args=(0,))
t2.start()
t1.start()
timer = QTimer()
timer.timeout.connect(update_label)
timer.start(1000)
user_name_wdgt.show()
sys.exit(app.exec_())
t2 = threading.Thread(target=just)
t3 = threading.Thread(target=user_name)
t3.start()