python窗体设置italic_007萝卜头学python:Python GUI 之Tkinter
蘿卜頭社區 http://www.luobotou.net 同學問,python有沒有圖形窗口?
肯定是有的,今天我們來了解下tkinter:Tkinter =tk+inter(接口)
Tkinter 是 Python 的標準 GUI 庫。Python 使用 Tkinter 可以快速的創建 GUI 應用程序。
由于 Tkinter 是內置到 python 的安裝包中、只要安裝好 Python 之后就能 import Tkinter 庫、而且 IDLE 也是用 Tkinter 編寫而成、對于簡單的圖形界面 Tkinter 還是能應付自如。
注意:Python3.x 版本使用的庫名為 tkinter,即首寫字母 T 為小寫:tkinter。
import tkinter
一、tkinter模塊常用參數(python3)
1、使用tkinter.Tk() 生成主窗口(root=tkinter.Tk())root.title('標題名')??? 修改框體的名字,也可在創建時使用className參數來命名;
root.resizable(0,0)??? 框體大小可調性,分別表示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') #生成標簽
label.pack()? ? ? ? #將標簽添加到主窗口
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()? ? ? ? ? ? #進入消息循環(必需組件)
3、tkinter中的15種核心組件
Button? ? ? ? 按鈕;
Canvas? ? ? ? 繪圖形組件,可以在其中繪制圖形;
Checkbutton? ? ? 復選框;
Entry? ? ? ? 文本框(單行);
Text? ? ? ? ? ? 文本框(多行);
Frame? ? ? ? 框架,將幾個組件組成一組
Label? ? ? ? 標簽,可以顯示文字或圖片;
Listbox? ? ? 列表框;
Menu? ? ? ? 菜單;
Menubutton? ? ? 它的功能完全可以使用Menu替代;
Message? ? ? ? ? 與Label組件類似,但是可以根據自身大小將文本換行;
Radiobutton? ? ? 單選框;
Scale? ? ? ? 滑塊;允許通過滑塊來設置一數字值
Scrollbar? ? ? ? 滾動條;配合使用canvas, entry, listbox, and text窗口部件的標準滾動條;
Toplevel? ? ? ? 用來創建子窗口窗口組件。
(在Tkinter中窗口部件類沒有分級;所有的窗口部件類在樹中都是兄弟。)
4、組件的放置和排版(pack,grid,place)
pack組件設置位置屬性參數:
after:? ? 將組件置于其他組件之后;
before:? ? 將組件置于其他組件之前;
anchor:? ? ? 組件的對齊方式,頂對齊'n',底對齊's',左'w',右'e'? ? side:? ? 組件在主窗口的位置,可以為'top','bottom','left','right'(使用時tkinter.TOP,tkinter.E);
fill? ? ? ? ? ? 填充方式 (Y,垂直,X,水平)
expand? ? ? ? ? 1可擴展,0不可擴展
grid組件使用行列的方法放置組件的位置,參數有:
column:? ? ? ? 組件所在的列起始位置;
columnspam:? ? 組件的列寬;
row:? ? ? 組件所在的行起始位置;
rowspam:? ? 組件的行寬;
place組件可以直接使用坐標來放置組件,參數有:
anchor:? ? 組件對齊方式;
x:? ? ? ? 組件左上角的x坐標;
y:? ? ? ? ? 組件右上角的y坐標;
relx:? ? ? ? 組件相對于窗口的x坐標,應為0-1之間的小數;
rely:? ? ? ? ? 組件相對于窗口的y坐標,應為0-1之間的小數;
width:? ? ? ? ? 組件的寬度;
heitht:? ? ? 組件的高度;
relwidth:? ? ? 組件相對于窗口的寬度,0-1;
relheight: ? ? 組件相對于窗口的高度,0-1;
5、使用tkinter.Button時控制按鈕的參數
anchor:? ? ? ? 指定按鈕上文本的位置;
background(bg)? ? ? 指定按鈕的背景色;
bitmap:? ? ? 指定按鈕上顯示的位圖;
borderwidth(bd) 指定按鈕邊框的寬度;
command:? ? 指定按鈕消息的回調函數;
cursor:? ? ? ? 指定鼠標移動到按鈕上的指針樣式;
font:? ? ? ? ? ? 指定按鈕上文本的字體;
foreground(fg) 指定按鈕的前景色;
height:? ? ? ? 指定按鈕的高度;
image:? ? ? ? 指定按鈕上顯示的圖片;
state:? ? ? ? ? 指定按鈕的狀態(disabled);
text:? ? ? ? ? 指定按鈕上顯示的文本;
width:? ? ? ? 指定按鈕的寬度
padx? ? ? ? ? 設置文本與按鈕邊框x的距離,還有pady;
activeforeground 按下時前景色
textvariable? ? ? 可變文本,與StringVar等配合著用
6、文本框tkinter.Entry,tkinter.Text控制參數
background(bg)? 文本框背景色;
foreground(fg)? ? ? ? 前景色;
selectbackground ? 選定文本背景色;
selectforeground ? 選定文本前景色;
borderwidth(bd)? ? 文本框邊框寬度;
font? ? ? ? ? ? ? ? 字體;
show? ? ? ? ? ? ? 文本框顯示的字符,若為*,表示文本框為密碼框;
state? ? ? ? ? ? 狀態;
width? ? ? ? ? 文本框寬度
textvariable? ? ? 可變文本,與StringVar等配合著用
7、標簽tkinter.Label組件控制參數
Anchor? ? ? ? 標簽中文本的位置;
background(bg) 背景色;
foreground(fg) ? ? 前景色;
borderwidth(bd) ? 邊框寬度;
width? ? ? ? 標簽寬度;
height? ? ? ? 標簽高度;
bitmap? ? ? ? ? 標簽中的位圖;
font? ? ? ? ? ? 字體;
image? ? ? ? 標簽中的圖片;
justify? ? ? ? 多行文本的對齊方式;
text? ? ? 標簽中的文本,可以使用'\n'表示換行
textvariable? ? 顯示文本自動更新,與StringVar等配合著用
8、單選框和復選框Radiobutton,Checkbutton控制參數
anchor? ? ? ? 文本位置;
background(bg) 背景色;
foreground(fg)? ? 前景色;
borderwidth? ? ? 邊框寬度;
width? ? ? ? ? 組件的寬度;
height? ? ? ? 組件高度;
bitmap? ? ? ? 組件中的位圖;
image? ? ? ? ? 組件中的圖片;
font? ? ? ? ? 字體;
justify? ? ? 組件中多行文本的對齊方式;
text? ? ? ? ? 指定組件的文本;
value? ? ? ? ? 指定組件被選中中關聯變量的值;
variable? ? ? ? 指定組件所關聯的變量;
indicatoron? ? ? ? 特殊控制參數,當為0時,組件會被繪制成按鈕形式;
textvariable? ? ? 可變文本顯示,與StringVar等配合著用
9、組圖組件Canvas控制參數
background(bg)? ? 背景色;
foreground(fg)? ? ? 前景色;
borderwidth? 組件邊框寬度;
width? ? ? ? 組件寬度;
height? ? ? ? ? 高度;
bitmap ? ? ? ? ? 位圖;
image ? ? ? ? 圖片;
繪圖的方法主要以下幾種:
create_arc? ? ? ? ? 圓弧;
create_bitmap? ? 繪制位圖,支持XBM;
create_image? ? 繪制圖片,支持GIF(x,y,image,anchor);
create_line? ? ? ? 繪制支線;
create_oval;? ? ? ? 繪制橢圓;
create_polygon? 繪制多邊形(坐標依次羅列,不用加括號,還有參數,fill,outline);
create_rectangle 繪制矩形((a,b,c,d),值為左上角和右下角的坐標);
create_text? ? ? ? 繪制文字(字體參數font,);
create_window? ? 繪制窗口;
delete? ? ? ? ? ? 刪除繪制的圖形;
itemconfig? ? ? ? ? 修改圖形屬性,第一個參數為圖形的ID,后邊為想修改的參數;
move? ? ? ? ? ? 移動圖像(1,4,0),1為圖像對象,4為橫移4像素,0為縱移像素,然后用root.update()刷新即可看到圖像的移動,為了使多次移動變得可視,最好加上time.sleep()函數;
只要用create_方法畫了一個圖形,就會自動返回一個ID,創建一個圖形時將它賦值給一個變量,需要ID時就可以使用這個變量名。
coords(ID)? ? ? ? ? 返回對象的位置的兩個坐標(4個數字元組);
對于按鈕組件、菜單組件等可以在創建組件時通過command參數指定其事件處理函數。方法為bind;或者用bind_class方法進行類綁定,bind_all方法將所有組件事件綁定到事件響應函數上。
10、菜單Menu
參數:
tearoff? ? ? ? 分窗,0為在原窗,1為點擊分為兩個窗口
bg,fg? ? ? ? 背景,前景
borderwidth? ? 邊框寬度
font? ? ? ? ? ? ? 字體
activebackgound? 點擊時背景,同樣有activeforeground,activeborderwidth,disabledforeground
cursor
postcommand
selectcolor? ? 選中時背景
takefocus
title
type
relief
方法:
menu.add_cascade? ? ? 添加子選項
menu.add_command? ? ? 添加命令(label參數為顯示內容)
menu.add_separator? ? 添加分隔線
menu.add_checkbutton? 添加確認按鈕
delete? ? ? ? ? ? ? ? 刪除
11、事件關聯
bind(sequence,func,add)——
bind_class(className,sequence,func,add)
bind_all(sequence,func,add)
事件參數:
sequence? ? ? 所綁定的事件;
func? ? ? ? ? ? 所綁定的事件處理函數;
add? ? ? ? ? ? 可選參數,為空字符或‘+’;
className? ? 所綁定的類;
鼠標鍵盤事件
鼠標左鍵按下,2表示中鍵,3表示右鍵;
同上;
鼠標左鍵釋放;
按住鼠標左鍵移動;
雙擊左鍵;
鼠標指針進入某一組件區域;
鼠標指針離開某一組件區域;
滾動滾輪;
按下A鍵,A可用其他鍵替代;
同時按下alt和A;alt可用ctrl和shift替代;
快速按兩下A;
大寫狀態下按A;
窗口事件
Activate? ? ? ? 當組件由不可用轉為可用時觸發;
Configure? ? ? ? 當組件大小改變時觸發;
Deactivate? ? 當組件由可用轉變為不可用時觸發;
Destroy? ? ? ? ? 當組件被銷毀時觸發;
Expose? ? ? ? 當組件從被遮擋狀態中暴露出來時觸發;
Unmap? ? ? ? 當組件由顯示狀態變為隱藏狀態時觸發;
Map? ? ? ? ? ? 當組件由隱藏狀態變為顯示狀態時觸發;
FocusIn? ? ? ? 當組件獲得焦點時觸發;
FocusOut? ? ? 當組件失去焦點時觸發;
Property? ? ? ? 當窗體的屬性被刪除或改變時觸發;
Visibility? ? ? 當組件變為可視狀態時觸發;
響應事件
event對象(def function(event)):
char? ? ? ? ? 按鍵字符,僅對鍵盤事件有效;
keycode? ? 按鍵名,僅對鍵盤事件有效;
keysym? ? 按鍵編碼,僅對鍵盤事件有效;
num? ? ? ? ? 鼠標按鍵,僅對鼠標事件有效;
type? ? ? ? ? ? 所觸發的事件類型;
widget? ? ? ? ? 引起事件的組件;
width,heigh ? 組件改變后的大小,僅Configure有效;
x,y? ? ? ? 鼠標當前位置,相對于窗口;
x_root,y_root 鼠標當前位置,相對于整個屏幕
12、彈窗
messagebox._show函數的控制參數:
default? ? ? ? 指定消息框按鈕;
icon? ? ? ? ? ? 指定消息框圖標;
message? ? 指定消息框所顯示的消息;
parent? ? ? ? ? 指定消息框的父組件;
title? ? ? ? ? 標題;
type? ? ? ? ? ? 類型;
simpledialog模塊參數:
title? ? ? ? ? 指定對話框的標題;
prompt? ? ? ? 顯示的文字;
initialvalue? ? 指定輸入框的初始值;
filedialog 模塊參數:
filetype? ? 指定文件類型;
initialdir ? 指定默認目錄;
initialfile 指定默認文件;
title? ? ? 指定對話框標題
colorchooser模塊參數:
initialcolor? 指定初始化顏色;
title? ? ? ? ? 指定對話框標題;
說明:messagebox使用import?tkinter?as?tk #不含message
import?tkinter.messagebox??#用message必須用這個引入
tkinter.messagebox.showinfo('提示框','操作完成,文件已保存')? #這樣調用
13、字體(font)
一般格式:
('Times -10 bold')
('Times',10,'bold','italic')??? 依次表示字體、字號、加粗、傾斜
14、補充:
config??????????? 重新配置
label.config(font='Arial -%d bold' % scale.get())
依次為字體,大小(大小可為字號大小),加粗
tkinter.StringVar??? 能自動刷新的字符串變量,可用set和get方法進行傳值和取值,類似的還有IntVar,DoubleVar...
sys.stdout.flush() 刷新輸出
二、創建一個GUI程序
1、導入 Tkinter 模塊:import tkinter?as?tk
2、創建控件: root=tk.TK()
3、指定這個控件的 master, 即這個控件屬于哪一個? ?root.title("第一個窗口")
4、告訴 GM(geometry manager) 有一個控件產生了。root .mainloop()
代碼import?tkinter?as?tk? #導入模塊
root=tk.Tk()??#建立TK
root.title("第一個窗口")?#主窗口
li=['c','python','php']? #建立列表數據
movie=['css','bootstrap','jquery']??#建立列表數據
listb1=tk.Listbox(root)? ?#建立列表
listb2=tk.Listbox(root)?#建立列表
for?item?in?li:
listb1.insert(0,item)? ?#向列表中 添加列表數據
for?item?in?movie:
listb2.insert(0,item)????#向列表中 添加列表數據
listb1.pack()? #將列表控件,打包到主窗口上
listb2.pack()??#將列表控件,打包到主窗口上
root.mainloop()? #告訴幾何控件,更新主窗口,顯示
三、例子:文本框text、輸入框ENTRY、列表Listbox、按鈕button等用法
import?tkinter?as?tk
root=tk.Tk()
root.title("財務小助手")
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,插入列表數據
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?在前?,那個出現的就在前
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():?#定義函數
var=my_et.get()
#my_text1.insert('end',var)?#插入到結尾
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學習?之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學習')
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學習')
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???#定義坐標
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度結束
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 的用法,可用于窗口排列、控制等
放置位置有三種方式:
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')
建立子窗口 TopLevelimport?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 學習視頻,參考如下:
總結
以上是生活随笔為你收集整理的python窗体设置italic_007萝卜头学python:Python GUI 之Tkinter的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++ 不允许使用抽象类类型的对象_Ja
- 下一篇: h5实现网页内容跟随窗口大小移动_HTM