Right now I am busy with making a GUI for my project. It also needs a test driven by time. However when I press the start button, the counter starts counting but the Gui freezes, so you cannot press the stop button. And eventually the Program stall and shut his self down.
Take a look at my code :
###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])
I will talk you trough this part of my program:
verlaat_settings
is used to move trough windows in the GUI Stop_test
is used to preform actions when stop is pressed (also to reset the while state) test_loopt
is the actual test, the counter has to run by here what it does in the shell. so after searching for a while I found a page on here with someone on python 2.7 having the exact same problem.
the solution to this is that everything runs in the main loop and the main loop waits for ever on this test_loopt
the solution was making a Thread, which in my case was :
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()