Tkinter quick reference
A quick reference for tkinter:
window = Tk()
to set up the window.widget = Widget(master=window)
to set up a widget in the window.widget.pack()
to add the widget to the window. Note there is no way to temporarily hide a widget without removing it; instead,callwidget.pack_forget()
and re-pack()
when ready.widget.configure()
to update widget text content and other config properties. Like:widget.configure(text="boo")
.button.bind("<Button-1>", callback)
to bind an on-click callback to a button. The callback takes thetk.Event
as a parameter.- Text edit fields (
Text()
) usetextbox.insert(tk.END, s)
to appends
to the textbox, andtextbox.delete("1.0", tk.END)
to clear. Note that neither method will work iftextbox.state == "disabled"
.textbox.configure(text=s)
might get around this if you want to set the content tos
. 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. :)