现在,我正忙着为我的项目创建一个GUI。它还需要时间驱动的测试。然而,当我按下开始按钮,计数器开始计数,但Gui冻结,所以你不能按停止按钮。最终节目停顿下来并关闭了他自己。
看看我的代码:
###import libaries ###
from guizero import *
import tkinter as tk
import time
timeLoop = False
###Variabelen###
Sec = 0
Min = 0
Hour = 0
test_stat = 0
###Define Loops###
def verlaat_settings():
window2.hide()
window3.hide()
window4.hide()
window.show(wait=True)
def stop_test():
info("test_result","Test stopped at" + str(Hour) + " Hour " + str(Min) + " Mins " + str(Sec) + " Sec ")
text_test.value = "Test Stopped..."
timeLoop: False
def test_loopt():
global Sec
global Min
text_test.value = "Test is running....."
timeLoop = True
while timeLoop:
Sec +=1
print(str(Min) + " Mins " + str(Sec) + " Sec ")
time.sleep(1)
if Sec == 60:
Sec = 0
Min += 1
app= App(title="Profiberry",layout="",width=480, height=272)
window3 = Window(app,title="Profiberry-Tester", layout="grid",width=480, height=272)
window3.show
###Window3###
welkom_tester= Text(window3, text="Profibus Tester",grid=[2,0,1,1])
Plaatje_profi= Picture(window3,image="logoprofi.gif",grid=[2,1,1,1])
lege_ruimte1 = Text(window3, text="", grid=[2,2,1,1])
text_test= Text(window3,text=" Waiting for input..... ",grid=[2,3,1,1])
timer_test= Text(window3,text=(""),grid=[2,4,1,1] )
lege_ruimte2 = Text(window3, text="", grid=[2,5,1,1])
lege_ruimte2 = Text(window3, text="", grid=[1,6])
Start_knop= PushButton(window3,text="Start",command=test_loopt,padx=50, pady=10, grid=[1,6] )
Start_knop.tk.config(foreground="white",background="green")
Stop_knop= PushButton(window3,text="Stop",command=stop_test,padx=50,pady=10,grid=[3,6])
Stop_knop.tk.config(foreground="white",background="red")
Exit_setting = PushButton(window3,command=verlaat_settings,text="Exit to Main Menu(F2)",padx=30,pady=10,grid=[2,6])我将通过我节目的这一部分告诉你:
verlaat_settings用于移动GUI中的低谷窗口,Stop_test用于预置当按下停止(也是重置时间状态)时的动作,test_loopt是实际的测试,计数器必须在这里运行它在shell中所做的操作。发布于 2018-05-25 06:40:45
因此,在搜索了一段时间之后,我在这里找到了一个页面,其中某个人在python2.7上遇到了完全相同的问题。
这个问题的解决方案是,所有的东西都在主循环中运行,主循环在这个test_loopt上等待永远,解决方案是创建一个线程,在我的例子中是这样的:
def test_loopt():
global Sec
global Min
text_test.value = "Test is running....."
timeLoop = True
while timeLoop:
Sec +=1
print(str(Min) + " Mins " + str(Sec) + " Sec ")
time.sleep(1)
if Sec == 60:
Sec = 0
Min += 1
if Min == 60:
Sec = 0
Min = 0
Hour = 1
if stop_test():
timeLoop = False
def start_test_loopt_thread():
global test_loopt_thread
test_loopt_thread = threading.Thread(target=test_loopt)
test_loopt_thread.deamon = True
test_loopt_thread.start()发布于 2020-12-09 13:49:14
这个答案是为了帮助遇到类似情况的用户。虽然建议的答案是富有想象力的,但却有点冗长。在GuiZero中,.display()函数创建一个无限循环。因此,任何正常python时间函数的使用都将阻止显示循环的执行。当使用GuiZero时,不要使用睡眠,也不要使用When循环。使用.repeat(工期、命令)更简单、更容易,例如,上面的代码应该可以工作,如果这一行:
Start_knop= PushButton(window3,text="Start",command=test_loopt,padx=50, pady=10, grid=[1,6] )改为:
Start_knop= PushButton(window3,text="Start",padx=50, pady=10, grid=[1,6])
Start_knop.repeat(1000, test_loopt)以及最初的test_loopt函数:
def test_loopt():
global Sec
global Min
text_test.value = "Test is running....."
timeLoop = True
while timeLoop:
Sec +=1
print(str(Min) + " Mins " + str(Sec) + " Sec ")
time.sleep(1)
if Sec == 60:
Sec = 0
Min += 1
if Min == 60:
Sec = 0
Min = 0
Hour = 1
if stop_test():
timeLoop = False修改为:
def test_loopt():
global Secs, Mins, Hrs
text_test.value = "Test is running....."
Secs +=1
print(str(Hrs) + " Hours " + str(Min) + " Mins " + str(Sec) + " Sec ")
if Secs%60 == 0:
Secs = 0
Mins += 1
if Min%60 == 0:
Secs, Mins = 0, 0
Hrs += 1如果必须注意,结果不是显示在GUI上,而是显示在IDE中,并且这个循环没有结束。还必须从以下几个方面修改stop_test函数:
def stop_test():
info("test_result","Test stopped at" + str(Hour) + " Hour " + str(Min) + " Mins " + str(Sec) + " Sec ")
text_test.value = "Test Stopped..."
timeLoop: False至
def stop_test():
info("test_result","Test stopped at" + str(Hrs) + " Hour " + str(Mins) + " Mins " + str(Secs) + " Sec ")
text_test.value = "Test Stopped..."
Start_knop.after(pause_loop)
def pause_loop():
pass或者其他类似的东西
发布于 2021-11-29 15:42:20
这在吉零的文件。中有一个基本的答案--问题是,由于循环的缘故,吉零的显示循环被阻塞了,就像你说的那样,它会延迟并最终崩溃。这是因为GuiZero是一个事件驱动的循环,这就是为什么它使用.display()循环来检查事件。
您需要使用.repeat方法(如下所示)来使应用程序重复该操作。
.repeat( [enter delay here in milliseconds] , [command to execute] )这意味着不需要函数中的循环。
我知道我回答这个问题有点晚了,但我希望它能帮上忙。
https://stackoverflow.com/questions/50508153
复制相似问题