可以編寫(xiě)出帶頁(yè)面的程序,不過(guò)由于界面不夠美化一般都是安裝程序中用到這些,僅僅對(duì)每個(gè)控件進(jìn)行代碼演示。 Tkinter主體:import tkinter # 創(chuàng)建主窗口? win = tkinter.Tk() # 設(shè)置窗口標(biāo)題? win.title("sunck") # 設(shè)置窗口大小與位置? win.geometry("400x400+200+0") # 進(jìn)入消息循環(huán) # ??程序運(yùn)行起來(lái)? win.mainloop() label控件import tkinter win = tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0") # win表示主窗體? # text:文本內(nèi)容? # bg:背景顏色? # fg:字體顏色 # font:字體型號(hào)與大小? # anchor:位置 N NE E SE S SW W NW CENTER? # width:空間寬度? # wraplength:指定text中文本多少寬度后開(kāi)始換行 # justify:text中多行文本的對(duì)齊方式 l = tkinter.Label(win, text="sunckisagoodman", bg="pink", fg="red", font=("Arial",12), anchor=tkinter.SW, width=10, height=3, wraplength = 30, justify="right") # 顯示控件? l.pack() win.mainloop() button控件import tkinter def func(): print("sunck is a good man") win = tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0") # 創(chuàng)建按鈕? # 顯示窗口? # 文本? # 點(diǎn)擊按鈕響應(yīng)的方法? button=tkinter.Button(win, text="按鈕", width=10, height=10, command=func) button.pack() button1=tkinter.Button(win, text="按鈕1", command=lambda :print("sunck is a nice man")) button1.pack() win.mainloop() entry控件import tkinter win = tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0") # 綁定變量? var = tkinter.Variable() e = tkinter.Entry(win, textvariable=var, show="*") e.pack() # 設(shè)置文本框中的值? var.set("sunck is a good man") # 獲取文本框中的值? print(var.get()) print(e.get()) win.mainloop()
5、點(diǎn)擊button將entry上的內(nèi)容打印出來(lái)import tkinter def func(): print(ve.get()) win = tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0") ve = tkinter.Variable() e = tkinter.Entry(win, textvariable=ve) e.pack() button=tkinter.Button(win, text="顯示", command=func) button.pack() win.mainloop() 6、text控件import tkinter win = tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0") #height:控制顯示行數(shù) t = tkinter.Text(win, width=30, height=4) t.pack() # t.pack(side=tkinter.LEFT, fill=tkinter.Y) #滾動(dòng)條寫(xiě)法 info = """HAMLET: To be, or not to be--that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune Or to take arms against a sea of troubles And by opposing end them. To die, to sleep-- No more--and by a sleep to say we end The heartache, and the thousand natural shocks That flesh is heir to. 'Tis a consummation Devoutly to be wished.""" #插入 t.insert(tkinter.INSERT, info) win.mainloop() 7、帶滾動(dòng)條的textimport tkinter win = tkinter.Tk() win.title("sunck") # win.geometry("400x400+200+0") s = tkinter.Scrollbar() s.pack(side=tkinter.RIGHT, fill=tkinter.Y) t = tkinter.Text(win, width=30, height=4) t.pack(side=tkinter.LEFT, fill=tkinter.Y) s.config(command=t.yview) t.config(yscrollcommand=s.set) info = """HAMLET: To be, or not to be--that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune Or to take arms against a sea of troubles And by opposing end them. To die, to sleep-- No more--and by a sleep to say we end The heartache, and the thousand natural shocks That flesh is heir to. 'Tis a consummation Devoutly to be wished.""" t.insert(tkinter.INSERT, info) win.mainloop() 8、CheckButton多選框控件from tkinter import * def updata(): message = "" if hobby1.get() == 1: message += "Your Hobby is 1\n" if hobby2.get() == 1: message += "Your Hobby is 2\n" if hobby3.get() == 1: message += "Your Hobby is 3\n" txt.delete(0.0, END)#全部刪除txt內(nèi)容 txt.insert(0.0, message) win = Tk() win.title("sunck") label=Label(win,text = "Please check") label.pack() hobby1 = BooleanVar() checkb1 = Checkbutton(win,text = "hobby1",variable = hobby1,command = updata) checkb1.pack() hobby2 = BooleanVar() checkb2 = Checkbutton(win,text = "hobby2",variable = hobby2,command = updata) checkb2.pack()
hobby3 = BooleanVar() checkb3 = Checkbutton(win,text = "hobby3",variable = hobby3,command = updata) checkb3.pack() txt = Text(win,width = 50,height = 5,wrap = WORD) txt.pack() win.mainloop() 9、RadioButton單選框控件from tkinter import * def updata(): print(v.get()) win = Tk() win.title("sunck") win.geometry("400x400+200+0") v = IntVar() Radiobutton(win, text="One", variable=v, value=1, command=updata).pack() Radiobutton(win, text="Two", variable=v, value=2, command=updata).pack() mainloop() 10、Listbox控件基本使用from tkinter import * win = Tk() win.title("sunck") # win.geometry("400x400+200+0") # 1、創(chuàng)建一個(gè)Listbox,向其中添加三個(gè)item # lb = Listbox(win, selectmode = BROWSE) # for item in ['good', 'nice', 'handsom', "grace", "noble", "sense"]: # #按順序向后添加 # lb.insert(END, item) # lb.pack() # 在開(kāi)始添加 # lb.insert(ACTIVE, "cool") # 只添加一項(xiàng)將[]作為一個(gè)item # lb.insert(END,["1","2","3"]) # 刪除 第一個(gè)為開(kāi)始的索引值;第二個(gè)為結(jié)束的索引值,如果不指定則只刪除第一個(gè)索引項(xiàng) # lb.delete(1,3) # lb.delete(1) # 選中函數(shù),selection_set函數(shù)有兩個(gè)參數(shù)第一個(gè)為開(kāi)始的索引;第二個(gè)為結(jié)束的索引,如果不指定則只選中第一個(gè)參數(shù)指定的索引項(xiàng) # lb.select_set(2, 4) # 取消選中 # lb.select_clear(2,4) # 得到當(dāng)前Listbox中的item個(gè)數(shù) # print(lb.size()) # 返回指定索引的項(xiàng),參數(shù)第一個(gè)為開(kāi)始的索引;第二個(gè)為結(jié)束的索引,如果不指定則只選中第一個(gè)參數(shù)指定的索引項(xiàng) # print(lb.get(1, 3)) # print(lb.get(1)) # 返回當(dāng)前返回的項(xiàng)的索引,不是item的值 # print(lb.curselection()) # 判斷 一個(gè)項(xiàng)是否被選中,使用索引 # print(lb.selection_includes(2)) # print(lb.selection_includes(5)) # 2、與BROWSE相似 的為SINGLE,但不支持鼠標(biāo)移動(dòng)選中位置 # v = StringVar() # lb = Listbox(win, selectmode = SINGLE, listvariable=v) # for item in ['good', 'nice', 'handsom']: # lb.insert(END, item) # lb.pack() # 打印當(dāng)前列表中的項(xiàng)值 # print(v.get()) # 改變v的值,使用tuple可以與item對(duì)應(yīng) # v.set(('1000','200')) # 事件綁定 # def printList(event): # print(lb.get(lb.curselection())) # lb.bind('<Double-Button-1>',printList) # 3、使用selectmode = EXPANDED使用Listbox來(lái)支持Shift和Control # lb = Listbox(win, selectmode = EXTENDED) # for item in ['good', 'nice', 'handsom', "grace", "noble", "sense", 1, 2, 3, 4, 5, 6]: # lb.insert(END, item) # lb.pack() # # 運(yùn)行程序,點(diǎn)中“python",shift + 點(diǎn)擊"widget",會(huì)選中所有的item # # 運(yùn)行程序,點(diǎn)中"python",control + 點(diǎn)擊"widget",會(huì)選中python和widget,第二項(xiàng)tkinter處于非選中狀態(tài) # # 滾動(dòng)條控件 # # 當(dāng)內(nèi)容超過(guò)可視化區(qū)域時(shí)使用,如列表框 # scrl = Scrollbar(win) # scrl.pack(side=RIGHT,fill=Y) # # 指定Listbox的yscrollbar的回調(diào)函數(shù)為Scrollbar的set,表示滾動(dòng)條在窗口變化時(shí)實(shí)時(shí)更新 # lb.configure(yscrollcommand=scrl.set) # lb.pack(side=LEFT,fill=BOTH) # # # 指定Scrollbar的command的回調(diào)函數(shù)是Listbar的yview # scrl['command'] = lb.yview # 4、創(chuàng)建一個(gè)可以多選的Listbox,使用屬性selectmaod lb = Listbox(win, selectmode = MULTIPLE, ) for item in ['good', 'nice', 'handsom']: lb.insert(END, item) lb.pack() win.mainloop() 11、Scale控件# Tkinter 中的 Scale 控件是一種可供用戶通過(guò)拖動(dòng)指示器改變變量值的控件. 這種控件可以水平放置, 也可以豎直放置 from tkinter import * win = Tk() win.title("sunck") win.geometry("400x400+200+0") # 豎 # from起始值 # to結(jié)束值 # tickinterval選擇值將會(huì)變?yōu)樵摂?shù)值的倍數(shù) # length 豎直時(shí)為高度,水平是為長(zhǎng)度 scale1 = Scale(win, from_=0, to=40, tickinterval=8, length=300) scale1.pack() #默認(rèn)值為起始值,可以通過(guò)set賦值 scale1.set(20) # 獲取值 # def show(): # print(scale1.get()) # Button(win, text="show", command=show).pack() #橫 orient=HORIZONTAL # scale2 = Scale(win, from_=0, to=200, orient=HORIZONTAL) # scale2.pack() win.mainloop() 12、Spinbox控件from tkinter import * win = Tk() win.title("sunck") win.geometry("400x400+200+0") def change(): print(v.get()) v = StringVar() # from_:最小值 # to:最大值 # increment:步長(zhǎng) # values:每次更新值將使用values指定的值,不要和from_與to同時(shí)使用values=(0,2,4,6,8) sp = Spinbox(win, from_=0, to=100, increment=1, textvariable=v, command=change) sp.pack() #設(shè)置值 v.set(20) #取值 print(v.get()) win.mainloop() 13、Menu頂層菜單from tkinter import * def callback(): print("sunck is a good man") win = Tk() win.title("sunck") win.geometry("400x400+200+0") menubar = Menu(win) win.config(menu=menubar)
filemenu = Menu(menubar, tearoff=False) for item in ['Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript', "退出"]: if item == "退出": filemenu.add_separator() filemenu.add_command(label=item, command=win.quit) else: filemenu.add_command(label=item, command=callback) filemenu2 = Menu(menubar, tearoff=False) #accelerator快捷鍵 filemenu2.add_command(label="顏色設(shè)置", command=callback, accelerator='Ctrl+N') filemenu2.add_command(label="字體設(shè)置") menubar.add_cascade(label="語(yǔ)言", menu=filemenu) menubar.add_cascade(label="設(shè)置", menu=filemenu2) win.mainloop() 14、Menu右鍵菜單from tkinter import * win = Tk() win.title("sunck") win.geometry("400x400+200+0") menubar = Menu(win) def callback(): print('sunck is a good man') filemenu = Menu(menubar, tearoff=0) for k in ['Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript']: filemenu.add_command(label=k, command=callback) filemenu.add_separator() menubar.add_cascade(label='語(yǔ)言', menu=filemenu) def popup(event): menubar.post(event.x_root, event.y_root) win.bind('<Button-3>', popup) win.mainloop() 15、Frame控件import tkinter win = tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0") tkinter.Label(win, text="frame", bg="red", font=("Arial",15)).pack() frm = tkinter.Frame(win) frm.pack() # #left frm_L = tkinter.Frame(frm) tkinter.Label(frm_L, text="左上", bg="pink", font=("Arial",12)).pack(side=tkinter.TOP) tkinter.Label(frm_L, text="左下", bg="green", font=("Arial",12)).pack(side=tkinter.TOP) frm_L.pack(side=tkinter.LEFT) #right frm_R = tkinter.Frame(frm) tkinter.Label(frm_R, text="右上", bg="yellow", font=("Arial",12)).pack(side=tkinter.TOP) tkinter.Label(frm_R, text="右下", bg="purple", font=("Arial",12)).pack(side=tkinter.TOP) frm_R.pack(side=tkinter.RIGHT) win.mainloop() 16、Combobox下拉控件import tkinter from tkinter import ttk def func(*args): # 當(dāng)前選中的值 print(comboxlist.get()) win = tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0") var = tkinter.StringVar() comboxlist = ttk.Combobox(win, textvariable = var) # 設(shè)置下拉數(shù)據(jù) comboxlist["value"] = ("1","2","3","4","5") # 默認(rèn)現(xiàn)實(shí)的值的下標(biāo) comboxlist.current(0) # 綁定事件 comboxlist.bind("<<ComboboxSelected>>", func) comboxlist.pack() win.mainloop() 17、表格數(shù)據(jù)import tkinter from tkinter import ttk win=tkinter.Tk() win.title("sunck") win.geometry("800x400+200+0") #表格 tree=ttk.Treeview(win) tree.pack() #表示列,不顯示 tree["columns"]=("name","age","height") tree.column("name",width=100) tree.column("age",width=100) tree.column("height",width=100) #顯示表頭 tree.heading("name",text="姓名") tree.heading("age",text="年齡") tree.heading("height",text="身高") # #插入行 tree.insert("",0,text="line1" ,values=("1","2","3")) tree.insert("",1,text="line2" ,values=("1","2","3")) tree.insert("",2,text="line3" ,values=("1","2","3")) tree.insert("",3,text="line4" ,values=("1","2","3")) win.mainloop() 18、樹(shù)狀數(shù)據(jù)import tkinter from tkinter import ttk win=tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0") tree=ttk.Treeview(win) tree.pack() #一級(jí)樹(shù)枝 treeF1=tree.insert("",0,"中國(guó)",text="中國(guó)CHI",values=("F1")) treeF2=tree.insert("",1,"美國(guó)",text="美國(guó)USA",values=("F2")) treeF3=tree.insert("",2,"英國(guó)",text="英國(guó)ENG",values=("F3")) #二級(jí)樹(shù)枝 threeF1_1=tree.insert(treeF1,0,"黑龍江",text="中國(guó)黑龍江",values=("F1_1")) threeF1_2=tree.insert(treeF1,1,"吉林",text="中國(guó)吉林",values=("F1_2")) threeF1_3=tree.insert(treeF1,2,"遼寧",text="中國(guó)遼寧",values=("F1_3")) threeF1_4=tree.insert(treeF1,3,"北京",text="中國(guó)北京",values=("F1_4")) threeF2_1=tree.insert(treeF2,0,"黑龍江1",text="美國(guó)黑龍江",values=("F2_1")) threeF2_2=tree.insert(treeF2,1,"吉林1",text="美國(guó)吉林",values=("F2_2")) threeF2_3=tree.insert(treeF2,2,"遼寧1",text="美國(guó)遼寧",values=("F2_3")) threeF2_4=tree.insert(treeF2,3,"北京1",text="美國(guó)北京",values=("F2_4")) threeF3_1=tree.insert(treeF3,0,"黑龍江2",text="英國(guó)黑龍江",values=("F3_1")) threeF3_2=tree.insert(treeF3,1,"吉林2",text="英國(guó)吉林",values=("F3_2")) threeF3_3=tree.insert(treeF3,2,"遼寧2",text="英國(guó)遼寧",values=("F3_3")) threeF3_4=tree.insert(treeF3,3,"北京2",text="英國(guó)北京",values=("F3_4")) #三級(jí)樹(shù)枝 threeF1_1_1=tree.insert(threeF1_1,0,"哈爾濱",text="黑龍江哈爾濱",values=("F1_1_1_1")) threeF1_1_2=tree.insert(threeF1_1,1,"五常",text="黑龍江五常",values=("F1_1_1_2")) threeF1_1_3=tree.insert(threeF1_1,2,"山河",text="黑龍江山河",values=("F1_1_1_3")) win.mainloop() 19、絕對(duì)布局import tkinter win=tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0") label1=tkinter.Label(win,text="good",bg="blue") label2=tkinter.Label(win,text="nice",bg="yellow") label3=tkinter.Label(win,text="handsome",bg="red") # 絕對(duì)布局,窗口變化不影響 label1.place(x=10, y=10) label2.place(x=50, y=50) label3.place(x=100, y=100) # label1.place(x=10,y=10,anchor=tkinter.NW) # #輸入N,距離N10,W靠近,輸入W,距離W10 ,N靠近 # #輸入NW,N,W都距離10 # label2.place(x=10,y=10,anchor=tkinter.NW) # label3.place(x=10,y=300,anchor=tkinter.W) win.mainloop() 20、相對(duì)布局import tkinter win=tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0") label1=tkinter.Label(win,text="good",bg="blue") label2=tkinter.Label(win,text="nice",bg="yellow") label3=tkinter.Label(win,text="handsome",bg="red") # fill窗口變化Y方向同步變化 X Y BOTH label1.pack(fill=tkinter.Y, side=tkinter.LEFT) # side:貼邊 TOP BOTTOM CENTER LEFT RIGHT label2.pack(fill=tkinter.X, side=tkinter.TOP) label3.pack() win.mainloop() 21、表格布局import tkinter win=tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0") label1=tkinter.Label(win,text="good",bg="blue") label2=tkinter.Label(win,text="nice",bg="yellow") label3=tkinter.Label(win,text="handsom",bg="red") label4=tkinter.Label(win,text="nobo",bg="green") #表格 label1.grid(row=0,column=0) label2.grid(row=0,column=1) label3.grid(row=1,column=0) label4.grid(row=1,column=1) win.mainloop() 22、鼠標(biāo)點(diǎn)擊事件from tkinter import * root = Tk() def printCoords(event): print(event.x,event.y) bt1 = Button(root,text = 'leftmost button') bt1.bind('<Button-1>',printCoords) bt2 = Button(root,text = 'middle button') bt2.bind('<Button-2>',printCoords) bt3 = Button(root,text = 'rightmost button') bt3.bind('<Button-3>',printCoords) bt4 = Button(root,text = 'double click') bt4.bind('<Double-Button-1>',printCoords) bt5 = Button(root, text = 'triple click') bt5.bind('<Triple-Button-1>',printCoords) bt1.pack() bt2.pack() bt3.pack() bt4.pack() bt5.pack() root.mainloop() 23、鼠標(biāo)移動(dòng)事件#測(cè)試鼠標(biāo)的移動(dòng)事件 #<Bx-Motion>鼠標(biāo)移動(dòng)事件,x=[1,2,3]分別表示左中右鼠標(biāo)操作 from tkinter import *
win = Tk() def printCoords(event): print(event.x,event.y) bt1 = Button(win,text = 'leftmost button') bt1.bind('<B1-Motion>',printCoords) bt2 = Button(win,text = 'middle button') bt2.bind('<B2-Motion>',printCoords) bt3 = Button(win,text = 'rightmost button') bt3.bind('<B3-Motion>',printCoords) bt1.grid() bt2.grid() bt3.grid() win.bind('<B1-Motion>',printCoords) win.mainloop() 24、鼠標(biāo)釋放事件from tkinter import * root = Tk() def printCoords(event): print(event.x,event.y) bt1 = Button(root,text = 'leftmost button') bt1.bind('<ButtonRelease-1>',printCoords) bt2 = Button(root,text = 'middle button') bt2.bind('<ButtonRelease-2>',printCoords) bt3 = Button(root,text = 'rightmost button') bt3.bind('<ButtonRelease-3>',printCoords) bt1.grid() bt2.grid() bt3.grid() root.mainloop() 25、進(jìn)入事件from tkinter import * root = Tk() def printCoords(event): print(event.x,event.y) bt1 = Button(root,text = 'leftmost button') bt1.bind('<Enter>',printCoords) bt1.grid() root.mainloop() 26、離開(kāi)事件from tkinter import * root = Tk() def printCoords(event): print(event.x,event.y) bt1 = Button(root,text = 'leftmost button') bt1.bind('<Leave>',printCoords) bt1.grid() root.mainloop() 27、響應(yīng)特殊按鍵事件from tkinter import * root = Tk() def printCoords(event): print('event.char = ',event.char) print('event.keycode = ',event.keycode) bt1 = Button(root,text = 'Press BackSpace') bt1.bind('<BackSpace>',printCoords) bt2 = Button(root,text = 'Press Enter') bt2.bind('<Return>',printCoords) bt3 = Button(root,text = 'F5') bt3.bind('<F5>',printCoords) bt4 = Button(root,text = 'Left Shift') bt4.bind('<Shift_L>',printCoords) bt5 = Button(root,text = 'Right Shift') bt5.bind('<Shift_R>',printCoords) bt1.focus_set() bt1.grid() bt2.grid() bt3.grid() bt4.grid() bt5.grid() root.mainloop() 28、響應(yīng)所有時(shí)間按鍵事件from tkinter import * root = Tk() def printCoords(event): print('event.char = ',event.char) print('event.keycode = ',event.keycode) bt1 = Button(root,text = 'Press BackSpace') bt1.bind('<Key>',printCoords) bt1.focus_set() bt1.grid() root.mainloop() 29、指定按鍵事件from tkinter import * root = Tk() def printCoords(event): print('event.char = ',event.char) print('event.keycode = ',event.keycode) bt1 = Button(root,text = 'Press BackSpace') bt1.bind('a',printCoords) bt1.focus_set() bt1.grid() root.mainloop() 30、組合按鍵事件from tkinter import * root = Tk() def printCoords(event): print('event.char = ',event.char) print('event.keycode = ',event.keycode) bt1 = Button(root,text = 'Press Shift - Up') bt1.bind('<Shift-Up>',printCoords)
bt2 = Button(root,text = 'Control-Alt-a') bt2.bind('<Control-Alt-a>',printCoords) bt3 = Button(root,text = 'Control-Alt') bt3.bind('<Control-Alt>',printCoords) bt1.focus_set() bt1.grid() bt2.grid() root.mainloop() |
|
來(lái)自: 昵稱61973759 > 《待分類(lèi)》