Python tkinter GUI freezing/crashing -
from tkinter import * import tkfiledialog import tkmessagebox import os import ttk import serial import timeit import time ###################################################################################### class myapp: def __init__(self, parent): ######################################################## #setup frames self.middleframe = frame(parent) #middle frame self.middleframe.pack() #global variables self.chip_number = 0 #number of chip testing ########################################### #middle frame setup label(self.middleframe, text='done').grid(row=8, column=1, sticky = e) self.done = canvas(self.middleframe, bg="yellow", width=10, height=10) self.done.grid(row=8, column=2) label(self.middleframe, text='chip number:').grid(row=9, column=1, sticky = e) #start button self.button1 = button(self.middleframe,state=normal, command= self.start_pre) self.button1["text"]= "start" self.button1.grid(row=1, column=2, sticky = e) ########################################### #action of start button def start_pre(self): x = 0 while x<10000: self.start_button() x=x+1 #talking board def start_button(self): #increase chip count number , update self.chip_number += 1 label(self.middleframe, text=str(self.chip_number)).grid(row=9, column=2, sticky = e) #reset-yellow self.reset_color() print "still working", self.chip_number self.done.configure(background="green") self.done.update_idletasks() ############################################################### #color boxes #reset def reset_color(self): self.done.configure(background="yellow") self.done.update_idletasks() ############################################################################################################### #start programs root = tk() #makes window root.title("interface") myapp = myapp(root) #this runs program root.mainloop() #keep window open
with program, first push start button. print "still working" , gui update chip number , blink done light on , over. start button go function execute 10000 times. after 3000 iterations, gui freeze, program still print "still working". how keep gui crashing?
there many problems code. one, fundamentally flawed:
while self.stop == true: self.start_button() time.sleep(0.5)
you can't expect gui behave code that. general rule of thumb should never have main thread of gui call sleep
. causing sleep
prevents event loop processing any events, including low level events such requests refresh screen.
the use of sleep
has been asked , answered many times on stackoverflow. might find of questions useful. example,
- windows thinks tkinter not responding
- python tkinter coords function not moving canvas objects inside loop
- how widgets update in tkinter?
- tkinter multiple operations
- python tkinter stopwatch error
you have problem falls category of memory leak. while loop, call self.start_button()
indefinitely. happens once second, due sleep being called half second in loop, , half second in start_button
.
each time call start_button
, create another label widget stack on top of previous widgets in row 9, column 2. cause program crash. i'm surprised causes program fail quickly, that's beside point.
my recommendation start on simple example nothing update label every second. working understand basic mechanism. then, once it's working, can add in code reads serial port.
Comments
Post a Comment