I'm trying to make a program, and as you can probably already guess, I'm using tkinter. I have seen from other questions on this site that tk.mainloop() contains root.update_idletasks() and root.update(). I also understand that it blocks, and that I can't really run anything in main() after it. Maybe threading is the solution, but I'd rather not have to wrap my head around that just yet. My first thought was that I should just abandon tk.mainloop() in favor of including these two methods in my own mainloop().
However, I ran into a small problem. I had some tasks that I could toggle on and off with a button. While these tasks were running, if I closed the window, it would crash. If without, it would close gracefully. Why it crashes isn't all that important (mostly because I don't care if it crashes when it's closing, because the user is done with it), but it did tell me that there's more to mainloop() than just:
def mainloop(self):
while true
self.update()
self.update_idletasks()
From what I've seen, I've surmised that parts of tkinter are integrated into the interpreter (or other some such nonsense that means I can't just go in and read it). Thus, here is my question:
What exactly does tkinter.tk.mainloop() do? Is the n argument a number of times it can run? If so, would there be anything wrong with just running it one iteration inside a method I define, that itself is a loop?
If n isn't iterations, what is it? Is there any built-in way I can add tasks to be carried out each loop?
None of Tkinter's actual functionality is implemented in Python; the Python side is just an interface to the embedded Tcl language interpreter, which is where Tk actually lives. As such, it seems unlikely that you could replace mainloop() with anything written in Python.
Fortunately, there's no reason to - Tkinter provides the after() and after_idle() methods, which allow you to schedule tasks to be performed on a future iteration of the event loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With