Python编程:Tkinter图形界面设计(1)
一、Tkinter的程序框架
窗口的生成和窗口的呈現(xiàn),用Tk()和mainloop()包括起來,中間程序是附著在窗口的內(nèi)部部件。
?
?二、窗口內(nèi)容
窗口的內(nèi)容,是最豐富的細(xì)節(jié)包括:1、幾何尺寸 2、按鈕部件 3、...
2.1 窗口的幾何尺寸
1)重要操作函數(shù)
- 設(shè)置窗口的標(biāo)題:root.title('Python GUI Learning')
- 設(shè)置窗口大小:root.geometry('380x300')
- 設(shè)定窗口可以(不可)重resize窗口:root.resizable(width=False, height=True)
2)參考代碼
from tkinter import Tk #初始化Tk() root = Tk() #設(shè)置標(biāo)題 root.title('Python GUI Learning') #設(shè)置窗口大小 root.geometry('380x300') #設(shè)置窗口是否可變長、寬,True:可變,False:不可變 root.resizable(width=False, height=True) #進(jìn)入消息循環(huán) root.mainloop()2.2 窗口的擺放位置
如果擺放窗口位置,需要知道屏幕的長寬。
1)關(guān)鍵函數(shù)
- 獲取屏幕尺寸:screenwidth? = root.winfo_screenwidth()
? ? ? ? ? ? ? ? ? ? ? ? ?screenheight = root.winfo_screenheight()
- 設(shè)置窗口的大小位置:root.geometry('380x300+200+100')
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?( 長=380,高=300,左上角位置(200,100))
2.3? 實(shí)驗(yàn)代碼
from tkinter import Tk #初始化Tk() root = Tk() #設(shè)置標(biāo)題 root.title('Python GUI Learning') #設(shè)置窗口大小 width = 380 height = 300 #獲取屏幕尺寸以計(jì)算布局參數(shù),使窗口居屏幕中央 screenwidth = root.winfo_screenwidth() screenheight = root.winfo_screenheight() alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2) root.geometry(alignstr) #設(shè)置窗口是否可變長、寬,True:可變,False:不可變 root.resizable(width=False, height=True) #進(jìn)入消息循環(huán) root.mainloop()2.3 控件管理
1)常用控件種類
????????一般窗口并不能實(shí)現(xiàn)交互,交互需要控件完成。Tkinter 提供了各種控件,如按鈕、標(biāo)簽和文本框。在一個(gè) GUI 應(yīng)用程序中使用,這些控件通常被稱為控件或者部件,目前有19種Tkinter 部件,如下列表:
| 控件 | 描述 |
| button | 按鈕控件 |
| Canvas | 畫布,可以實(shí)現(xiàn)幾何元素的繪制; |
| Checkbutton | 選擇按鈕 |
| Entry | 就是其它語言的editer,文本輸入件 |
| Frame | 一個(gè)矩形區(qū)域,構(gòu)成控件容器 |
| Label | 顯示文字和圖片 |
| ListBox | 列表框 |
| MenuButton | 菜單工具條 |
| Menu | 菜單 |
| Message | 消息控件,顯示多行文本 |
| RadioButton | 單選按鈕 |
| Scale | 可拉動(dòng)數(shù)字游標(biāo)尺 |
| ScrollBar | 滾動(dòng)條 |
| Text | 顯示多行文本 |
| TopLevel | 容器,用來提供單獨(dú)對(duì)話框 |
| Spinbox | 輸入控件,與Entry類,但支持范圍 |
| panedWindow | 布局管理插件,上盛放控件 |
| labelFrame | 簡單容器 |
| tkMessageBox | 顯示應(yīng)用程序的消息框 |
?
2 控件擺放(?幾何管理)
????????Tkinter 控件有特定的幾何狀態(tài)管理方法,管理整個(gè)控件區(qū)域組織,以下是 Tkinter 公開
的幾何管理類:包、網(wǎng)格、位置。
| 幾何方法 | 描述 | 屬性說明 |
| pack() | 設(shè)置控件位置 | after:將組件置于其它組件之后 fill:填充方式(X垂直,X:水平) |
| grid() | 網(wǎng)格規(guī)劃 | column:組件所在的列起始位置 columnspam:組件列寬 row:組件起始位置 rowspam:組件的行寬 |
| place() | 位置 | anchor:組件對(duì)齊方式 x:組件左上角x坐標(biāo) y:組件左上角y坐標(biāo) relx:組件對(duì)于窗口的x相對(duì)坐標(biāo)(0,1)之間 rely:組件對(duì)于窗口的y相對(duì)坐標(biāo)(0,1)之間 width:組件寬度 height:組件高度 relwidth:組件相對(duì)窗口的寬度(0,1) relheight:組件相對(duì)窗口的高度(0,1) |
三、控件編程
3.1 Lable控件
????????標(biāo)簽控件,基本用法為:? Lable(root, option...) ,即:Label(根對(duì)象, [屬性列表]),
其中屬性列表如下:
| 可選屬性 | 說明 |
| text | 文本內(nèi)容 text=’登錄名稱‘ |
| bg | 背景顏色 |
| fg | 前景顏色 |
| font | 字體 |
| width | 寬度 |
| height | 高度 |
| padx | 水平邊距 |
| pady | 垂直邊距 |
| justify | 對(duì)齊方式 |
| image | 圖像文件路徑 |
| compound | 同一個(gè)區(qū)域顯示文字和圖片混合 |
?
Lable 控件實(shí)例
實(shí)例1:標(biāo)簽展示文本,代碼如下:
執(zhí)行結(jié)果:
??
實(shí)例2:標(biāo)簽展示圖標(biāo),代碼如下:
from tkinter import* #初始化Tk() root = Tk() #設(shè)置標(biāo)題 root.title('Python GUI Learning') #創(chuàng)建一個(gè)標(biāo)簽,顯示圖標(biāo) logo = PhotoImage(file="temp.gif") Label(root, image=logo).pack(side='left') #進(jìn)入消息循環(huán) root.mainloop()運(yùn)行結(jié)果:
實(shí)例3:標(biāo)簽圖文混疊,邊距控制,代碼如下:
from tkinter import* #初始化Tk() root = Tk() #設(shè)置標(biāo)題 root.title('Python GUI Learning') #創(chuàng)建一個(gè)標(biāo)簽,顯示文本 logo = PhotoImage(file="skyeL2.gif") explanation = """At present, only GIF and PPM/PGM formats are supported, but an interface exists to allow additional image file formats to be added easily.""" Label(root,compound=CENTER,text=explanation,image=logo).pack(side="right") #進(jìn)入消息循環(huán) root.mainloop()?運(yùn)行結(jié)果:
?
總結(jié)
以上是生活随笔為你收集整理的Python编程:Tkinter图形界面设计(1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 语音识别:时间序列Damerau–Lev
- 下一篇: Python编程:Tkinter图形界面