Python Tkinter Tutorial: Create GUIs Easily
Creating a graphical user interface (GUI) using the Tkinter tool in Python is very simple. Here is a straightforward example:
import tkinter as tk
# 创建主窗口
root = tk.Tk()
root.title("Hello, Tkinter!")
# 创建标签
label = tk.Label(root, text="Hello, Tkinter!")
label.pack()
# 运行主循环
root.mainloop()
In this example, we first imported the Tkinter module and created a main window called root. Afterwards, a label was created and added to the main window. Finally, by calling root.mainloop(), we started the main loop of the Tkinter application, keeping the program running.
Creating a GUI using Tkinter allows you to add components such as buttons, input fields, and list boxes, as well as define event handling functions. By reading the official Tkinter documentation or referring to online tutorials, you can learn more about creating complex GUI applications using the Tkinter tool.