简单组件使用案例

from tkinter import *
master = Tk()
master.geometry("700x600")
# Label 标签组件
l_show = Label(master,text="小怪兽")
l_show.pack(side="left")
# Entry 单行文本组件
e_show = Entry(master,width=10) # 创建10个字符宽的单文本输入框
e_show.pack(side="left")
# Text 多行文本组件
t_show = Text(master,width=10,height=4) # 创建多文本输入框
t_show.pack(side="bottom")
# Checkbutton 复选框组件
var = StringVar() # 字符串变量子类,创建对应的实例
c_show = Checkbutton(master,text="蓝猫",variable=var,onvalue="RGB",offvalue="L",fg="blue")
c_show.pack(side="top")
# Radiobutton 单选组件
v = IntVar() # 整型变量子类,创建对应的实例
r_show = Radiobutton(master,text="One",variable=v,value=1) # 创建单选框
r_show.pack(anchor=W)
# 创建框架
f_show = Frame(master,height=200,width=200,bd=1,bg='white',relief=SUNKEN) # 创建框架
f_show.pack(anchor="center")
# LabelFrame标签框架组件
lf_show = LabelFrame(master,text="Group",padx=5,pady=5) # 创建标签框架
lf_show.pack(padx=10,pady=10,expand="yes")
e1 = Entry(lf_show,width=10)
e1.pack()
e2 = Entry(lf_show,width=10)
e2.pack()
# Listbox列表框组件
lb_show = Listbox(master,bg="yellow",height=5,width=20) # 创建黄色列表框实例
lb_show.pack(side="top")
for item in ["one","two","three","four"]:
    lb_show.insert(END,item)
# Scrollbar 滚动条组件
s_show = Scrollbar(master)
s_show.pack(side=RIGHT,fill=Y)
lb_show1 = Listbox(master,fg="red",height=5,width=20) # 创建需要滚动条的列表框
lb_show1['yscrollcommand'] = s_show.set # 把滚动条对象赋给列表框属性
lb_show1.pack(side="right")
for item in ["1","2","3","4","5","6","7"]:
    lb_show1.insert(END,item)
s_show.config(command=lb_show.yview)
# Scale 刻度条组件
sc_show = Scale(master,from_=0,to=100)
sc_show.pack(side="right")
# Message和Button 组件
def showMessage(event): # 自定义按钮鼠标事件回调函数
    m1 = Message(master,text="高极合伙人",width=60)
    m1.pack()
b_show = Button(master,text="确认",fg="black") # 创建按钮实例
b_show.bind("<Button-1>",showMessage) # 按钮对象绑定鼠标回调函数
b_show.pack(side="left")
# Spinbox 组件
sb_show = Spinbox(master,from_=0,to=10) # 创建0~10的输入框
sb_show.pack(side="left")
# Toplevel子窗体组件
tl_show = Toplevel(master)
tl_show.wm_attributes("-topmost",1)
tl_show.title("OK!")
t1_show = Text(tl_show,width=10,height=4)
t2_show = Text(tl_show,width=10,height=4)
t1_show.pack()
t2_show.pack()
# panedWindow组件
pw = PanedWindow(orient=VERTICAL,bg="green")
pw.pack(fill=BOTH,expand=1)
for w in [Label,Button,Checkbutton,Radiobutton]:
    pw.add(w(pw,text='hello'))
mainloop()

运行结果:

posted @ 2020-05-27 19:39  小他_W  阅读(220)  评论(0编辑  收藏  举报