import tkinter as tk
# 创建主窗口
root = tk.Tk()
root.title("Tkinter 示例")
# 创建标签组件
label = tk.Label(root, text="Hello, Tkinter!")
label.pack()
# 创建按钮组件,并定义点击按钮后的动作
def on_button_click():
label.config(text="Button Clicked!")
button = tk.Button(root, text="Click Me", command=on_button_click)
button.pack()
# 进入主事件循环
root.mainloop()
import tkinter as tk
导入了 Tkinter 模块,并将其命名为 tk
,以便后续代码中使用。root = tk.Tk()
创建了一个主窗口对象,并通过 root.title("Tkinter 示例")
设置了窗口标题。label = tk.Label(root, text="Hello, Tkinter!")
创建了一个标签组件,并通过 label.pack()
将其添加到窗口中。button = tk.Button(root, text="Click Me", command=on_button_click)
创建了一个按钮组件,并指定了点击按钮后执行的函数 on_button_click
。该函数会改变标签的内容。root.mainloop()
启动了 Tkinter 的主事件循环,使窗口保持打开状态并响应用户操作。上一篇:python 定义全局变量
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站