Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to debug a Python tkinter application while in the mainloop?

I was trying to debug my python tkinter app but I noticed that my debugger just stopped working when it reached tkinter's mainloop() method.

Although my app was working as expected, interacting with it did not seem to update my debugger or variable values. After searching around I found very little information on the topic and that it is not possible to debug a tkinter app while in the mainloop since the core library is written in C.

Surely there must be some workarounds?

At the moment I am finding myself having to debug each and every interaction on my tkinter app manually, which is extremely time confusing and inefficient as my app grows complex.

like image 441
bwrr Avatar asked Oct 28 '25 15:10

bwrr


1 Answers

Don't put breakpoints before the mainloop. Put a breakpoint in the action you want to debug, e.g. a button command:

def command():
    print("hello")  # <-- breakpoint here

tk.Button(frame,
          text="Hello",
          command=command)

Then the debugger can stop there.

like image 140
Alex Hall Avatar answered Oct 30 '25 08:10

Alex Hall