Guizero / TKinter中的计时器

时间:2018-05-24 11:24:56

标签: python-3.x user-interface tkinter timer

我正在寻求帮助。现在我正忙着为我的项目制作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])

我将通过我的程序的这一部分与您交谈,首先我们导入用于此目的的库。之后我们给timeLoop,我们的while变量,一个错误状态。然后我们给出变量值。下面是我们的Def循环verlaat_settings用于移动GUI中的窗口 Stop_test用于在按下停止时执行操作(也用于重置while状态)

test_loopt是实际测试,计数器必须在这里运行它在shell中的作用。

下面我们打开窗口并放置小部件。

我希望有人可以帮我解决这个问题!

2 个答案:

答案 0 :(得分:1)

此答案是为了帮助遇到类似情况的用户。 尽管提出的答案是富于想象力的,但它有些冗长。 在GuiZero中,.display()函数创建一个无限循环。因此,任何使用常规python时间函数的操作都会阻止显示循环的执行。 使用GuiZero时,请勿使用睡眠或while循环。 使用.repeat(duration,command)更简单易用 例如,如果以下行,则上面的代码应该起作用:

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

或类似的东西

答案 1 :(得分:0)

所以在搜索了一段时间后,我在这里找到了一个与python 2.7上的某个人有完全相同问题的页面。

对此的解决方案是,所有内容都在主循环中运行,并且主循环在此test_loopt上等待,解决方案是创建一个Thread,在我的情况下是:

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