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、初級樣例:
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時控制按鈕的參數:
6、文本框tkinter.Entry,tkinter.Text控制參數:
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
?11、事件關聯
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 鼠標當前位置,相對于整個屏幕12、彈窗
messagebox._show函數的控制參數:default 指定消息框按鈕;icon 指定消息框圖標;message 指定消息框所顯示的消息;parent 指定消息框的父組件;title 標題;type 類型;simpledialog模塊參數:title 指定對話框的標題;prompt 顯示的文字;initialvalue 指定輸入框的初始值;filedialog 模塊參數:filetype 指定文件類型;initialdir 指定默認目錄;initialfile 指定默認文件;title 指定對話框標題colorchooser模塊參數:initialcolor 指定初始化顏色;title 指定對話框標題;13、字體(font)
一般格式:
(’Times -10 bold’)
(‘Times’,10,’bold’,’italic’)??? 依次表示字體、字號、加粗、傾斜
補充:
config??????????? 重新配置
label.config(font=’Arial -%d bold’ % scale.get())
依次為字體,大小(大小可為字號大小),加粗
tkinter.StringVar??? 能自動刷新的字符串變量,可用set和get方法進行傳值和取值,類似的還有IntVar,DoubleVar…
?
Python3 從入門到開車
?
附1:tkinter中的顏色
?
附2:一個自己用tkinter寫的計算器程序
1 #filename:Caculater2 3 import tkinter,time,decimal,math,string4 5 root=tkinter.Tk()6 root.title('計算器')7 root.resizable(0,0)8 global cuncu, vartext, result, fuhao9 result = fuhao = None10 vartext = tkinter.StringVar()11 cuncu = []12 13 class anjianzhi:14 global cuncu, vartext, result, fuhao15 def __init__(self,anjian):16 self.anjian = anjian17 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 = None27 fuhao = None28 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 pass40 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, fuhao47 if vartext.get() == '':48 pass49 else:50 get1 = decimal.Decimal(vartext.get())51 if self.anjian in ('1/x','sqrt'):52 if self.anjian == '1/x':53 result = 1/get154 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 + get262 elif fuhao == '-':63 result = get1 - get264 elif fuhao == '*':65 result = get1 * get266 elif fuhao == '/':67 result = get1 / get268 else:69 result = get170 if self.anjian == '=':71 fuhao = None72 else:73 fuhao = self.anjian74 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, fuhao85 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()計算器
?
標簽: python 好文要頂 關注我 收藏該文 覆手為云p關注 - 5
粉絲 - 25 +加關注 0 0 ? 上一篇:python3的正則表達式(regex)
? 下一篇:Python3組合數據類型(元組、列表、集合、字典)語法
</div>
總結
以上是生活随笔為你收集整理的Tkinter模块常用参数(python3)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新手也能看懂的 SpringBoot 异
- 下一篇: 拿到一台新的Windows电脑,我会做什