Python GUI开发:tkinter库的应用教程实例
Tkinter是Python标准库中的一个GUI(图形用户界面)库,它为我们提供了构建桌面应用程序的简单工具。
以下是一些使用Tkinter创建基本GUI的例子:
- 创建一个主窗口:
```python
import tkinter as tk
创建主窗口
root = tk.Tk()
设置窗口标题
root.title(“Hello, GUI!”)
显示窗口
root.mainloop()
2. 添加按钮并处理点击事件:
```python
def button_click():
label.config(text="Button clicked!")
# 创建标签
label = tk.Label(root, text="Click the button")
# 定位和大小调整
label.pack(pady=10)
# 创建按钮
button = tk.Button(root, text="Click me!", command=button_click)
# 添加按钮并定位
button.pack()
# 显示窗口
root.mainloop()
以上就是使用Tkinter创建基本GUI的一些基本操作。在实际项目中,你还可以根据需求添加更多的控件和功能。
还没有评论,来说两句吧...