Tkinter模塊("Tk 接口")是Python的標准Tk GUI工具包的接口,位Python的內置模塊,直接import tkinter即可使用。
1.創建窗口
from Tkinter import * #引用Tk模塊 root = Tk() #初始化Tk() root.mainloop() #進入消息循環
幾個常用屬性
- title: 設置窗口標題
- geometry: 設置窗口大小
- resizable():設置窗口是否可以變化長 寬
2.模塊詳解
#!/usr/bin/python # -*- coding: utf-8 -*- from Tkinter import * root = Tk() root.title("test Tkinter") #### Text() ################################################################################## root.geometry('300x100') root.resizable(width=False,height=True) #============================================ # 向空間內輸入文本 Text # # t = Text(根對象) # 插入:t.insert(mark, 內容) # 刪除:t.delete(mark1, mark2) # 其中,mark可以是行號,或者特殊標識 #============================================ t = Text(root) t.insert(1.0, 'hello\n') t.insert(END, 'hello000000\n') t.insert(END, 'nono') t.pack() #### Entry() ################################################################################## root.geometry() #=================================================================== # 創建單行文本框 Entry # # 創建:lb =Entry(根對象, [屬性列表]) # 綁定變量 var=StringVar() lb=Entry(根對象, textvariable = var) # 獲取文本框中的值 var.get() # 設置文本框中的值 var.set(item1) #=================================================================== var = StringVar() e = Entry(root, textvariable = var) var.set("hello") e.pack() #### Listbox() ################################################################################ root.geometry() # ====================================================================== # # 列表控件,可以含有一個或多個文本想,可單選也可多選 Listbox # 創建:lb = ListBox(根對象, [屬性列表]) # 綁定變量 var=StringVar() lb=ListBox(根對象, listvariable = var) # 得到列表中的所有值 var.get() # 設置列表中的所有值 var.set((item1, item2, .....)) # 添加:lb.insert(item) # 刪除:lb.delete(item,...) item為序號 # 綁定事件 lb.bind('<ButtonRelease-1>', 函數) # 獲得所選中的選項 lb.get(lb.curselection()) # selectmode可以為BROWSE MULTIPL SINGLE # ====================================================================== def print_item(event): print lb.get(lb.curselection()) var = StringVar() lb = Listbox(root, listvariable=var) list_item = [1,2,3,4] for item in list_item: lb.insert(END, item) lb.delete(2,4) var.set(('a', 'ab', 'c', 'd')) print var.get() lb.bind('<ButtonRelease-1>', print_item) lb.pack() #### Scrollbar ################################################################################ root.geometry() # ================================================================ # 滾動條控件,當內容超過可視化區域時使用,如列表框。 # # Frame(根對象, [屬性列表]), 最長用的用法是和別的控件一起使用. # ================================================================ def print_item(event): print lb.get(lb.curselection()) var = StringVar() lb = Listbox(root, selectmode=BROWSE, listvariable=var) lb.bind('<ButtonRelease-1>', print_item) list_item = [1,2,3,4,5,6,7,8,9,0] for item in list_item: lb.insert(END, item) scrl = Scrollbar(root) scrl.pack(side=RIGHT, fill=Y) lb.configure(yscrollcommand=scrl.set) lb.pack(side=LEFT, fill=BOTH) scrl['command'] = lb.yview # 進入消息循環 root.mainloop()
3.pack 函數詳解
'''''fill如何控制子組件的布局''' # -*- coding: utf-8 -*- # 不設置root的大小,使用默認 from tkinter import * root = Tk() # 改變root的大小為80x80 root.geometry('80x80+0+0') print(root.pack_slaves()) # 創建三個Label分別使用不同的fill屬性 Label(root, text='pack1', bg='red').pack(fill=Y) Label(root, text='pack2', bg='blue').pack(fill=BOTH) Label(root, text='pack3', bg='green').pack(fill=X) print(root.pack_slaves()) root.mainloop() # 第一個只保證在Y方向填充,第二個保證在XY兩個方向上填充,第三個不使用填充屬性, # 注意Pack只會吝嗇地給出可以容納這三個組件的最小區域,它不允許使用剩余的空間了,故下方留有“空白”。
'''''expand如何控制組件的布局''' # -*- coding: utf-8 -*- # 這個屬性指定如何使用額外的空間,即上例中留下來的“空白” from tkinter import * root = Tk() # 改變root的大小為80x80 root.geometry('80x80+0+0') print(root.pack_slaves()) # 創建三個Label分別使用不同的fill屬性 Label(root, text='pack1', bg='red').pack(fill=Y, expand=1) Label(root, text='pack2', bg='blue').pack(fill=BOTH, expand=1) Label(root, text='pack3', bg='green').pack(fill=X, expand=0) print(root.pack_slaves()) root.mainloop() # 第一個只保證在Y方向填充,第二個保證在XY兩個方向上填充,第三個不使用填充屬性, # 這個例子中第一個Label和第二個Label使用了expand = 1屬性,而第三個使用expand = 0屬性,改變root的大小, # 可以看到Label1和Label2是隨着root的大小變化而變化(嚴格地它的可用空間在變化),第三個只中使用fill進行X方向上的填充,不使用額外的空間。
'''''改變組件的排放位置''' # 使用side屬性改變放置位置 # -*- coding: utf-8 -*- from tkinter import * root = Tk() # 改變root的大小為80x80 root.geometry('80x80+0+0') print(root.pack_slaves()) # 創建三個Label分別使用不同的fill屬性,改為水平放置 # 將第一個Label居左放置 Label(root, text='pack1', bg='red').pack(fill=Y, expand=1, side=LEFT) # 將第二個Label居右放置 Label(root, text='pack2', bg='blue').pack(fill=BOTH, expand=1, side=RIGHT) # 將第三個Label居左放置,靠Label放置,注意它不會放到Label1的左邊 Label(root, text='pack3', bg='green').pack(fill=X, expand=0, side=LEFT) print(root.pack_slaves()) root.mainloop() # 第一個只保證在Y方向填充,第二個保證在XY兩個方向上填充,第三個不使用填充屬性,這個例子中第一個Label和第二個Label使用了expand = 1屬性, # 而第三個使用expand = 0屬性,改變root的大小,可以看到Label1和Label2是隨着root的大小變化而變化(嚴格地它的可用空間在變化), # 第三個只中使用fill進行X方向上的填充,不使用額外的空間。
'''''設置組件之間的間隙大小''' # ipadx設置內部間隙 # padx設置外部間隙 # -*- coding: utf-8 -*- # 不設置root的大小,使用默認 from tkinter import * root = Tk() # 改變root的大小為80x80 # root.geometry('80x80+0+0') print(root.pack_slaves()) # 創建三個Label分別使用不同的fill屬性,改為水平放置 # 將第一個LabelFrame居左放置 L1 = LabelFrame(root, text='pack1', bg='red') # 設置ipadx屬性為20 L1.pack(side=LEFT, ipadx=20) Label(L1, text='inside', bg='blue' ).pack(expand=1, side=LEFT) L2 = Label(root, text='pack2', bg='blue' ).pack(fill=BOTH, expand=1, side=LEFT, padx=10) L3 = Label(root, text='pack3', bg='green' ).pack(fill=X, expand=0, side=LEFT, pady=10) print(root.pack_slaves()) root.mainloop() # 為了演示ipadx/padx,創建了一個LabelFrame設置它的ipadx為20,即內部間隔值為20, # 它的子組件若使用則會留出20個單位;Label2和Label3分別設置x和y方向上的外部間隔值,所有與之排列的組件會與之保留10個單位值的距離
4.PaneWindow 詳解
from Tkinter import * #### PanedWindow ##################################################################### # PanedWindow是一個窗口布局管理的插件,可以包含一個或者多個子控件。 # 用戶可以用鼠標移動上面的分割線來改變每個子控件的大小。 PanedWindow可以用來創建2格或者3格的布局。 m1 = PanedWindow() m1.pack(fill=BOTH, expand=1) left = Label(m1, text="left pane", bg="red") m1.add(left) m2 = PanedWindow(m1, orient=VERTICAL) m1.add(m2) top = Label(m2, text="top pane", bg="blue") m2.add(top) bottom = Label(m2, text="bottom pane", bg="yellow") m2.add(bottom) mainloop()
參考自:
Tkinter 說明:http://www.runoob.com/python/python-gui-tkinter.html
Tkinter 內容詳解:https://www.cnblogs.com/kaituorensheng/p/3287652.html
pack 函數詳解:http://blog.csdn.net/aa1049372051/article/details/51886909
