import tkinter as tk
# 创建主窗口
root = tk.Tk()
root.title("Python Windows App")
# 设置窗口大小
root.geometry("300x200")
# 定义按钮点击事件处理函数
def on_button_click():
label.config(text="Hello, Tkinter!")
# 创建标签
label = tk.Label(root, text="Welcome to Tkinter!")
label.pack(pady=20)
# 创建按钮
button = tk.Button(root, text="Click Me", command=on_button_click)
button.pack()
# 进入主事件循环
root.mainloop()
tkinter
是 Python 的标准 GUI 库,用于创建 Windows 应用程序。Tk()
创建一个主窗口,并设置标题为 "Python Windows App"。geometry()
方法设置窗口的初始大小为 300x200 像素。on_button_click()
函数,修改标签的文字内容。Label
和 Button
小部件创建标签和按钮,并将它们添加到窗口中。mainloop()
启动应用程序的主事件循环,等待用户交互。这个示例展示了如何使用 tkinter
创建一个简单的 Windows 应用程序。
上一篇:python ord和chr
下一篇:python时间格式
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站