我的脚本有一个情况,希望你能帮助我找到根本原因。
在我下面的测试脚本中,我使用函数“root.after(1000,update_timeText)”在理论上每1000毫秒执行一次计数器“update_timeText”(每一秒)。
此功能还每秒更新一次显示1,2,3,4 ......的GUI。第一次执行完美工作,每秒执行一次“update_timeText”,每秒只执行一次GUI正确更新1,2,3,4,.....
但是,如果我停止计时并重新启动计数器,GUI每秒更新一次,但每秒显示1,3,5,7 ..... “有趣”的情况是,在现实中,循环运行了两次。如果我打印变量 timer [0] ,则此变量显示1,2,3,4 ....但由于函数“root.after(1000,update_timeText)”每隔一秒更新一次GUI,显示1,3,5,7。
现在,如果我再次停止时钟并重新启动,现在GUI将显示1,4,7,11,14 ....但实际中变量 timer [0] 计算1,2,3,4,5 ...但真的很快。
我的代码如下;希望有人可以帮助我解决这个问题
感谢您的光临。
def update_timeText():
global temp
global degree_sign
global temp_b
global temp_c
if (state):
global timer
timer [0] +=1
if (timer[0] >= 60): ## timer [0] Seconds
timer[1] += 1
timer[0] = 0
if (timer[1] >= 60): ## timer [1] Minutes
timer[2] += 1
timer[1] = 0
if (timer[2] == 24): ## timer [2] Hours
timer[2] = 0
timeString = pattern.format(timer[2], timer[1], timer[0])
timeText.configure(text=timeString)
root.after(1000, update_timeText)
def update_temp():
global timer
global loga
global temp
global degree_sign
global temp_b
global temp_c
global temp_alter
if (timer[0] == 1):
temp = a.read_holding_registers(17415, 1)
temp_alter = a.read_holding_registers(1031, 1)
temp_a = str(temp)
temp_b = temp_a.replace("[", "").replace("]", "")
temp_c = temp_b[:2]+'.'+temp_b[2:]
if (temp_alter >= [40]):
degree_sign= u'\N{DEGREE SIGN}'
Label(text=(str(temp_c)) + degree_sign + "C",fg = "red", font=("Arial", 12)).place(x=210,y=443)
if (temp_alter <= [39]):
degree_sign= u'\N{DEGREE SIGN}'
Label(text=(str(temp_c)) + degree_sign + "C",fg = "green", font=("Arial", 12)).place(x=210,y=443)
if (timer[0] == 10 or timer[0] == 20 or timer[0] == 30 or timer[0] == 40 or timer[0] == 50 or timer[0] == 60):
loga.write("Time Stamp : Hours " +(str(timer[2]))+" ; Minutes "+(str(timer[1]))+" ; Seconds "+(str(timer[0]))+"\n")
loga.write("Temp Stamp : "+(temp_c) + degree_sign + "C" "\n")
if (timer[1] == 1):
end_time1 = datetime.now()
endd1 =(end_time1.strftime("%A %d %b %y, %H:%M"))
def pause():
global state
state = False
global timer
timer = [0, 0, 0, 0]
pause()
process = "PASSED"
Label(text="PASSED ", fg = "green", font=("Arial", 12)).place(x=210,y=543)
loga.write("==============================================\n")
loga.write("My Company. \n")
loga.write("Assy PN #: 00000-00 Rev. A \n")
loga.write("Serial #: N/A \n")
loga.write("Start Date: "+str(startt)+" \n")
loga.write("End Date : "+str(endd1)+" \n")
loga.write("Station : TEST \n")
loga.write("Status : "+str(process)+" \n")
loga.write("==============================================\n")
loga.close() ##to close logfile
write = a.write_single_register(8245, 3)
time.sleep(0.050)
write = a.write_single_register(7668, 3)
time.sleep(0.050)
connect_btna.config(state = NORMAL)
start_btna.config(state = DISABLED)
abort_btna.config(state = DISABLED)
root.after(1000, update_temp)
def start():
global state
state = True
##Starting Set Profile
write = a.write_single_register(8245, 1)
##Lock Door Enabled
Label(text = "DOOR LOCKED... ",fg = "green", font=("Arial", 12)).place(x=210,y=343)
start()
if status == "":
messagebox.showinfo('Process App 1.00', "An Error has occurred, Please, restart the app. Click OK to continue")
write = a.write_single_register(8245, 3)
time.sleep(0.050)
write = a.write_single_register(7668, 3)
time.sleep(0.050)
loga.write("An Error has accurred.....\n")
write = a.close()
sys.exit()
root.destroy()
quit()
if status == "Fail":
messagebox.showinfo('Process App 0.00', "An Error has occurred, Please, restart the app. Click OK to continue")
root.destroy()
quit()
def pause():
global state
state = False
def reset():
global timer
timer = [0, 0, 0]
timeText.configure(text='00:00:00')
def exist():
root.destroy()
timer = [0, 0, 0]
pattern = '{0:02d}'+" h "+' {1:02d}'+" m "+' {2:02d}'+" s "
timeText = tk.Label(text="00:00:00", font=("Tahoma", 12))
timeText.pack()
timeText.place_configure(x=210,y=393)
update_temp()
update_timeText()
root.mainloop()
答案 0 :(得分:0)
在我的剧本分析之后,我发现属于没有正确嵌套的属性函数,这就是我所拥有的
timeString = pattern.format(timer[2], timer[1], timer[0])
timeText.configure(text=timeString)
root.after(1000, update_timeText)
在我正确嵌套 root.after(1000,update_timeText)后,我的测试脚本正在运行并作为冠军运行。
timeString = pattern.format(timer[2], timer[1], timer[0])
timeText.configure(text=timeString)
root.after(1000, update_timeText)
感谢所有人, F Casian