This tutorial is the personal effort of bloggers. If there is any quotation, the quotation information should be indicated at the top of the article
tkinter practical tutorial I installing tkinter under Linux environment
tkinter sub module ttk of tkinter practical tutorial II
tkinter Button control in tkinter practical tutorial III
Installing tkinter in Linux Environment
tkinter is a set of controls described in the python programming language to build a graphical user interface (GUI). Therefore, before learning tkinter installation, you need to make sure that you have installed Python correctly.
You can directly install Python 3 and tkinter using the following command.
sudo apt-get install python3 # Installing Python 3 sudo apt-get install python3-tk # Installing tkinter
Installing Python 3
This tutorial takes python 3 as the target language and directly inputs python or python 3 on the Linux terminal. If the following contents are output, it proves that you have correctly installed python 3.
ubuntu:~$ python Python 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
If you output the following, it means that you have installed Python 2. You can try entering Python 3 to start Python 3.
ubuntu:~$ python Python 2.7.17 (default, Feb 27 2021, 15:10:58) [GCC 7.5.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
If you input Python 3 and the terminal outputs something similar to the following, then you have not installed Python 3.
ubuntu:~$ python3 Command 'python3' not found, but can be installed with: sudo apt install python3
You can install Python 3 using the following command:
sudo apt-get install python3
Installing pip3
pip is a Python package management tool. You can use it to install and uninstall packages. You usually need to install pip before learning Python.
pip2 is the package management tool of Python 2, and pip3 is the package management tool.
If you input the pip --version command and output the following contents, it proves that you have not installed pip.
ubuntu:~$ pip --version Command 'pip' not found, but can be installed with: sudo apt install python3-pip
You can install pip3 using the following instructions:
sudo apt-get intall python3-pip
Before installing pip, be sure to install python3 correctly.
If you enter the pip --version command and output the following, congratulations on successfully installing pip3.
ubuntu:~$ pip --version pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
After pip3 is installed for the first time, it usually needs to be updated. Please use the following command:
pip3 install --upgrade pip
So far, both Python 3 and pip3 have been successfully installed on your computer.
Installing tkinter
If you enter import tkinter in the python 3 environment and press enter to output the following content, it indicates that you have not installed tkinter.
ubuntu:~$ python3 Python 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tkinter Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'tkinter' >>>
At this point, you need to use the following command to install tkinter:
sudo apt-get install python3-tk
If you output the following in the python 3 environment, congratulations on successfully installing tkinter:
ubuntu:~$ python3 Python 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tkinter >>>
A simple tkinter test program
To make sure everything works, let's try running a "Hello World" program in tkinter. For such short content, you can input it directly into the interpreter instead of putting it into a file using your favorite text editor.
from tkinter import * from tkinter import ttk root = Tk() ttk.Button(root, text="Hello World").grid() root.mainloop()
After the above code is executed, a window as shown in the following figure will pop up:
The code in this article is executed under wsl ubuntu, so the window belongs to Windows style. If you execute this code in other environments, it may be slightly different and will not affect your learning.
tkinter practical tutorial I installing tkinter under Linux environment