root.title('標題名') 修改框體的名字,也可在建立時使用className參數來命名;
root.resizable(0,0) 框體大小可調性,分別表示x,y方向的可變性;
root.geometry('250x150') 指定主框體大小;
root.quit() 退出;
root.update_idletasks()
root.update() 刷新頁面;html
1 import tkinter 2 root=tkinter.Tk() #生成root主窗口 3 label=tkinter.Label(root,text='Hello,GUI') #生成標籤 4 label.pack() #將標籤添加到主窗口 5 button1=tkinter.Button(root,text='Button1') #生成button1 6 button1.pack(side=tkinter.LEFT) #將button1添加到root主窗口 7 button2=tkinter.Button(root,text='Button2') 8 button2.pack(side=tkinter.RIGHT) 9 root.mainloop() #進入消息循環(必需組件)
Button 按鈕; Canvas 繪圖形組件,能夠在其中繪製圖形; Checkbutton 複選框; Entry 文本框(單行); Text 文本框(多行); Frame 框架,將幾個組件組成一組 Label 標籤,能夠顯示文字或圖片; Listbox 列表框; Menu 菜單; Menubutton 它的功能徹底可使用Menu替代; Message 與Label組件相似,可是能夠根據自身大小將文本換行; Radiobutton 單選框; Scale 滑塊;容許經過滑塊來設置一數字值 Scrollbar 滾動條;配合使用canvas, entry, listbox, and text窗口部件的標準滾動條; Toplevel 用來建立子窗口窗口組件。 (在Tkinter中窗口部件類沒有分級;全部的窗口部件類在樹中都是兄弟。)
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;
anchor: 指定按鈕上文本的位置;
background(bg) 指定按鈕的背景色;
bitmap: 指定按鈕上顯示的位圖;
borderwidth(bd) 指定按鈕邊框的寬度;
command: 指定按鈕消息的回調函數;
cursor: 指定鼠標移動到按鈕上的指針樣式;
font: 指定按鈕上文本的字體;
foreground(fg) 指定按鈕的前景色;
height: 指定按鈕的高度;
image: 指定按鈕上顯示的圖片;
state: 指定按鈕的狀態(disabled);
text: 指定按鈕上顯示的文本;
width: 指定按鈕的寬度
padx 設置文本與按鈕邊框x的距離,還有pady;
activeforeground 按下時前景色
textvariable 可變文本,與StringVar等配合着用
background(bg) 文本框背景色; foreground(fg) 前景色; selectbackground 選定文本背景色; selectforeground 選定文本前景色; borderwidth(bd) 文本框邊框寬度; font 字體; show 文本框顯示的字符,若爲*,表示文本框爲密碼框; state 狀態; width 文本框寬度 textvariable 可變文本,與StringVar等配合着用
Anchor 標籤中文本的位置; background(bg) 背景色; foreground(fg) 前景色; borderwidth(bd) 邊框寬度; width 標籤寬度; height 標籤高度; bitmap 標籤中的位圖; font 字體; image 標籤中的圖片; justify 多行文本的對齊方式; text 標籤中的文本,可使用'\n'表示換行 textvariable 顯示文本自動更新,與StringVar等配合着用
anchor 文本位置;
background(bg) 背景色;
foreground(fg) 前景色;
borderwidth 邊框寬度;
width 組件的寬度;
height 組件高度;
bitmap 組件中的位圖;
image 組件中的圖片;
font 字體;
justify 組件中多行文本的對齊方式;
text 指定組件的文本;
value 指定組件被選中中關聯變量的值;
variable 指定組件所關聯的變量;
indicatoron 特殊控制參數,當爲0時,組件會被繪製成按鈕形式;
textvariable 可變文本顯示,與StringVar等配合着用
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方法將全部組件事件綁定到事件響應函數上。
參數:
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 刪除
bind(sequence,func,add)—— bind_class(className,sequence,func,add) bind_all(sequence,func,add) 事件參數: sequence 所綁定的事件; func 所綁定的事件處理函數; add 可選參數,爲空字符或‘+’; className 所綁定的類; 鼠標鍵盤事件 <Button-1> 鼠標左鍵按下,2表示中鍵,3表示右鍵; <ButtonPress-1> 同上; <ButtonRelease-1> 鼠標左鍵釋放; <B1-Motion> 按住鼠標左鍵移動; <Double-Button-1> 雙擊左鍵; <Enter> 鼠標指針進入某一組件區域; <Leave> 鼠標指針離開某一組件區域; <MouseWheel> 滾動滾輪; <KeyPress-A> 按下A鍵,A可用其餘鍵替代; <Alt-KeyPress-A> 同時按下alt和A;alt可用ctrl和shift替代; <Double-KeyPress-A> 快速按兩下A; <Lock-KeyPress-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 鼠標當前位置,相對於整個屏幕
messagebox._show函數的控制參數:
default 指定消息框按鈕;
icon 指定消息框圖標;
message 指定消息框所顯示的消息;
parent 指定消息框的父組件;
title 標題;
type 類型;
simpledialog模塊參數:
title 指定對話框的標題;
prompt 顯示的文字;
initialvalue 指定輸入框的初始值;
filedialog 模塊參數:
filetype 指定文件類型;
initialdir 指定默認目錄;
initialfile 指定默認文件;
title 指定對話框標題
colorchooser模塊參數:
initialcolor 指定初始化顏色;
title 指定對話框標題;
通常格式:
('Times -10 bold')
('Times',10,'bold','italic') 依次表示字體、字號、加粗、傾斜canvas
補充:
config 從新配置
label.config(font='Arial -%d bold' % scale.get())
依次爲字體,大小(大小可爲字號大小),加粗
tkinter.StringVar 能自動刷新的字符串變量,可用set和get方法進行傳值和取值,相似的還有IntVar,DoubleVar...app
sys.stdout.flush() 刷新輸出框架
附1:tkinter中的顏色函數
附2:一個本身用tkinter寫的計算器程序oop
1 #filename:Caculater 2 3 import tkinter,time,decimal,math,string 4 5 root=tkinter.Tk() 6 root.title('計算器') 7 root.resizable(0,0) 8 global cuncu, vartext, result, fuhao 9 result = fuhao = None 10 vartext = tkinter.StringVar() 11 cuncu = [] 12 13 class anjianzhi: 14 global cuncu, vartext, result, fuhao 15 def __init__(self,anjian): 16 self.anjian = anjian 17 def jia(self): 18 cuncu.append(self.anjian) 19 vartext.set( ''.join(cuncu)) 20 def tui(self): 21 cuncu.pop() 22 vartext.set(''.join(cuncu)) 23 def clear(self): 24 cuncu.clear() 25 vartext.set('') 26 result = None 27 fuhao = None 28 def zhengfu(self): 29 if cuncu[0]: 30 if cuncu[0] == '-': 31 cuncu[0] = '+' 32 elif cuncu[0] == '+': 33 cuncu[0] = '-' 34 else: 35 cuncu.insert(0, '-') 36 vartext.set(''.join(cuncu)) 37 def xiaoshudian(self): 38 if cuncu.count('.') >= 1: 39 pass 40 else: 41 if cuncu == [] : 42 cuncu.append('0') 43 cuncu.append('.') 44 vartext.set(''.join(cuncu)) 45 def yunshuan(self): 46 global cuncu, vartext, result, fuhao 47 if vartext.get() == '': 48 pass 49 else: 50 get1 = decimal.Decimal(vartext.get()) 51 if self.anjian in ('1/x','sqrt'): 52 if self.anjian == '1/x': 53 result = 1/get1 54 elif self.anjian == 'sqrt': 55 result = math.sqrt(get1) 56 elif self.anjian in ('+','-','*','/','='): 57 if fuhao is not None: 58 get1 = decimal.Decimal(result) 59 get2 = decimal.Decimal(vartext.get()) 60 if fuhao == '+': 61 result = get1 + get2 62 elif fuhao == '-': 63 result = get1 - get2 64 elif fuhao == '*': 65 result = get1 * get2 66 elif fuhao == '/': 67 result = get1 / get2 68 else: 69 result = get1 70 if self.anjian == '=': 71 fuhao = None 72 else: 73 fuhao = self.anjian 74 print(fuhao) 75 print(result) 76 vartext.set(str(result)) 77 cuncu.clear() 78 79 def copy1(): 80 # tkinter.Misc().clipboard_clear() 81 tkinter.Misc().clipboard_append(string(vartext.get())) 82 83 def buju(root): 84 global cuncu, vartext, result, fuhao 85 entry1 = tkinter.Label(root, width=30, height=2, bg='white', anchor='se', textvariable=vartext) 86 entry1.grid(row=0, columnspan=5) 87 buttonMC=tkinter.Button(root,text='MC',width=5) 88 buttonMR=tkinter.Button(root,text='MR',width=5) 89 buttonMS=tkinter.Button(root,text='MS',width=5) 90 buttonM1=tkinter.Button(root,text='M+',width=5) 91 buttonM2=tkinter.Button(root,text='M-',width=5) 92 buttonMC.grid(row=1,column=0) 93 buttonMR.grid(row=1,column=1) 94 buttonMS.grid(row=1,column=2) 95 buttonM1.grid(row=1,column=3) 96 buttonM2.grid(row=1,column=4) 97 98 99 buttonJ=tkinter.Button(root,text='←',width=5,command=anjianzhi('c').tui) 100 buttonCE=tkinter.Button(root,text='CE',width=5) 101 buttonC=tkinter.Button(root,text=' C ',width=5,command=anjianzhi('c').clear) 102 button12=tkinter.Button(root,text='±',width=5,command=anjianzhi('c').zhengfu) 103 buttonD=tkinter.Button(root,text='√',width=5,command=anjianzhi('sqrt').yunshuan) 104 buttonJ.grid(row=2,column=0) 105 buttonCE.grid(row=2,column=1) 106 buttonC.grid(row=2,column=2) 107 button12.grid(row=2,column=3) 108 buttonD.grid(row=2,column=4) 109 110 button7=tkinter.Button(root,text=' 7 ',width=5,command=anjianzhi('7').jia) 111 button8=tkinter.Button(root,text=' 8 ',width=5,command=anjianzhi('8').jia) 112 button9=tkinter.Button(root,text=' 9 ',width=5,command=anjianzhi('9').jia) 113 buttonc=tkinter.Button(root, text=' / ',width=5,command=anjianzhi('/').yunshuan) 114 buttonf= tkinter.Button(root, text=' % ',width=5) 115 button7.grid(row=3,column=0) 116 button8.grid(row=3,column=1) 117 button9.grid(row=3,column=2) 118 buttonc.grid(row=3,column=3) 119 buttonf.grid(row=3,column=4) 120 121 button4=tkinter.Button(root,text=' 4 ',width=5,command=anjianzhi('4').jia) 122 button5=tkinter.Button(root,text=' 5 ',width=5,command=anjianzhi('5').jia) 123 button6=tkinter.Button(root,text=' 6 ',width=5,command=anjianzhi('6').jia) 124 buttonx=tkinter.Button(root,text=' * ',width=5,command=anjianzhi('*').yunshuan) 125 buttonfs=tkinter.Button(root,text='1/x',width=5,command=anjianzhi('1/x').yunshuan) 126 button4.grid(row=4,column=0) 127 button5.grid(row=4,column=1) 128 button6.grid(row=4,column=2) 129 buttonx.grid(row=4,column=3) 130 buttonfs.grid(row=4,column=4) 131 132 button1 = tkinter.Button(root, text=' 1 ',width=5,command=anjianzhi('1').jia) 133 button2 = tkinter.Button(root, text=' 2 ',width=5,command=anjianzhi('2').jia) 134 button3 = tkinter.Button(root, text=' 3 ',width=5,command=anjianzhi('3').jia) 135 button_= tkinter.Button(root, text=' - ',width=5,command=anjianzhi('-').yunshuan) 136 buttondy= tkinter.Button(root, text=' \n = \n ',width=5,command=anjianzhi('=').yunshuan) 137 button1.grid(row=5, column=0) 138 button2.grid(row=5, column=1) 139 button3.grid(row=5, column=2) 140 button_.grid(row=5, column=3) 141 buttondy.grid(row=5, column=4,rowspan=2) 142 143 button0=tkinter.Button(root,text=' 0 ',width=11,command=anjianzhi('0').jia) 144 buttonjh = tkinter.Button(root,text=' . ',width=5,command=anjianzhi('c').xiaoshudian) 145 buttonjia=tkinter.Button(root,text=' + ',width=5,command=anjianzhi('+').yunshuan) 146 button0.grid(row=6,column=0,columnspan=2) 147 buttonjh.grid(row=6,column=2) 148 buttonjia.grid(row=6,column=3) 149 def caidan(root): 150 151 menu=tkinter.Menu(root) 152 submenu1=tkinter.Menu(menu,tearoff=0) 153 menu.add_cascade(label='查看',menu=submenu1) 154 submenu2 = tkinter.Menu(menu, tearoff=0) 155 submenu2.add_command(label='複製') 156 submenu2.add_command(label='粘貼') 157 menu.add_cascade(label='編輯',menu=submenu2) 158 submenu = tkinter.Menu(menu, tearoff=0) 159 submenu.add_command(label='查看幫助') 160 submenu.add_separator() 161 submenu.add_command(label='關於計算機') 162 menu.add_cascade(label='幫助',menu=submenu) 163 root.config(menu=menu) 164 165 166 buju(root) 167 caidan(root) 168 root.mainloop()