【Python】Tkinter 体验

import tkinter as tk
root = tk.Tk()
root.title("work hard")
#添加一个Label组件,Label组件是GUI程序中最常用的组件之一
#Label组件可以显示文本,图标或图片
#在这里我们让它显示指定文本
theLabel = tk.Label(root,text="努力努力再努力")
#然后调用Label组件的pack()方法,用于自动调节组件自身的尺寸
theLabel.pack()
#注意:这个窗口还是不会显示的
#除非执行下面的代码
root.mainloop()

还可以拉长窗口,可以看见

import tkinter as tk

class App:
    def __init__(self,root):
        #创建一个框架,然后在里面添加一个Button按钮组件
        #框架一般是用于复杂的布局中起到将组建分组的作用
        frame = tk.Frame(root)
        frame.pack(side=tk.TOP,padx=220,pady=10)
        #创建一个按钮组件,fg是foreg的缩写,就是设置前景色的意思
        self.hi_there = tk.Button(frame,text="say hi",bg="pink",fg="black",command=self.say_hi)
        self.hi_there.pack()

    def say_hi(self):
        print("你好")
        print("hello")

#创建一个toplevel的根窗口,并把它作为参数实例化app对象
root = tk.Tk()
app = App(root)
#开始主事件循环
root.mainloop()

 每点击一次按钮,就会打印一遍内容

猜你喜欢

转载自blog.csdn.net/CSDN___CSDN/article/details/81270584