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 get the value of the selected Radiobutton in tkinter? Radio buttons are grouped by sharing one variable, and you read the choice from that variable. Includes why every button appears selected at once, why two separate groups deselect each other, and how to start with none chosen.
- 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.
- How do I get the value of a Checkbutton in tkinter? A Checkbutton reports through a variable, so without one there is nothing to read. How to attach an IntVar or BooleanVar, what .get() returns, and how to use your own values instead of 1 and 0.
- How do I get the selected value from a Combobox in tkinter? Read a ttk Combobox with .get(), react with the <<ComboboxSelected>> event, and stop people typing their own answers with state="readonly". Includes why the box starts empty.
- How do I use an OptionMenu in tkinter? An OptionMenu is a drop-down backed by a variable: the variable holds the choice and supplies the label on the button. How to set a default, read the choice, and rebuild the list at runtime.
- How do I use a Spinbox in tkinter? A Spinbox is a number field with arrows: set the range with from_ and to, step with increment, and read it with .get(). Includes why the value comes back as text and how to spin through words instead of numbers.
- How do I get the value of a Scale (slider) in tkinter? Read a tkinter Scale with .get(), or take the value the command callback is handed. Includes the trap that the callback receives a string, the difference between tk.Scale and ttk.Scale, and how to control the step.
- How do I add a scrollbar in tkinter? A tkinter scrollbar needs wiring in both directions: the scrollbar drives the widget, and the widget reports back. Miss one and the bar appears but never moves. The pattern for listboxes, text boxes and canvases.
- How do I add a separator line in tkinter? A ttk.Separator draws a divider between sections, but it has no length of its own — without fill it is one pixel square and invisible. How to size one, and the horizontal and vertical forms.
- How do I group widgets with a LabelFrame in tkinter? A LabelFrame is a bordered box with a caption that holds other widgets. How to put widgets inside one, why the parent matters, and when to use a plain Frame or a separator instead.
- How do I add a date picker to tkinter? Tkinter has no date field of its own. Install tkcalendar for a DateEntry with a drop-down calendar, read it with .get_date(), and set the display format without changing what your code receives.
- How do I make a scrollable frame in tkinter? A tkinter Frame cannot scroll. The standard fix is a Canvas holding the frame, plus a scrollbar and a binding that keeps the scroll region in step. Includes the one-line versions in CustomTkinter and ttkbootstrap.
- How do I add a circular meter or gauge in tkinter? Plain tkinter has no gauge, but ttkbootstrap ships a Meter widget: a circular dial with a number in the middle. How to build one, update it as work progresses, and read its value back.
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 →