PythonGUI programming (Tkinter)-basic concepts and core development steps

Python GUI programming (Tkinter)

Python provides multiple libraries for graphical development interfaces. Several commonly used Python GUI libraries are as follows:

  • Tkinter: The Tkinter module (Tk interface) is the interface of Python's standard Tk GUI toolkit. Tk and Tkinter can be used on most Unix platforms, and can also be applied to Windows and Macintosh systems. Subsequent versions of Tk8.0 can achieve the local window style and run well on most platforms.

  • wxPython: wxPython is an open source software. It is a set of excellent GUI graphics library in the Python language, allowing Python programmers to easily create a complete and functional GUI user interface.

  • Jython: Jython programs can be seamlessly integrated with Java. In addition to some standard modules, Jython uses Java modules. Jython has almost all the modules in standard Python that do not depend on the C language. For example, the user interface of Jython will use Swing, AWT or SWT. Jython can be compiled into Java bytecode dynamically or statically.


Tkinter programming

Tkinter is Python's standard GUI library. Python uses Tkinter to quickly create GUI applications.

Because Tkinter is built into the installation package of Python, as long as Python is installed, the Tkinter library can be imported, and IDLE is also written in Tkinter. For simple graphical interface Tkinter can still cope with it.

Development steps:
Creating a GUI program based on the tkinter module includes the following four core steps :

Sample code:

 1 # 1.创建应用程序主窗口对象(根窗口)
 2 # 通过类Tk的无参构造函数
 3 from tkinter import *
 4 from tkinter import messagebox
 5 
 6 root = Tk()
 7 # 调整窗口大小
 8 root.title("这是窗口标题")
 9 root.geometry("500x400+200+200")
10 
11 
12 # 2.在主窗空里面添加各种可视化组件,比如按钮(Button)文本框(Label)
13 btn01 = Button(root)
14 btn01["text"] = "我要送你几朵花"
15 
16 # 3.通过集合布局管理器,管理组件大小和位置
17 btn01.pack()
18 
19 
20 # 4.事件的处理:通过绑定事件处理程序,响应用户操作所触发的事件(比如单击双击)
21 
22 def songhuan(e):
23     messagebox.showinfo("Message", "送你一朵玫瑰花,不要爱上我")  # 第一块是文本目录,第二块是文本内容
24     print("给你玫瑰花")
25 
26 
27 # 单击左键,执行songhua方法
28 btn01.bind("<Button-1>", songhuan)
29 
30 # 调用主键的mainloop方法,进入事件循环
31 root.mainloop()

效果图:

Guess you like

Origin www.cnblogs.com/youliang-null/p/12718343.html