Python Tkinter 教程:使用 Tk 库制作 GUI 软件
Python 的 Tkinter 库可以用于创建 GUI 应用程序。以下是基本步骤:
- 导入 Tkinter 库
import tkinter as tk
- 创建主窗口
root = tk.Tk()
- 添加组件
label = tk.Label(root, text='Hello, World!')
button = tk.Button(root, text='Click Me!')
- 设置组件属性
label.config(font=('Arial', 24))
button.config(command=some_function)
- 布局组件
label.pack()
button.pack()
- 启动主循环
root.mainloop()
完整示例:
import tkinter as tk
def say_hello():
print('Hello, World!')
root = tk.Tk()
label = tk.Label(root, text='Hello, World!')
button = tk.Button(root, text='Click Me!', command=say_hello)
label.config(font=('Arial', 24))
label.pack()
button.pack()
root.mainloop()
这个示例创建了一个带有一个标签和一个按钮的窗口。当用户点击按钮时,程序将打印一条消息。您可以根据需要添加更多组件和功能来制作您自己的应用程序。
原文地址: https://www.cveoy.top/t/topic/nYSV 著作权归作者所有。请勿转载和采集!