如果要开发一个比较大的程序,那么应该先把代码封装起来,在面向对象编程中,就是封装成类
先看代码:
import tkinter as tk
class App:
def __init__(self, root):
root.title("打招呼测试")
frame = tk.Frame(root)
frame.pack()
self.hi_there = tk.Button(frame, text="打招呼", fg="blue", command=self.say_hi)
self.hi_there.pack(side=tk.LEFT)
def say_hi(self):
print("您刚才通过点击打招呼触发了我:大家好,我是badao!")
root = tk.Tk()
app = App(root)
root.mainloop()
程序跑起来后:
代码解释:
#导入tkinter模块并创建别名tk
import tkinter as tk
class App:
def __init__(self, root):
#设置标题
root.title("打招呼测试")
#创建一个框架,然后在里面添加一个Button组件
#框架的作用一般是在复杂的布局中起到将组件分组的作用
frame = tk.Frame

这篇博客介绍了如何在Python中使用Tkinter库创建一个简单的GUI应用,当用户点击按钮时触发事件。通过创建一个App类,设置窗口标题,创建Button组件并将其绑定到say_hi方法,实现了点击按钮打印消息的功能。
最低0.47元/天 解锁文章
1524

被折叠的 条评论
为什么被折叠?



