Tkinter最佳實踐(半小時)


概述:

簡介

  • Tkinter模塊("Tk 接口")是Python的標准Tk GUI工具包的接口.Tk和Tkinter可以在大多數的Unix平台下使用,同樣可以應用在Windows和Macintosh系統里.Tk8.0的后續版本可以實現本地窗口風格,並良好地運行在絕大多數平台中。

目的:

  1. 網絡上對於Tinker都是比較散亂的介紹,需要花費大量時間理清楚,實際上我發現理解Tkinter的就很簡單的事件,一般包括界面的組織,控件的學習,事件的綁定。窗口間的通訊。

x  2: 代碼鏈接:https://gitee.com/dgwcode/an_example_of_py_learning/tree/master/language_python/LearTinker

入門:

 

  1. 基本窗口繪制:
    1.   win.geometry('600x800+500+50'); x 必須為小寫字母x
import tkinter;
# 創建主窗口
win=tkinter.Tk();
win.title("HI Tkinker");
win.geometry('600x800+500+50');
# 進入消息循環,可以寫控件
...
win.mainloop();

  

 

布局

絕對布局place:

參數
anchor:   組件對齊方式;n, ne, e, se, s, sw, w, nw, or center ; ('n'==N)
x:   組件左上角的x坐標;
y:     組件左上角的y坐標;
relx:    組件左上角相對於窗口的x坐標,應為0-1之間的小數;圖形位置相對窗口變化
rely:    組件左上角相對於窗口的y坐標,應為0-1之間的小數;
width:  組件的寬度;
heitht:   組件的高度;
relwidth:  組件相對於窗口的寬度,0-1之間的小數,圖形寬度相對窗口變化;
relheight:  組件相對於窗口的高度,0-1之間的小數;

# coding=utf-8

import tkinter;

win = tkinter.Tk();
win.title('絕對布局')
win.geometry(
'400x600+400+50'
);
# 寫入button測試布局
bt1 = tkinter.Button(win, text='按鈕1', bg='red');
bt2 = tkinter.Button(win, text='按鈕2', bg='black');
bt3 = tkinter.Button(win, text='按鈕3', bg='yellow');

# 設置位置
bt1.place(x=100,y=20);
bt2.place(x=100,y=50);
bt3.place(x=100,y=80);

win.mainloop();

 

 

 

 

 

相對布局pack

 

參數
after:   將組件置於其他組件之后;
before:   將組件置於其他組件之前;
anchor:   組件的對齊方式,頂對齊'n',底對齊's',左'w',右'e'
side:   組件在主窗口的位置,可以為'top','bottom','left','right'(使用時tkinter.TOP,tkinter.LEFT);
fill: 填充方式 (Y,垂直,X,水平,BOTH,水平+垂直),是否在某個方向充滿窗口

expand           1可擴展,0不可擴展,代表控件是否會隨窗口縮放

 

# coding=utf-8
import tkinter; win = tkinter.Tk(); win.title('絕對布局') win.geometry( '400x600+400+50' ); # 寫入button測試布局 bt1 = tkinter.Button(win, text='按鈕1', background='red'); bt2 = tkinter.Button(win, text='按鈕2', bg='black'); bt3 = tkinter.Button(win, text='按鈕3', bg='yellow'); # 設置位置 bt1.pack(side=tkinter.TOP); bt2.pack(side=tkinter.LEFT); bt3.pack(side=tkinter.RIGHT); win.mainloop();

 

 

 

表格布局、grid、

 

概念
column:    組件所在的列起始位置;                                             
columnspan:  組件的列寬;跨列數
row:   組件所在的行起始位置;
rowspan: 組件的行寬;rowspam=3   跨3行
sticky             對齊方式:NSEW(北南東西)上下左右               
padx、pady x方向間距、y方向間距(padx=5)    

 

import tkinter;

win = tkinter.Tk();
win.title('絕對布局')
win.geometry(
'400x600+400+50'
);
# 寫入button測試布局
bt1 = tkinter.Button(win, text='按鈕1', background='red');
bt2 = tkinter.Button(win, text='按鈕2', bg='red');
bt3 = tkinter.Button(win, text='按鈕3', bg='red');
bt4 = tkinter.Button(win, text='按鈕4', bg='red');
bt5 = tkinter.Button(win, text='按鈕5', bg='red');
bt6 = tkinter.Button(win, text='按鈕6', bg='red');

# 設置位置
bt1.grid(row=1,column=0,ipadx=10,ipady=10);
bt2.grid(row=1,column=1);
bt3.grid(row=1,column=2);

#對比 跨三行
bt4.grid(row=2,column=0,rowspan=3);
bt5.grid(row=2,column=1);
bt6.grid(row=3,column=2);
win.mainloop();

 

 

 

控件:

  • Label控件
  • Button
  • Entry
  • Text
  • Checkbutton
  • Radiobutton
  • Listbox
  • Scale
  • SpinBox
  • Menu
  • Combobox
  • Frame

 

事件

鼠標事件類型:

<Button-1> 按下了鼠標左鍵 <ButtonPress-1>
<Button-2> 按下了鼠標中鍵 <ButtonPress-2>
<Button-3> 按下了鼠標右鍵 <ButtonPress-3>
<Enter> 鼠標進入組件區域
<Leave> 鼠標離開組件區域
<ButtonRelease-1> 釋放了鼠標左鍵
<ButtonRelease-2> 釋放了鼠標中鍵
<ButtonRelease-3> 釋放了鼠標右鍵
<B1-Moion> 按住鼠標左鍵移動
<B2-Moion> 按住鼠標中鍵移動
<B3-Moion> 按住鼠標右鍵移動
<Double-Button-1> 雙擊鼠標左鍵
<Double-Button-2> 雙擊鼠標中鍵
<Double-Button-3> 雙擊鼠標右鍵
<MouseWheel> 滾動鼠標滾輪

 

鍵盤事件類型:


<KeyPress-A> 表示按下鍵盤A鍵 A可以設置為其他的按鍵
<Alt-KeyPress-A> 表示同時按下Alt和A鍵 A可以設置為其他的按鍵
<Control-KeyPress-A> 表示同時按下Ctrl和A鍵 A可以設置為其他的按鍵
<Shift-KeyPress-A> 表示同時按下Shift和A鍵 A可以設置為其他的按鍵
<Double-KeyPress-A> 表示雙擊鍵盤A鍵 A可以設置為其他的按鍵
<Lock-KeyPress-A> 表示開啟大寫之后鍵盤A鍵 A可以設置為其他的按鍵
<Alt-Control-KeyPress-A> 表示同時按下alt+Ctrl和A鍵 A可以設置為其他的按鍵

注意:鍵盤事件除了entry和text組件其他組件的事件最好綁定在主界面上事件對象中包含的信息


c. event 對象包含的事件信息


x,y 當前觸發事件時鼠標相對觸發事件的組件的坐標值
x_root,y_root 當前觸發事件時鼠標相對於屏幕的坐標值
char 獲取當前鍵盤事件時按下的鍵對應的字符
keycode 獲取當前鍵盤事件時按下的鍵對應的的ascii碼
type 獲取事件的類型
num 獲取鼠標按鍵類型 123 左中右
widget 觸發事件的組件
width/height 組件改變之后的大小和configure()相關

 

d:窗口和組件相關事件類型:


Activate 當中組件由不可以用變為可用時 針對於state的變值
Deactivate 當組件由可用變為不可用時觸發
Configure 當組件大小發生變化時觸發
Destory 當組件銷毀時觸發
FocusIn 當組件獲取焦點時觸發 針對於Entry和Text有效
Map 當組件由隱藏變為顯示時觸發
UnMap 當組件由顯示變為隱藏時觸發
Perproty 當窗口屬性發生變化時觸發

 

其他常用模塊

(簡單對話/文件對話/顏色對話)

tkinter.simpledialog模塊 可以創建標准的輸入對話框。
tkinter.filedialog模塊 可以創建文件打開和保存文件對話框。
tkinter.colorchooser模塊 可以創建顏色選擇對話框。

 

tkinter.simpledialog模塊

tkinter.simpledialog.askstring(標題,提示文字,初始值) 輸入字符串
tkinter.simpledialog.askinteger(title,prompt,initialvalue) 輸入整數
tkinter.simpledialog.askfloat(title,prompt,initialvalue) 輸入浮點型
注意:第三個參數為關鍵字參數

 

tkinter.filedialog模塊

建議使用easygui 代替
tkinter.filedialog.askopenfilename(關鍵字參數傳入) 選取單個文件 創建標准的【 打開 文件】對話框。
.askopenfilenames 選取多個文件
.askdirectory 彈出選取壓縮地址路徑的對話框
tkinter.filedialog.asksaveasfilename(關鍵字參數傳入) 可以創建標准的【 保存 文件】對話框。
filetypes 指定文件類型。  
initialdir 指定默認目錄。  
 initialfile 指定默認文件。  
 title 指定對話框標題。
 

tkinter.colorchooser模塊

tkinter.colorchooser.askcolor(關鍵字參數傳入)

initialcolor 指定 初始化 顏色。  
title 指定 對話框 標題。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM