python脚本运行tk gui 点击按钮管理gui
要在Python脚本中运行Tkinter GUI,并使用按钮管理GUI,请按照以下步骤操作:
- 导入Tkinter模块
import tkinter as tk
- 创建主窗口
root = tk.Tk()
- 添加GUI元素
label = tk.Label(root, text="Hello, world!")
button = tk.Button(root, text="Click me!")
- 将元素放置到GUI中
label.pack()
button.pack()
- 添加按钮点击事件
def button_click():
print("Button clicked!")
button.config(command=button_click)
- 运行GUI
root.mainloop()
完整代码示例:
import tkinter as tk
# 创建主窗口
root = tk.Tk()
# 添加GUI元素
label = tk.Label(root, text="Hello, world!")
button = tk.Button(root, text="Click me!")
# 将元素放置到GUI中
label.pack()
button.pack()
# 添加按钮点击事件
def button_click():
print("Button clicked!")
button.config(command=button_click)
# 运行GUI
root.mainloop()
``
原文地址: http://www.cveoy.top/t/topic/g1Gm 著作权归作者所有。请勿转载和采集!