Tkinter控件

1.顶层(Toplevel)

Toplevel为其他控件提供单独的容器。共有四种类型
(1)主顶层,作为根被应用,应该就是root
(2)子顶层,依赖于根,根破坏,子顶层也被破坏
(3)临时顶层,画在父顶层顶部,父顶层最小化,他们也被隐藏
(4)未被视窗管理者创建的顶层通过设置overrideredirect标志位非零值来创建,该窗口不能缩放或拖动。

from tkinter import *

root=Tk()
Label(root,text="This is the main(default) Toplevel").pack(pady=10)

t1=Toplevel(root)
Label(t1,text="This is a child of root").pack(pady=10,padx=10)

t2=Toplevel(root)
Label(t2,text="This is a transient window of root").pack(padx=10,pady=10)
t2.transient(root)

t3=Toplevel(root)
Label(t3,text="No wm decorations").pack(padx=10,pady=10)
t3.overrideredirect(1)

2.框架(Frame)
Frame为其他组件提供容器
例1

from tkinter import *

root=Tk()

for relief in(RAISED,SUNKEN,FLAT,RIDGE,GROOVE,SOLID):
    f=Frame(root,borderwidth=2,relief=relief)
    Label(f,text=relief,width=10).pack()
    f.pack(side=LEFT,padx=5,pady=5)

例2

from tkinter import *

root=Tk()

of=[None]*5
for bdw in range(5):
    of[bdw]=Frame(root,borderwidth=0)
    Label(of[bdw],text="borderwidth=%d"%bdw).pack(side=LEFT)
    for relief in(RAISED,SUNKEN,FLAT,RIDGE,GROOVE,SOLID):
        f=Frame(of[bdw],borderwidth=bdw,relief=relief)
        Label(f,width=10,text=relief).pack()
        f.pack(side=LEFT,padx=7-bdw,pady=5+bdw)
    of[bdw].pack()

例3:

from tkinter import *

root=Tk()

f=Frame(root,width=245,height=110)

xf=Frame(f,relief=GROOVE,borderwidth=2)

Label(xf,text="You shot him!").pack(pady=10)
Button(xf,text="He's dead!",state=DISABLED).pack(side=LEFT,padx=5,pady=8)
Button(xf,text="He's completely dead!").pack(side=RIGHT,padx=5,pady=8)
xf.place(relx=0.01,rely=0.125,anchor=NW)
Label(f,text="Self-defence against fruit").place(relx=.06,rely=0.125,anchor=W)
f.pack()
root.mainloop()

3.标签(Label)
Label用于显示文本或图像

from tkinter import *

root=Tk()

f=Frame(root)

Label(f,text="May I be strenuous, energetic and persevering !May I be patient! May I be able to bear and forbear the wrongs of others! May I ever keep a promise given!",wraplength=300,justify=LEFT).pack(pady=10)

Label(f,bitmap="@woman",relief=GROOVE).pack(side=RIGHT,padx=5,pady=5)
f.pack()

4.按钮
按钮是对鼠标和键盘事件起反应的标签

from tkinter import *

class GUI:
    def __init__(self):
        self.root=Tk()
        of=[None]*5
        for bdw in range(5):
            of[bdw]=Frame(self.root,borderwidth=0)
            Label(of[bdw],text="borderwidth=%d"%bdw).pack(side=LEFT)
            for relief in(RAISED,SUNKEN,FLAT,RIDGE,GROOVE,SOLID):
                Button(of[bdw],text=relief,borderwidth=bdw,relief=relief,command=lambda s=self,r=relief,b=bdw:s.prt(r,b)).pack(side=LEFT,padx=7-bdw,pady=5)
            of[bdw].pack()
    def prt(self,relief,border):
            print("%s:%d"%(relief,border))

myGUI=GUI()

5.输入(Entry)
用于收集用户输入

from tkinter import *

root=Tk()
Label(root,text="Anagrom:").pack(side=LEFT)
e=StringVar()
Entry(root,textvariable=e,width=40).pack(side=LEFT)

e.set("A shroe!A shroe!MY dingkom for a shroe!")

6.单选按钮(Radiobutton)

from tkinter import *

root=Tk()
var=IntVar()

for text,value in[('Passion fruit', 1), ('Loganberries', 2), ('Mangoes in syrup', 3),
       ('Oranges', 4), ('Apples', 5), ('Grapefruit', 10)]:
    Radiobutton(root,text=text,value=value,variable=var).pack(anchor=W)

var.set(10)

7.复选按钮(Checkbutton)

from tkinter import *

root = Tk()

'''
var=[0]*6
for i in range(6):
    var[i]=IntVar()
i=0
'''
class Dummy:pass
var=Dummy()

var2=IntVar()
for castmember, row, col, status in [
    ('John Cleese', 0,0,NORMAL), ('Eric Idle', 0,1,NORMAL),
    ('Graham Chapman', 1,0,DISABLED), ('Terry Jones', 1,1,NORMAL),
    ('Michael Palin',2,0,NORMAL), ('Terry Gilliam', 2,1,NORMAL)]:
    '''
    Checkbutton(root, text=castmember, state=status,variable=var[i]).grid(row=row, column=col,sticky=W)
    i+=1
    '''
    setattr(var,castmember,IntVar)
    Checkbutton(root, text=castmember, state=status,variable=getattr(var,castmember)).grid(row=row, column=col,sticky=W)
root.mainloop()

8.菜单(Menu)

from tkinter import *

def makeCascadeMenu():
    casBtn=Menubutton(mBar,text="Sascading Menus",underline=0)
    casBtn.pack(side=LEFT,padx="2m")
    casBtn.menu=Menu(casBtn)
    casBtn.menu.choices=Menu(casBtn.menu)
    casBtn.menu.choices.wieerdones1=Menu(casBtn.menu.choices)

    casBtn.menu.choices.wieerdones1.add_command(label="Stockbroker")
    casBtn.menu.choices.add_command(label="Wooden Leg")

    casBtn.menu.choices.add_cascade(label="Is it a...",menu=casBtn.menu.choices.wieerdones1)
    casBtn.menu.add_cascade(label="Script",menu=casBtn.menu.choices)

    casBtn["menu"]=casBtn.menu

if __name__=="__main__":
    root=Tk()
    mBar=Frame(root,relief=RAISED,borderwidth=2)
    mBar.pack(fill=X)
    makeCascadeMenu()
    root.geometry("400x400")
    root.mainloop()

9.列表框(Listbox)

from tkinter import *

root=Tk()
list=Listbox(root ,width=15)
list.pack()
for item in range(10):
    list.insert(END,item)

10.滚动条(Scrollbar)

from tkinter import *

root=Tk()
list=Listbox(root,height=6,width=15)
scroll=Scrollbar(root,command=list.yview)
list.configure(yscrollcommand=scroll.set)
list.pack(side=LEFT)

scroll.pack(side=RIGHT,fill=Y)

for item in range(30):
    list.insert(END,item)

11.文本(Text)

from tkinter import *

root=Tk()
text=Text(root,width=50,height=26)
scroll=Scrollbar(root,command=text.yview)
text.configure(yscrollcommand=scroll.set)

text.tag_configure("bold_italics",font=("Verdana",12,"bold","italic"))
text.tag_configure("color",foreground="blue",font=("黑体",14))
text.tag_bind("bite","<1>",lambda e, t=text:t.insert(END,"I'll bite your legs off!"))

text.insert(END,"Four hours to bury a cat?\n","bold_italics")
text.insert(END,'Can I call you "Frank "\n',"color")
text.insert(END,'I dare you to click on this "\n',"bite")

button=Button(text,text="I do live at 46 Horton terrace")
text.window_create(END,window=button)

#photo=PhotoImage(file="")
#text.image_create(END,image=photo)

text.pack(side=LEFT)
scroll.pack(side=RIGHT,fill=Y)

 

12.画布

from tkinter import *

root=Tk()
canvas=Canvas(root,width=400,height=400)
canvas.create_oval(10,10,100,100,fill="gray90")
canvas.create_line(105,10,200,105)
canvas.create_rectangle(205,10,300,105,outline="white",fill="gray50")

xy=(10,105,100,200)
canvas.create_arc((10,105,100,200),start=0,extent=270,fill="gray60")
canvas.create_arc(xy,start=270,extent=5,fill="gray70")
canvas.create_arc(xy,start=275,extent=35,fill="gray80")
canvas.create_arc(xy,start=310,extent=49,fill="gray90")

canvas.create_polygon(205,105,285,125,166,177,210,199,205,105,fill="white")

canvas.pack()

John E Grayson Python与Tkinter编程

posted @ 2019-09-04 17:38  vocus  阅读(3552)  评论(0)    收藏  举报