Python 基於Tkinter的GUI圖形介面學習
阿新 • • 發佈:2019-01-02
用Tkinter畫圖首先要建立一個根掛件,可以用Tk()產生
然後要產生不同型別的小掛件,有標籤Label,按鈕Button,介面Entry 之類的,具體的檢視網上的詳細文件。
比如說用widget = Label(None, text = 'hello')產生了一個內容為hello的標籤之後還要確定把這個標籤放在哪個位置,所以就要用到pack()
pack有很多選項,比如說要不要隨著螢幕縮放expand,要不要填充fill,還可以設定位置anchor。
pack完了之後要維持這個視窗的執行,root.mainloop()或者是widget.mainloop()都行。
這樣產生的視窗只能有一個掛件,如果要把多個掛件放在一個視窗就要用到容器Frame()
win = Frame(root) 產生一個叫作win的容器,下面要產生的掛件都是放在這個容器裡面的。
widget1 = Label(win, text = 'widget1')
widget1.pack(side = TOP)
widget2 = Button(win, text = 'button')
widget2.pack(side = TOP)
就能產生類似下圖的效果:
如果要分割槽,即安排視窗的佈局的話還可以用grid()設定行列的位置。
元件名 | 說明 | 用法 | 屬性(引數) |
---|---|---|---|
label | 標籤 | Label(根物件, [屬性列表]) | text:要現實的文字bg:背景顏色font:字型(顏色, 大小)width:控制元件寬度height:控制元件高度 |
Frame | 在螢幕上建立一塊矩形區域,多作為容器來佈局窗體 | Frame(根物件, [屬性列表]) | 同上 |
Entry | 建立單行文字框 |
建立:lb =Entry(根物件,[屬性列表]) 繫結變數: var=StringVar() lb=Entry(根物件, textvariable = var) 獲取文字框中的值:var.get()設定文字框中的值 var.set(item1) |
同上 |
Text | 向該空間內輸入文字 |
t = Text(根物件)插入:t.insert(mark, 內容)刪除:t.delete(mark1, mark2)其中,mark可以是行號,或者特殊標識,例如•INSERT:游標的插入點CURRENT:滑鼠的當前位置所對應的字元位置 •END:這個Textbuffer的最後一個字元 •SEL_FIRST:選中文字域的第一個字元,如果沒有選中區域則會引發異常 •SEL_LAST:選中文字域的最後一個字元,如果沒有選中區域則會引發 異常 | 同上 |
Button | 建立按鈕 | Button(根物件, [屬性列表]) | 同上 |
Listbox | 列表控制元件,可以含有一個或多個文字想,可單選也可多選 |
• 建立:lb = ListBox(根物件, [屬性列表]) • 繫結變數 var=StringVar() lb=ListBox(根物件, listvariable = var)比如 • 得到列表中的所有值 var.get() • 設定列表中的所有值 var.set((item1, item2, …..)) • 新增:lb.insert(item) • 刪除:lb.delete(item,…) • 繫結事件 lb.bind(‘<\ButtonRelease-1>’, 函式) • 獲得所選中的選項 lbl.get(lb.curselection()) |
同上 |
Scrollbar | 在螢幕上建立一塊矩形區域,多作為容器來佈局窗體 | Frame(根物件, [屬性列表]), 最長用的用法是和別的控制元件一起使用. | 同上 |
… … … |
… … … |
…. …. …. |
….. ….. ….. |
常用顏色名稱
19.3Tkinter舉例
標籤元件
- #-*-coding: utf-8-*-
- import Tkinter
- top = Tkinter.Tk() # 建立頂層視窗
- label = Tkinter.Label(top, text="Hello World!") # 建立標籤元件
- label.pack() # packer管理和顯示元件
- Tkinter.mainloop() # 進入主事件迴圈
按鈕元件
- #-*-coding: utf-8-*-
- import Tkinter
- top = Tkinter.Tk()
- quit = Tkinter.Button(top, text="Hello World!", command=top.quit) # 建立按鈕元件。第三個引數就是回撥函式物件,按下按鈕後,執行回撥函式。
- quit.pack()
- Tkinter.mainloop()
標籤和按鈕元件
- #-*-coding: utf-8-*-
- import Tkinter
- top = Tkinter.Tk()
- hello = Tkinter.Label(top, text="Hello World!")
- hello.pack()
- quit = Tkinter.Button(top, text="QUIT", command=top.quit, bg="red", fg="white") # bg引數表示按鈕元件用紅色填充,而fg引數表示其中的文字用白色顯示
- quit.pack(fill=Tkinter.X, expand=1) # fill引數是讓餘空間QUIT按鈕充滿水平方向的剩,expand引數引導packer填充了水平方向的所有可視空間,並將按鈕延伸到視窗的左右邊界。
- Tkinter.mainloop()
- #-*-coding: utf-8-*-
- # 這一例子中增加了進度條元件,進度條與標籤進行互動
- from Tkinter import *
- def resize(ev=None): # 回撥函式,在滑塊移動時,啟用執行
- label.config(font='Helvetica -%d bold' % scale.get())
- top = Tk()
- top.geometry('250x150') # 限定頂層視窗尺寸為250x150。
- label = Label(top, text='Hello World!', font='Helvetica -12 bold')
- ###Label是偏函式,'Helvetica -12 bold'與偏函式處的變數一樣
- label.pack(fill=Y, expand=1) # 在豎直方向充滿剩餘空間,
- scale = Scale(top, from_=10, to=40, orient=HORIZONTAL, command=resize) # from_和to兩個引數表示進度條範圍,orient引數表示進度條的取向,水平放置還是豎直放置,command就是回撥函式
- scale.set(12) # 設定進度條初始值在12
- scale.pack(fill=X, expand=1) # 在水平方向充滿剩餘空間
- quit = Button(top, text='QUIT', command=top.quit, activeforeground='white', activebackground='red') # activeforeground引數表示按鈕被按下時,文字的顏色為白色;activebackground引數表示按鈕被按下時,按鈕背景色變為紅色。
- quit.pack()
- mainloop()
偏函式應用舉例
- #-*-coding: utf-8-*-
- from functools import partial as pto
- from Tkinter import Tk, Button, X
- from tkMessageBox import showinfo, showwarning, showerror # 這是什麼模組?
- WARN = 'warn'
- CRIT = 'crit'
- REGU = 'regu'
- SIGNS = {
- 'do not enter': CRIT,
- 'railroad crossing': WARN,
- '55\nspeed limit': REGU,
- 'wrong way': CRIT,
- 'merging traffic': WARN,
- 'one way': REGU,
- }
- # 定義危急、警告和通知三個回撥函式物件
- critCB = lambda: showerror('Error', 'Error Button Pressed!')
- warnCB = lambda: showwarning('Warning', 'Warning Button Pressed!')
- infoCB = lambda: showinfo('Info', 'Info Button Pressed!')
- top = Tk()
- top.title('Road Signs')
- Button(top, text='QUIT', command=top.quit, bg='red', fg='white').pack()
- # 偏函式應用,書上說是“建立按鈕類”,這樣text就作為引數傳入,從而例項化相應的按鈕
- MyButton = pto(Button, top) # 危急、警告和通知三個回撥函式的共同引數?書上是“模板化的按鈕類及根視窗top”
- # 書上說是再次模板化
- CritButton = pto(MyButton, command=critCB, bg='white', fg='red') # 相當於CritButton = Button(top, command=critCB, bg='white', fg='red')?但是缺少text,應該理解為一個函式物件,以text為引數。
- WarnButton = pto(MyButton, command=warnCB, bg='goldenrod1')
- ReguButton = pto(MyButton, command=infoCB, bg='white')
- for eachSign in SIGNS:
- signType = SIGNS[eachSign]
- cmd = '%sButton(text=%r%s).pack(fill=X, expand=True)' % (signType.title(), eachSign, '.upper()'if signType == CRIT else'.title()') # python求值字串
- eval(cmd)
- top.mainloop()
#!/usr/bin/python
#-*- coding:utf-8 –*-
from Tkinter import *
win = Tk()
#win.title('google search engine optimal')
win.title('一個測試程式')
# the top level of the window
def report(text):
#print(v.get())
entry2 = Label(root, text = v.get())
entry2.grid(row = 1, column = 1)
root = Frame(win)
root.pack()
label1 = Label(root, text = 'd 取值手動滑動條', width = 25)
label1.pack(side = LEFT)
label1.grid(row = 0, column = 0)
v = StringVar()
scl = Scale(root, from_ = 0, to = 1, tickinterval = 0.5, resolution = 0.01, orient = 'horizontal', variable=v, sliderlength = 10, width = 40, length = 200, command=report)
scl.grid(row = 0, column = 1)
print (scl.get())
label2 = Label(root, text = 'd當前取值:', height = 10)
label2.grid(row = 1, column = 0)
label3 = Label(root, text = 'PR=d*GPR+10*(1-d)*FPR :d越大則排序考慮人氣越多。', height = 10)
label3.grid(row = 2)
root.mainloop()