tkinter how-to
Tkinter ships with Python but not with much of a manual, so the same handful of questions get asked over and over — usually with an answer that works and a reason it works that nobody explains. Each page below gives the code first, then the part that trips people up.
- How do I fix ModuleNotFoundError: No module named 'tkinter'? Tkinter ships with Python but is packaged separately on Linux and Homebrew. The fix per platform, how to check whether it is really missing, and why a virtual environment can inherit the problem.
- Why is tkinter not working in VS Code? Tkinter usually works fine and VS Code is pointing at a different Python. How to check the selected interpreter, tell a Pylance squiggle from a real error, and what to do under WSL or SSH.
- Why is my tkinter window not showing? A tkinter script that runs and exits without a window is almost always a missing mainloop, widgets built after it, or a window placed off-screen. How to tell which, in order.
- How do I set the window size in tkinter? Set a tkinter window size with root.geometry("520x380"), position it, stop it being resized, and centre it on screen — plus why the window sometimes ignores you.
- How do I get the value from an Entry in tkinter? Read a tkinter Entry with .get(), or bind a StringVar. Includes the reason the value is empty — reading it at build time instead of inside the handler — and how to clear or preset the field.
- How do I display text in tkinter? Show text in tkinter with a Label, change it later with config() or a StringVar, and know when to reach for the Text widget instead. Includes why your label never updates.
- How do I open a new window in tkinter? Open a second tkinter window with tk.Toplevel — never a second tk.Tk() — keep it on top, make it modal, and pass data between the two windows.
- How do I clear a window in tkinter? Clear a tkinter window by destroying its children, or switch screens by swapping frames instead. Also covers clearing an Entry, a Text box and a Listbox.
- How do I close a window in tkinter? Close a tkinter window with destroy(), understand how it differs from quit(), close a Toplevel without killing the app, and ask "are you sure?" when someone clicks the X.
- How do I change the background colour in tkinter? Set a tkinter background with root.configure(bg=…) or bg on a widget — and find out why it does nothing on ttk widgets like Combobox, Notebook and Treeview, which need a style instead.
Longer reads: building a GUI online, embedding a matplotlib chart, and CustomTkinter versus ttkbootstrap. The widget reference covers every widget in the palette.
Or skip the lookup: lay the window out and read the Python it generates.
Open the tkinter designer →