Tkinter comes with Python on Windows and Mac, but you may need to install it separately on Linux
Tkinter is Python's built-in library for creating windows, buttons, text boxes, and other graphical elements. On Windows and Mac, it arrives with Python automatically. On Linux, you install it through your system's package manager in one command. The installation takes less than a minute either way.
Before you install anything, check whether Tkinter is already on your machine. Open a terminal or command prompt and type python3 -m tkinter. If a small window appears with a button labeled "Click me!", Tkinter is ready to use. If you see an error instead, follow the steps for your operating system below.
Key Takeaways
- Windows and Mac users have Tkinter already; test it by running python3 -m tkinter in a terminal.
- Linux users install Tkinter through apt (Ubuntu/Debian), dnf (Fedora), or pacman (Arch) depending on their distribution.
- After installation, verify Tkinter works by running the test command again and confirming a window appears.
- Once installed, you import Tkinter into a Python script with import tkinter as tk and start building interfaces.
Installing Tkinter on Windows
Windows includes Tkinter with Python, so you likely do not need to do anything. Run python3 -m tkinter in Command Prompt or PowerShell. If the test window appears, you are done.
If you see an error, your Python installation may have skipped Tkinter. Reinstall Python from python.org, and during setup, make sure the checkbox labeled "tcl/tk and IDLE" is checked. Complete the installation, then test again with python3 -m tkinter.
If you have multiple Python versions installed, the error may mean you are running the wrong one. Type python --version to see which version runs by default. If it shows Python 2, use python3 -m tkinter instead. If it still fails, uninstall all Python versions and reinstall the latest one from python.org with the Tkinter checkbox enabled.
Installing Tkinter on Mac
Mac includes Tkinter with Python from python.org. If you installed Python through Homebrew instead, Tkinter may not be present. Test first with python3 -m tkinter.
If the test fails and you used Homebrew, reinstall Python with brew install python-tk@3.11 (replace 3.11 with your Python version). Find your version by typing python3 --version. After installation, test again.
If you installed Python from python.org but still see an error, your installation may be incomplete. read the latest Python installer from python.org, run it, and select "Install for all users" during setup. This ensures Tkinter and all standard libraries are included.
Installing Tkinter on Ubuntu and Debian Linux
Ubuntu and Debian separate Tkinter into its own package. Open a terminal and run sudo apt update, then sudo apt install python3-tk. Enter your password when prompted. The installation completes in seconds.
After it finishes, test Tkinter with python3 -m tkinter. A window should appear. If you see an error about "no display", you are on a headless server (no graphical interface), and Tkinter cannot run there.
If the package manager says python3-tk is not found, your system may use a different package name. Try sudo apt install tk instead, which installs Tkinter for all Python versions on your machine.
Installing Tkinter on Fedora and Red Hat Linux
Fedora and Red Hat use dnf instead of apt. Open a terminal and run sudo dnf install python3-tkinter. Enter your password when prompted.
Once the installation completes, test with python3 -m tkinter. The test window confirms Tkinter is working.
If dnf cannot find the package, your system may have a different Python version installed. Type python3 --version to check. If you have Python 3.10, try sudo dnf install python3.10-tkinter instead, replacing 3.10 with your actual version number.
Installing Tkinter on Arch Linux
Arch Linux uses pacman. Open a terminal and run sudo pacman -S tk. Enter your password when prompted.
Test the installation with python3 -m tkinter. If the window appears, Tkinter is ready.
The tk package on Arch installs Tkinter for all Python versions. If you see an error after installation, make sure you are running python3 and not python, which may point to an older version.
Writing and running your first Tkinter program
Once Tkinter is installed, create a new file called hello.py and paste this code:
import tkinter as tk root = tk.Tk() root.title("My First Window") root.geometry("300x200") label = tk.Label(root, text="Hello, Tkinter!") label.pack() root.mainloop()
Save the file, then run it from a terminal with python3 hello.py. A window appears with the text "Hello, Tkinter!" inside. This window stays open until you close it. You now have a working Tkinter environment and can start building more complex interfaces by adding buttons, text fields, and other widgets.
The code above creates a window 300 pixels wide and 200 pixels tall, sets its title to "My First Window", and places a label with text inside. The mainloop() function keeps the window open and responsive to clicks and keyboard input. When you close the window, the program ends.
Frequently Asked Questions
What if python3 -m tkinter shows an error about "no module named tkinter"?
Tkinter is not installed. Follow the steps for your operating system above. On Windows or Mac, reinstall Python and check the Tkinter checkbox. On Linux, use your package manager to install the python3-tk or tk package.
Can I use Tkinter with Python 2?
Python 2 reached end of life in 2020 and is no longer supported. Use Python 3.6 or newer. If you have both Python 2 and Python 3 installed, make sure you run python3 (not python) to test and run your code.
Do I need to install anything else to use Tkinter?
No. Tkinter is self-contained and does not require additional libraries. Once it is installed, you can import it and start building interfaces when ready.
Why does the test window look different on my Mac than on Windows?
Tkinter uses the native graphical toolkit for each operating system. Mac windows follow macOS design, Windows windows follow Windows design, and Linux windows follow your desktop environment. The code is identical; only the appearance changes.
What should I do if Tkinter still does not work after following these steps?
Verify your Python version with python3 --version and make sure it is 3.6 or newer. Check that you are using python3 and not python. On Linux, confirm your distribution and use the correct package manager. If errors persist, try uninstalling and reinstalling Python completely.