While loop is taking forever and freezing screen? - python

I have this large program, one of whose parts includes the click of a button (this GUI was made in wxPython). Basically, the button is a timer, and when you click on it, the text of the button switches based on the time left, e.g. 5 Seconds, 4 Seconds, 3 Seconds... For some reason, though, when I click on the button in the GUI, the entire program freezes and I have to force quit the app. (Everything else in the app works fine and responds, until I click on this one button). Here is the event handler that is called when the button is clicked.
Thanks in advance!
def TossUpTimer(self, event):
while self.tossuptimer > -1:
while self.tossuptimer > 0:
self.tossupbutton.SetLabel(str(self.tossuptimer) + "Seconds")
time.sleep(1)
self.tossuptimer -= 1
if self.tossuptimer == 0:
self.tossupbutton.SetLabel("Time is Up!")
time.sleep(1)
self.tossupbutton.SetLabel(str(self.tossuptimer) + "Seconds")
self.tossuptimer = 5

Why your GUI app freezes is an attempt to explain the basic idea, and all of the different options for dealing with it.
But here's a short version:
The whole point of a GUI app is that it's running an event loop, responding to events as they come in from the user or the OS. You write handlers for particular events. Until your handler returns, the GUI can't process the next event. That means that the entire app is frozen.
You need to return from your handler as quickly as possible. What you can't do, ever, is time.sleep(1) or a long while loop or anything of the kind directly in a handler.
There are various ways around this. The two most common are to ask the event loop to run some of your code later, or to move it to a background thread. (wx adds a third way, SafeYield and friends, which is appropriate in some cases, but probably not what you want here.)
For the first alternative, the idea is that, instead of looping around sleep, you write a function that handles a single iteration of the loop, and schedule a timer to fire that function once/second. This is known as "turning your control flow inside out", which can be a bit hard to get your head around.
Unfortunately, I'm not 100% sure what you're trying to do, because, as Jon Clements points out, your logic doesn't make sense even in a sequential program. But I'll write something simple: when you click the button, it will count down once/second for 5 seconds, then stop counting.
def OnTossUpTimer(self, event):
self.tossuptimer -= 1
if self.tossuptimer > 0:
self.tossupbutton.SetLabel(str(self.tossuptimer) + "Seconds")
else:
self.tossupbutton.SetLabel("Time is Up!")
self.tossuptimer_timer.Stop()
def TossUpTimer(self, event):
self.tossuptimer = 5
self.tossupbutton.SetLabel(str(self.tossuptimer) + "Seconds")
self.tossuptimer_timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTossUpTimer, self.tossuptimer_timer)
self.tossuptimer_timer.Start(1000, False)
See Using wx.Timers and the Timer docs for more details.
The threaded version is simpler in some ways, more complicated in others. You can leave your sequential logic alone, but you can't talk to any GUI widgets except indirectly, by calling PostEvent. So:
class LabelUpdateEvent(wx.PyEvent):
EVT_LABEL_UPDATE_ID = wx.NewId()
def __init__(self, data):
wx.PyEvent.__init__(self)
self.SetEventType(EVT_LABEL_UPDATE_ID)
self.data = data
def SetLabelOnMainThread(self, value):
wx.PostEvent(self.SetLabelForReal, ResultEvent(value))
def SetLabelForReal(self, event):
self.tossupbutton.SetLabel(event.data)
def TossUpTimerThread(self):
self.tossuptimer = 5
while self.tossuptimer > 0:
self.SetLabelOnMainThread(str(self.tossuptimer) + "Seconds")
time.sleep(1)
self.tossuptimer -= 1
self.SetLabelOnMainThread(str(self.tossuptimer) + "Time is Up!")
def TossUpTimer(self, event):
threading.Thread(target=self.TossUpTimerThread).start()
If you're doing a lot of this, you probably want to write more generic "on-main-thread" functions, instead of all this boilerplate for each function. (You can, in fact, write a single completely-generic function, which wxPython really ought to come with but doesn't.)

Related

How can my program be prevented from freezing?

I have decided to finally work on a project, as I've tried to code in python before, with at least some success. In my project, I am trying to build a menu that lets me "Auto-farm" in a game. It uses 3 modules, namely pynput, pause, and PySimpleGUI.
Whenever I run the code, it runs fine, until I click the button that starts the automation part. It runs completely fine, but I have to force close the GUI prompt that shows up as it just completely stops responding to input until you close it.
How I can make a stop button, and stop my program from freezing up?
I am using 2 files to keep this project slightly more organized, although I don't know if this is the best way to go around doing this. These 2 files are main.py and chand.py.
main.py
import PySimpleGUI as sg
import chand
loop = 0
layout = [[sg.Text("Welcome to RedGrowie's autofarm menu!")], [sg.Button("Chandeliers")]]
window = sg.Window("Autofarming Menu", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == "Chandeliers":
loop = 1
if loop == 1:
chand.Chandeliers.start(self=chand.Chandeliers)
window.close
chand.py
from pynput.keyboard import Key, Controller
import pause
keyboard = Controller()
start = "0"
class Chandeliers:
def d_press(self):
keyboard.press("d")
pause.milliseconds(70)
keyboard.release("d")
pause.milliseconds(300)
keyboard.release(Key.space)
def space_press(self):
keyboard.press(Key.space)
pause.milliseconds(1)
#keyboard.release(Key.space)
def start(self):
start = "1"
while start == "1":
self.d_press(self)
self.space_press(self)
Your Chandeliers.start function loops indefinitely, so the call from the main loop in main.py never gets returned to. If you want both loops to be running at the same time, you probably need to use threading or some other means of concurrency. Or you might be able to interleave the two loops somehow, depending on the timing requirements for each one.
As a side note, you are using your Chandeliers class in a very odd way. You're never creating an instance of the class, but rather calling the methods it defines as if they were class methods (but with manual passing of the class, in the misleadingly named self argument.
You should probably not do that. Either treat the class as a normal one, and create an instance:
cha = chand.Chandeliers()
chat.start() # and change start to not manually pass self any more
Or you should do away with the unneeded class all together and just make the methods into top-level functions.

stop self.after when interrupt happens

I started coding snake in python 3. As GUI i use Tkinter.
I got a timer which waits for a second an then calls the method again. Well now my question is how to i stop the self.wait?
I know i could work around this pretty easily, but i had this problem already somewhere else, so it would be nice to know how i can stop this.
This is the method which moves the snake around. (Only the timer is critical). The timer is here so it moves every second.
def move_snake(self):
self.after(1000, self.move_snake)
# code goes on
Now if i change the direction (by pressing a button) i do following:
def change_direction(self, event):
self.pressed = event.keysym
self.move_snake()
If i do this this way the "old" timer still is active and therefore the method gets called multiple times (it adds an additional timer when you press an button).
It would be nice that just the latest timer is activated.
Do you need more information?
Assuming that move_snake uses self.pressed, you don't need to call move_snake inside of change_direction.
However, if you really want to stop the old loop and start a new loop, you can save the id that is returned from after and give that to after_cancel:
def move_snake(self):
self.after_id = self.after(1000, self.move_snake)
# code goes on
def change_direction(self, event):
self.pressed = event.keysym
# cancel the old loop
self.after_cancel(self.after_id)
# start a new loop
self.move_snake()

Weird behavior - wxPython Events

I have this following piece of code. It is working sometimes, it is not the other time.
def OnReset(self, event):
self.reset_pump.Disable() # Disables the button so it is clicked
self.WriteToController([0x30],'GuiMsgIn') # Sends the reset command
self.flag_read.set()
self.tr.join()
time.sleep(2)
start = time.time()
self.offset_text_control.Clear()
print "Helloin reset"
self.gain_text_control.Clear()
self.firmware_version_text_control.Clear()
self.pump_rpm_text_control.Clear()
self.pressure_text_control.Clear()
self.last_error_text_control.Clear()
self.error_count_text_control.Clear()
self.pump_model_text_control.Clear()
self.pump_serial_number_text_control.Clear()
self.on_time_text_control.Clear()
self.job_on_time_text_control.Clear()
#self.MessageBox('Pump RESET going on Click OK \n')
# Having the above step is useful
print time.time() - start
#self.ser.close()
wx.CallLater(3000, self.CalledAfter, [event,])
def CalledAfter(self, event):
self.tr = threading.Thread(target=ReadData, name="ReadThread", args=(self.ser, self.flag_read))
self.tr.daemon = True
self.tr.start()
self.reset_pump.Enable()
What it does is When I click the Reset button on my GUI, it has to clear certain text fields on the GUI. It has to clear it, only after joining the self.tr thread.
Once it clears, it will execute the command wx.CallLater(3000, self.CalledAfter, [event,]). Which then starts a new thread again.
Apparently The .Clear() command is working very at a non consistent level, it is working some time, not working the other times, and working again.
Any idea why this might happen would be very helpful.
There seems to be a difference in the way SetValue and Clear update the windows. Calling SetValue("") seems to be a suitable workaround to this behavior.

Python tkinter time.sleep()

How come when I run my code, it will sleep for 3 seconds first, then execute the 'label' .lift() and change the text? This is just one function of many in the program. I want the label to read "Starting in 3...2...1..." and the numbers changing when a second has passed.
def predraw(self):
self.lost=False
self.lossmessage.lower()
self.countdown.lift()
self.dx=20
self.dy=0
self.delay=200
self.x=300
self.y=300
self.foodx=self.list[random.randint(0,29)]
self.foody=self.list[random.randint(0,29)]
self.fillcol='blue'
self.canvas['bg']='white'
self.lossmessage['text']='You lost! :('
self.score['text']=0
self.countdown['text']='Starting in...3'
time.sleep(1)
self.countdown['text']='Starting in...2'
time.sleep(1)
self.countdown['text']='Starting in...1'
time.sleep(1)
self.countdown.lower()
self.drawsnake()
It does this because changes in widgets only become visible when the UI enters the event loop. You aren't allowing the screen to update after calling sleep each time, so it appears that it's sleeping three seconds before changing anything.
A simple fix is to call self.update() immediately before calling time.sleep(1), though the better solution is to not call sleep at all. You could do something like this, for example:
self.after(1000, lambda: self.countdown.configure(text="Starting in...3"))
self.after(2000, lambda: self.countdown.configure(text="Starting in...2"))
self.after(3000, lambda: self.countdown.configure(text="Starting in...1"))
self.after(4000, self.drawsnake)
By using after in this manner, your GUI remains responsive during the wait time and you don't have to sprinkle in calls to update.

Creating Toplevel widgets that are thread safe

I'm trying to learn how to use the thread module. I followed along with the instructions here: http://effbot.org/zone/tkinter-threads.htm
My hope is the test script will:
Print out the "count" every two seconds
Show a pop-up dialog window (also every 2 seconds)
The pop-ups should be allowed to accumulate (if I don't click "OK" for a while, there should be
multiple pop-ups)
However, when I run this script it will freeze the main window and after a while crash. I think I'm not implementing the thread module correctly.
Could someone please have a look and point out what I'm doing wrong?
Here is what I've tried so far:
from Tkinter import *
import thread
import Queue
import time
class TestApp:
def __init__(self, parent):
self.super_Parent = parent
self.main_container = Frame(parent)
self.main_container.pack()
self.top_frame = Frame(self.main_container)
self.top_frame.pack(side=TOP)
self.bottom_frame = Frame(self.main_container)
self.bottom_frame.pack(side=TOP)
self.text_box = Text(self.top_frame)
self.text_box.config(height=20, width=20)
self.text_box.pack()
self.queue = Queue.Queue()
self.update_me()
def show_popup(self):
self.my_popup = Toplevel(self.main_container)
self.my_popup.geometry('100x100')
self.popup_label = Label(self.my_popup, text="Hello!")
self.popup_label.pack(side=TOP)
self.pop_button = Button(self.my_popup, text="OK", command=self.my_popup.destroy)
self.pop_button.pack(side=TOP)
def write(self, line):
self.queue.put(line)
def update_me(self):
try:
while 1:
line = self.queue.get_nowait()
if line is None:
self.text_box.delete(1.0, END)
else:
self.text_box.insert(END, str(line))
self.text_box.see(END)
self.text_box.update_idletasks()
except Queue.Empty:
pass
self.text_box.after(100, self.update_me)
def pipeToWidget(input, widget):
widget.write(input)
def start_thread():
thread.start_new(start_test, (widget,))
def start_test(widget):
count = 0
while True:
pipeToWidget(str(count) + "\n", widget)
count += 1
time.sleep(2)
widget.show_popup()
root = Tk()
widget = TestApp(root)
start_button = Button(widget.bottom_frame, command=start_thread)
start_button.configure(text="Start Test")
start_button.pack(side=LEFT)
root.title("Testing Thread Module")
root.mainloop()
I can't reproduce your problem, but I can see why it would happen.
You're using the queue to pass messages from the background thread to the main thread for updating text_box, which is correct. But you're also calling widget.show_popup() from the background thread, which means it creates and displays a new Toplevel in the background thread. That's not correct.
All UI code must run in the same thread—not all UI code for each top-level window, all UI code period. On some platforms, you may get away with running each window in its own thread (or even free-threading everything), but that isn't supposed to work, and definitely will crash or do improper things on some platforms. (Also, that single UI thread has to be the initial thread on some platforms, but that isn't relevant here.)
So, to fix this, you need to do the same dance for creating the popups that you do for updating the textbox.
The obvious way to do that is to move the widget.show_popup() to the loop in update_me(). If you want it to happen 2 seconds after the textbox updates, just add self.top_frame.after(2000, self.show_popup) to the method.
But I'm guessing you're trying to teach yourself how to have multiple independent updating mechanisms, so telling you "just use a single update queue for everything" may not be a good answer. In that case, just create two queues, and a separate update method servicing each queue. Then, do your pipeToWidget, sleep 2 seconds, then pipeToPopup.
Another way around this is to use mtTkinter. It basically does exactly what you're doing, but makes it automatic, pushing each actual Tk GUI call onto a queue to be run later by the main loop. Of course your objects themselves have to be thread-safe, and this also means that you have to deal with the GUI calls from one thread getting interleaved with calls from another thread. But as long as neither of those is a problem (and they don't seem to be in your case), it's like magic.
If you want to know why this is freezing and/or crashing for you on Win7 and not for me on OS X 10.8… well, you really need to look into a mess of Tcl, C, and Python code, and also at how each thing is built. And, unless it's something simple (like your Tk build isn't free-threaded), it wouldn't tell you much anyway. The code isn't supposed to work, and if it seems to work for me… that probably just means it would work every time until the most important demo of my career, at which point it would fail.

Categories

Resources