蘿卜頭社區(qū) http://www.luobotou.net 同學(xué)問,python有沒有圖形窗口?
肯定是有的,今天我們來了解下tkinter:Tkinter =tk+inter(接口)
Tkinter 是 Python 的標(biāo)準(zhǔn) GUI 庫。Python 使用 Tkinter 可以快速的創(chuàng)建 GUI 應(yīng)用程序。
由于 Tkinter 是內(nèi)置到 python 的安裝包中、只要安裝好 Python 之后就能 import Tkinter 庫、而且 IDLE 也是用 Tkinter 編寫而成、對于簡單的圖形界面 Tkinter 還是能應(yīng)付自如。
注意:Python3.x 版本使用的庫名為 tkinter,即首寫字母 T 為小寫:tkinter。
import tkinter
一、tkinter模塊常用參數(shù)(python3)
1、使用tkinter.Tk() 生成主窗口(root=tkinter.Tk())
?5、使用tkinter.Button時控制按鈕的參數(shù)
6、文本框tkinter.Entry,tkinter.Text控制參數(shù)
7、標(biāo)簽tkinter.Label組件控制參數(shù)
8、單選框和復(fù)選框Radiobutton,Checkbutton控制參數(shù)
1、使用tkinter.Tk() 生成主窗口(root=tkinter.Tk())
root.title('標(biāo)題名')??? 修改框體的名字,也可在創(chuàng)建時使用className參數(shù)來命名;
root.resizable(0,0)??? 框體大小可調(diào)性,分別表示x,y方向的可變性;
root.geometry('250x150') 指定主框體大??;
root.quit()??????? ?退出;
root.update_idletasks()
root.update()??????? 刷新頁面;
2、初級樣例
import tkinter
root=tkinter.Tk() #生成root主窗口
label=tkinter.Label(root,text='Hello,GUI') #生成標(biāo)簽
label.pack()? ? ? ? #將標(biāo)簽添加到主窗口
button1=tkinter.Button(root,text='Button1') #生成button1
button1.pack(side=tkinter.LEFT)? ? ? ? #將button1添加到root主窗口
button2=tkinter.Button(root,text='Button2')
button2.pack(side=tkinter.RIGHT)
root.mainloop()? ? ? ? ? ? #進入消息循環(huán)(必需組件)
3、tkinter中的15種核心組件
? ? Button? ? ? ? 按鈕;
? ? Canvas? ? ? ? 繪圖形組件,可以在其中繪制圖形;
? ? Checkbutton? ? ? 復(fù)選框;
? ? Entry? ? ? ? 文本框(單行);
? ? Text? ? ? ? ? ? 文本框(多行);
? ? Frame? ? ? ? 框架,將幾個組件組成一組
? ? Label? ? ? ? 標(biāo)簽,可以顯示文字或圖片;
? ? Listbox? ? ? 列表框;
? ? Menu? ? ? ? 菜單;
? ? Menubutton? ? ? 它的功能完全可以使用Menu替代;
? ? Message? ? ? ? ? 與Label組件類似,但是可以根據(jù)自身大小將文本換行;
? ? Radiobutton? ? ? 單選框;
? ? Scale? ? ? ? 滑塊;允許通過滑塊來設(shè)置一數(shù)字值
? ? Scrollbar? ? ? ? 滾動條;配合使用canvas, entry, listbox, and text窗口部件的標(biāo)準(zhǔn)滾動條;
? ? Toplevel? ? ? ? 用來創(chuàng)建子窗口窗口組件。
(在Tkinter中窗口部件類沒有分級;所有的窗口部件類在樹中都是兄弟。)
4、組件的放置和排版(pack,grid,place)
pack組件設(shè)置位置屬性參數(shù):
? ? after:? ? 將組件置于其他組件之后;
? ? before:? ? 將組件置于其他組件之前;
? ? anchor:? ? ? 組件的對齊方式,頂對齊'n',底對齊's',左'w',右'e'? ? side:? ? 組件在主窗口的位置,可以為'top','bottom','left','right'(使用時tkinter.TOP,tkinter.E);
? ? fill? ? ? ? ? ? 填充方式 (Y,垂直,X,水平)
? ? expand? ? ? ? ? 1可擴展,0不可擴展
grid組件使用行列的方法放置組件的位置,參數(shù)有:
? ? column:? ? ? ? 組件所在的列起始位置;
? ? columnspam:? ? 組件的列寬;
? ? row:? ? ? 組件所在的行起始位置;
? ? rowspam:? ? 組件的行寬;
place組件可以直接使用坐標(biāo)來放置組件,參數(shù)有:
? ? anchor:? ? 組件對齊方式;
? ? x:? ? ? ? 組件左上角的x坐標(biāo);
? ? y:? ? ? ? ? 組件右上角的y坐標(biāo);
? ? relx:? ? ? ? 組件相對于窗口的x坐標(biāo),應(yīng)為0-1之間的小數(shù);
? ? rely:? ? ? ? ? 組件相對于窗口的y坐標(biāo),應(yīng)為0-1之間的小數(shù);
? ? width:? ? ? ? ? 組件的寬度;
? ? heitht:? ? ? 組件的高度;
? ? relwidth:? ? ? 組件相對于窗口的寬度,0-1;
? ? relheight: ? ? 組件相對于窗口的高度,0-1;
?5、使用tkinter.Button時控制按鈕的參數(shù)
? ? anchor:? ? ? ? 指定按鈕上文本的位置;
? ? background(bg)? ? ? 指定按鈕的背景色;
? ? bitmap:? ? ? 指定按鈕上顯示的位圖;
? ? borderwidth(bd) 指定按鈕邊框的寬度;
? ? command:? ? 指定按鈕消息的回調(diào)函數(shù);
? ? cursor:? ? ? ? 指定鼠標(biāo)移動到按鈕上的指針樣式;
? ? font:? ? ? ? ? ? 指定按鈕上文本的字體;
? ? foreground(fg) 指定按鈕的前景色;
? ? height:? ? ? ? 指定按鈕的高度;
? ? image:? ? ? ? 指定按鈕上顯示的圖片;
? ? state:? ? ? ? ? 指定按鈕的狀態(tài)(disabled);
? ? text:? ? ? ? ? 指定按鈕上顯示的文本;
? ? width:? ? ? ? 指定按鈕的寬度
? ? padx? ? ? ? ? 設(shè)置文本與按鈕邊框x的距離,還有pady;
? ? activeforeground 按下時前景色
? ? textvariable? ? ? 可變文本,與StringVar等配合著用
6、文本框tkinter.Entry,tkinter.Text控制參數(shù)
? ? background(bg)? 文本框背景色;
? ? foreground(fg)? ? ? ? 前景色;
? ? selectbackground ? 選定文本背景色;
? ? selectforeground ? 選定文本前景色;
? ? borderwidth(bd)? ? 文本框邊框?qū)挾龋?/p>
? ? font? ? ? ? ? ? ? ? 字體;
? ? show? ? ? ? ? ? ? 文本框顯示的字符,若為*,表示文本框為密碼框;
? ? state? ? ? ? ? ? 狀態(tài);
? ? width? ? ? ? ? 文本框?qū)挾?/p>
? ? textvariable? ? ? 可變文本,與StringVar等配合著用
7、標(biāo)簽tkinter.Label組件控制參數(shù)
? ? Anchor? ? ? ? 標(biāo)簽中文本的位置;
? ? background(bg) 背景色;
? ? foreground(fg) ? ? 前景色;
? ? borderwidth(bd) ? 邊框?qū)挾龋?/p>
? ? width? ? ? ? 標(biāo)簽寬度;
? ? height? ? ? ? 標(biāo)簽高度;
? ? bitmap? ? ? ? ? 標(biāo)簽中的位圖;
? ? font? ? ? ? ? ? 字體;
? ? image? ? ? ? 標(biāo)簽中的圖片;
? ? justify? ? ? ? 多行文本的對齊方式;
? ? text? ? ? 標(biāo)簽中的文本,可以使用'\n'表示換行
? ? textvariable? ? 顯示文本自動更新,與StringVar等配合著用
8、單選框和復(fù)選框Radiobutton,Checkbutton控制參數(shù)
? ? anchor? ? ? ? 文本位置;
? ? background(bg) 背景色;
? ? foreground(fg)? ? 前景色;
? ? borderwidth? ? ? 邊框?qū)挾龋?/p>
? ? width? ? ? ? ? 組件的寬度;
? ? height? ? ? ? 組件高度;
? ? bitmap? ? ? ? 組件中的位圖;
? ? image? ? ? ? ? 組件中的圖片;
? ? font? ? ? ? ? 字體;
? ? justify? ? ? 組件中多行文本的對齊方式;
? ? text? ? ? ? ? 指定組件的文本;
? ? value? ? ? ? ? 指定組件被選中中關(guān)聯(lián)變量的值;
? ? variable? ? ? ? 指定組件所關(guān)聯(lián)的變量;
? ? indicatoron? ? ? ? 特殊控制參數(shù),當(dāng)為0時,組件會被繪制成按鈕形式;
? ? textvariable? ? ? 可變文本顯示,與StringVar等配合著用
9、組圖組件Canvas控制參數(shù)
? ? background(bg)? ? 背景色;
? ? foreground(fg)? ? ? 前景色;
? ? borderwidth? 組件邊框?qū)挾龋?/p>
? ? width? ? ? ? 組件寬度;
? ? height? ? ? ? ? 高度;
? ? bitmap ? ? ? ? ? 位圖;
? ? image ? ? ? ? 圖片;
繪圖的方法主要以下幾種:
? ? create_arc? ? ? ? ? 圓弧;
? ? create_bitmap? ? 繪制位圖,支持XBM;
? ? create_image? ? 繪制圖片,支持GIF(x,y,image,anchor);
? ? create_line? ? ? ? 繪制支線;
? ? create_oval;? ? ? ? 繪制橢圓;
? ? create_polygon? 繪制多邊形(坐標(biāo)依次羅列,不用加括號,還有參數(shù),fill,outline);
? ? create_rectangle 繪制矩形((a,b,c,d),值為左上角和右下角的坐標(biāo));
? ? create_text? ? ? ? 繪制文字(字體參數(shù)font,);
? ? create_window? ? 繪制窗口;
? ? delete? ? ? ? ? ? 刪除繪制的圖形;
? ? itemconfig? ? ? ? ? 修改圖形屬性,第一個參數(shù)為圖形的ID,后邊為想修改的參數(shù);
? ? move? ? ? ? ? ? 移動圖像(1,4,0),1為圖像對象,4為橫移4像素,0為縱移像素,然后用root.update()刷新即可看到圖像的移動,為了使多次移動變得可視,最好加上time.sleep()函數(shù);
? ? 只要用create_方法畫了一個圖形,就會自動返回一個ID,創(chuàng)建一個圖形時將它賦值給一個變量,需要ID時就可以使用這個變量名。
? ? coords(ID)? ? ? ? ? 返回對象的位置的兩個坐標(biāo)(4個數(shù)字元組);
對于按鈕組件、菜單組件等可以在創(chuàng)建組件時通過command參數(shù)指定其事件處理函數(shù)。方法為bind;或者用bind_class方法進行類綁定,bind_all方法將所有組件事件綁定到事件響應(yīng)函數(shù)上。
10、菜單Menu
參數(shù):
? ? tearoff? ? ? ? 分窗,0為在原窗,1為點擊分為兩個窗口
? ? bg,fg? ? ? ? 背景,前景
? ? borderwidth? ? 邊框?qū)挾?/p>
? ? font? ? ? ? ? ? ? 字體
? ? activebackgound? 點擊時背景,同樣有activeforeground,activeborderwidth,disabledforeground
? ? cursor
? ? postcommand
? ? selectcolor? ? 選中時背景
? ? takefocus
? ? title? ? ?
? ? type
? ? relief
方法:
? ? menu.add_cascade? ? ? 添加子選項
? ? menu.add_command? ? ? 添加命令(label參數(shù)為顯示內(nèi)容)
? ? menu.add_separator? ? 添加分隔線
? ? menu.add_checkbutton? 添加確認按鈕
? ? delete? ? ? ? ? ? ? ? 刪除
11、事件關(guān)聯(lián)
bind(sequence,func,add)——
bind_class(className,sequence,func,add)
bind_all(sequence,func,add)
事件參數(shù):
sequence? ? ? 所綁定的事件;
func? ? ? ? ? ? 所綁定的事件處理函數(shù);
add? ? ? ? ? ? 可選參數(shù),為空字符或‘+’;
className? ? 所綁定的類;
鼠標(biāo)鍵盤事件
? ? ? ? ? ? ? 鼠標(biāo)左鍵按下,2表示中鍵,3表示右鍵;
? ? ? ? ? 同上;
? ? 鼠標(biāo)左鍵釋放;
? ? ? ? ? ? 按住鼠標(biāo)左鍵移動;
? ? ? 雙擊左鍵;
? ? ? ? ? ? ? ? 鼠標(biāo)指針進入某一組件區(qū)域;
? ? ? ? ? ? ? ? 鼠標(biāo)指針離開某一組件區(qū)域;
? ? ? ? 滾動滾輪;
? ? ? ? 按下A鍵,A可用其他鍵替代;
? ? ? 同時按下alt和A;alt可用ctrl和shift替代;
? ? 快速按兩下A;
? ? ? 大寫狀態(tài)下按A;
窗口事件
? ? Activate? ? ? ? 當(dāng)組件由不可用轉(zhuǎn)為可用時觸發(fā);
? ? Configure? ? ? ? 當(dāng)組件大小改變時觸發(fā);
? ? Deactivate? ? 當(dāng)組件由可用轉(zhuǎn)變?yōu)椴豢捎脮r觸發(fā);
? ? Destroy? ? ? ? ? 當(dāng)組件被銷毀時觸發(fā);
? ? Expose? ? ? ? 當(dāng)組件從被遮擋狀態(tài)中暴露出來時觸發(fā);
? ? Unmap? ? ? ? 當(dāng)組件由顯示狀態(tài)變?yōu)殡[藏狀態(tài)時觸發(fā);
? ? Map? ? ? ? ? ? 當(dāng)組件由隱藏狀態(tài)變?yōu)轱@示狀態(tài)時觸發(fā);
? ? FocusIn? ? ? ? 當(dāng)組件獲得焦點時觸發(fā);
? ? FocusOut? ? ? 當(dāng)組件失去焦點時觸發(fā);
? ? Property? ? ? ? 當(dāng)窗體的屬性被刪除或改變時觸發(fā);
? ? Visibility? ? ? 當(dāng)組件變?yōu)榭梢暊顟B(tài)時觸發(fā);
響應(yīng)事件
event對象(def function(event)):
? ? char? ? ? ? ? 按鍵字符,僅對鍵盤事件有效;
? ? keycode? ? 按鍵名,僅對鍵盤事件有效;
? ? keysym? ? 按鍵編碼,僅對鍵盤事件有效;
? ? num? ? ? ? ? 鼠標(biāo)按鍵,僅對鼠標(biāo)事件有效;
? ? type? ? ? ? ? ? 所觸發(fā)的事件類型;
? ? widget? ? ? ? ? 引起事件的組件;
? ? width,heigh ? 組件改變后的大小,僅Configure有效;
? ? x,y? ? ? ? 鼠標(biāo)當(dāng)前位置,相對于窗口;
? ? x_root,y_root 鼠標(biāo)當(dāng)前位置,相對于整個屏幕
12、彈窗
messagebox._show函數(shù)的控制參數(shù):
? ? default? ? ? ? 指定消息框按鈕;
? ? icon? ? ? ? ? ? 指定消息框圖標(biāo);
? ? message? ? 指定消息框所顯示的消息;
? ? parent? ? ? ? ? 指定消息框的父組件;
? ? title? ? ? ? ? 標(biāo)題;
? ? type? ? ? ? ? ? 類型;
simpledialog模塊參數(shù):
? ? title? ? ? ? ? 指定對話框的標(biāo)題;
? ? prompt? ? ? ? 顯示的文字;
? ? initialvalue? ? 指定輸入框的初始值;
filedialog 模塊參數(shù):
? ? filetype? ? 指定文件類型;
? ? initialdir ? 指定默認目錄;
? ? initialfile 指定默認文件;
? ? title? ? ? 指定對話框標(biāo)題
colorchooser模塊參數(shù):
? ? initialcolor? 指定初始化顏色;
? ? title? ? ? ? ? 指定對話框標(biāo)題;
說明:messagebox使用
import?tkinter?as?tk #不含message
import?tkinter.messagebox??#用message必須用這個引入
tkinter.messagebox.showinfo('提示框','操作完成,文件已保存')? #這樣調(diào)用
13、字體(font)
一般格式:
('Times -10 bold')
('Times',10,'bold','italic')??? 依次表示字體、字號、加粗、傾斜
14、補充:
config??????????? 重新配置
label.config(font='Arial -%d bold' % scale.get())
依次為字體,大?。ù笮】蔀樽痔柎笮。哟?/p>
tkinter.StringVar??? 能自動刷新的字符串變量,可用set和get方法進行傳值和取值,類似的還有IntVar,DoubleVar...
sys.stdout.flush() 刷新輸出
二、創(chuàng)建一個GUI程序
1、導(dǎo)入 Tkinter 模塊:import tkinter?as?tk
2、創(chuàng)建控件: root=tk.TK()
3、指定這個控件的 master, 即這個控件屬于哪一個? ?root.title("第一個窗口")
4、告訴 GM(geometry manager) 有一個控件產(chǎn)生了。root .mainloop()
代碼
import?tkinter?as?tk? #導(dǎo)入模塊
root=tk.Tk()??#建立TK
root.title("第一個窗口")?#主窗口
li=['c','python','php']? #建立列表數(shù)據(jù)
movie=['css','bootstrap','jquery']??#建立列表數(shù)據(jù)
listb1=tk.Listbox(root)? ?#建立列表
listb2=tk.Listbox(root)?#建立列表?
for?item?in?li:
????listb1.insert(0,item)? ?#向列表中 添加列表數(shù)據(jù)
for?item?in?movie:
????listb2.insert(0,item)????#向列表中 添加列表數(shù)據(jù)
listb1.pack()? #將列表控件,打包到主窗口上
listb2.pack()??#將列表控件,打包到主窗口上
root.mainloop()? #告訴幾何控件,更新主窗口,顯示
三、例子:文本框text、輸入框ENTRY、列表Listbox、按鈕button等用法
import?tkinter?as?tk
root=tk.Tk()
root.title("財務(wù)小助手")
root.geometry('300x500')???#定義窗口大小?x連接
#以下是列表?listbox控件
li=['c','python','php']???#建立兩個表
movie=['css','bootstrap','jquery']
listb1=tk.Listbox(root)
listb2=tk.Listbox(root)
for?item?in?li:
????????listb1.insert(0,item)??#從開始位置0,插入列表數(shù)據(jù)
for?item?in?movie:
????????listb2.insert(0,item)
listb2.insert(2,'我的')#從索引位置強制插入
listb1.insert('end','很高')#在最后插入
listb1.delete(2)?#刪除第2個索引
listb1.pack()
listb2.pack()
#定義一個lable?變量
var1=tk.StringVar()
my_lb=tk.Label(root,bg='yellow',width=4,textvariable=var1)
my_lb.pack()
#選擇列表框的值,放到lable?
def?print_selection():
????value=listb1.get(listb1.curselection())
????var1.set(value)
bt2=tk.Button(root,text='print?selection',width=15,height=2,command=print_selection)
bt2.pack()
#======以下是按鈕?BUTTON控件
var?=tk.StringVar()?#字符串變量
my_lb1=tk.Label(root,textvariable=var,bg='green',width=20,height=2,font=('Arial',12))
my_lb1.pack()??#那個pack?在前?,那個出現(xiàn)的就在前
on_hit=False
def?hit_me():
????global?on_hit??#聲明?on_hit?變量為全局變量
????if?on_hit==False:
????????on_hit=True
????????var.set('you?hit?me')
????else:
????????on_hit=False
????????var.set('')
my_b=tk.Button(root,text='HIT?ME',width=20,height=2,command=hit_me)
my_b.pack()
#======以下是輸入ENTRY?和文本TEXT控件
my_et=tk.Entry(root,show='*')?#show?想讓其顯示什么字符,就輸入什么,如不控制,可用None
my_et.pack()
def?insert_point():
????var=my_et.get()? ?#獲得ENTRY的值
????my_text1.insert('insert',var)
def?insert_end():?#定義函數(shù)
????var=my_et.get()
????#my_text1.insert('end',var)?#插入到結(jié)尾
????my_text1.insert(1.0,var)??#插入到指定行列,從1行0列開始
my_bt1=tk.Button(root,text='insert?point',command=insert_point)
my_bt1.pack()
my_bt2=tk.Button(root,text='insert?end',command=insert_end)
my_bt2.pack()
my_text1=tk.Text(root,height=2)
my_text1.pack()
root.mainloop()
radiobutton的用法:單選按鈕
#-*--coding:utf-8?--*-
#tkinter??radiobutton
import?tkinter?as?tk
root=tk.Tk()
root.title("python學(xué)習(xí)?之radiobutton")
root.geometry('300x400')
var=tk.StringVar()
lb1=tk.Label(root,width=20,bg='yellow',text='empty')
lb1.pack()
def?print_selection():
????lb1.config(text='you?have?selected'+var.get())
rb1=tk.Radiobutton(root,text='Option?A',variable=var,value='A',command=print_selection)
rb1.pack()
rb2=tk.Radiobutton(root,text='Option?B',variable=var,value='B',command=print_selection)
rb2.pack()
rb3=tk.Radiobutton(root,text='Option?C',variable=var,value='C',command=print_selection)
rb3.pack()
root.mainloop()

SCALE尺度的列子
import?tkinter?as?tk
root=tk.Tk()
root.title('scale學(xué)習(xí)')
root.geometry('500x400')
lb=tk.Label(root,bg='yellow',width=20,text='empty')
lb.pack()
def?print_selection(v):
????lb.config(text='you?have?selected??'+v)
sc=tk.Scale(root,label='try?me',from_=5,to=30,orient=tk.HORIZONTAL,length=500,showvalue=1,tickinterval=2,resolution=0.01,command=print_selection)
sc.pack()
root.mainloop()

canvas 畫布:
import?tkinter?as?tk
root=tk.Tk()
root.title('scale學(xué)習(xí)')
root.geometry('800x600')
canvas1=tk.Canvas(root,bg='blue',height=500,width=500)??#建立畫布
image_file=tk.PhotoImage(file='E:/編程/test/按照某一列把一個Excel文件拆分為多個Excel文件/image.gif')????#獲得圖片
image=canvas1.create_image(0,0,anchor='nw',image=image_file)??#在畫布上加載圖片,nw是錨點位置
x0,y0,x1,y1=100,100,150,150???#定義坐標(biāo)
line=canvas1.create_line(x0,y0,x1,y1)?#在畫布上劃線
oval=canvas1.create_oval(x0,y0,x1,y1,fill='red')?#在畫布上劃圓?并填充
arc=canvas1.create_arc(x0+30,y0+30,x1+30,y1+30,start=0,extent=170)?#在畫布上劃扇形,從開始角度start?0度開始,到170度結(jié)束
rect=canvas1.create_rectangle(200,80,280,260,fill='yellow')?#在畫布上劃矩形,這里是正方形
canvas1.pack()
def?moveit():
????canvas1.move(oval,0,5)???#圖形位移,移動那個選那個,這里是移動圓OVAL,列不變0,行每次5
bt=tk.Button(root,text='move',command=moveit)
bt.pack()
root.mainloop()

定義菜單Menubar

框架 FRAME 的用法,可用于窗口排列、控制等

部件放置位置pack grid place 放置位置
放置位置有三種方式:
1)pack
常見的打包放置:
lb=tkinter.Lable(root,text='放置位置')
lb.pack(side='left')? #位置為用side=‘位置’,位置為:left,right,top,bottom
2)grid? 方格的方式:幾行幾列的方式
部件.grid(row=*,column=*)? #行等于多少,列等于多少
3)place
部件.place(x=10,y=11,anchor='nw')

建立子窗口 TopLevel
import?tkinter?as?tk
root=tk.Tk()
root.title("菜單建立")
root.geometry('500x400')
#--下面是打開新窗口
def?open_wd():
????new_wd=tk.Toplevel(root)??#在主窗口上,建立新窗口
????new_wd.title('子窗口')
????new_wd.geometry('300x300')
bt1=tk.Button(root,text='打開新窗口',command=open_wd)
bt1.place(x=10,y=10)
root.mainloop()

python3的 Tkinter 學(xué)習(xí)視頻,參考如下:
https://morvanzhou.github.io/tutorials/python-basic/tkinter/