import tkinter as tk
# 创建主窗口
root = tk.Tk()
root.title("Tkinter 示例")
# 创建标签组件
label = tk.Label(root, text="Hello, Tkinter!")
label.pack(pady=10)
# 创建按钮组件,并定义点击按钮时的事件处理函数
def on_button_click():
label.config(text="Button Clicked!")
button = tk.Button(root, text="Click Me", command=on_button_click)
button.pack(pady=10)
# 进入主事件循环
root.mainloop()
创建主窗口:
root = tk.Tk()
:创建一个主窗口对象。root.title("Tkinter 示例")
:设置窗口标题。创建标签组件:
label = tk.Label(root, text="Hello, Tkinter!")
:创建一个标签组件,显示文本 "Hello, Tkinter!"。label.pack(pady=10)
:将标签添加到窗口中,并设置上下间距为 10 像素。创建按钮组件:
def on_button_click():
:定义一个事件处理函数,当按钮被点击时调用此函数。label.config(text="Button Clicked!")
:修改标签的文本内容为 "Button Clicked!"。button = tk.Button(root, text="Click Me", command=on_button_click)
:创建一个按钮组件,按钮上显示文本 "Click Me",并绑定点击事件处理函数 on_button_click
。button.pack(pady=10)
:将按钮添加到窗口中,并设置上下间距为 10 像素。进入主事件循环:
root.mainloop()
:启动 Tkinter 的主事件循环,等待用户交互。上一篇:python .shape
下一篇:rabbitmq python
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站