tkinter第一章1

时间:2023-03-09 13:19:39
tkinter第一章1

tk1

------------------------------------------------------------------------------------------

import tkinter as tk

#app是一个Tk(界面)类
app = tk.Tk()
app.title("标题")

#the label是一个Label类
theLabel = tk.Label(app,text="我的第一个标签")#建立一个label类
theLabel.pack()

app.mainloop()

------------------------------------------------------------------------------------------

tk2

------------------------------------------------------------------------------------------

import tkinter as tk

class APP:#写一个窗口
  def __init__(self,master):
    frame = tk.Frame(master)#框架类,起到分类的作用
    #frame.pack()#自动调整位置
    frame.pack(side =tk.LEFT,padx=10,pady=10 )#调整为左边的位置 padx:x轴的间距 pady :y轴的间距

     #按钮类,内容为text fg:前景色 ,command:触发方法 bg:背景色
    self.hi_there = tk.Button(frame,text="打招呼",fg="blue",bg="green",command=self.say_hi)
    self.hi_there.pack()#自动调整位置

  def say_hi(self):#触发button按钮
    print("我在打招呼")

root = tk.Tk()#root 相当于雕刻的原木

app = APP(root)#APP这个类 相当于雕饰的过程

root.mainloop()#让这个艺术品运行起来

------------------------------------------------------------------------------------------