1 min read

Tkinter quick reference

A quick reference for tkinter:

  1. window = Tk() to set up the window.
  2. widget = Widget(master=window) to set up a widget in the window.
  3. widget.pack() to add the widget to the window. Note there is no way to temporarily hide a widget without removing it; instead,call widget.pack_forget() and re-pack() when ready.
  4. widget.configure() to update widget text content and other config properties. Like: widget.configure(text="boo").
  5. button.bind("<Button-1>", callback) to bind an on-click callback to a button. The callback takes the tk.Event as a parameter.
  6. Text edit fields (Text()) use textbox.insert(tk.END, s) to append s to the textbox, and textbox.delete("1.0", tk.END) to clear. Note that neither method will work if textbox.state == "disabled". textbox.configure(text=s) might get around this if you want to set the content to s.
  7. window.mainloop() to start the app.

Worth noting that the bind() callback is called synchronously. This means if you're, say, making a long-running HTTP request, you'll want to use grequests to make the request asynchronously. I haven't needed that yet, though. Maybe next time I need to put tk and HTTP together it will matter whether I halt the whole app dead. This time it didn't (disposable dev tool).

Much more detail at Real Python, which I used to get through the app in the first place. :)