基于tkinter中ttk控件的width-height設(shè)置方式
ttk控件的width-height設(shè)置
tkinter中窗口,框架容器,控件如果不設(shè)置寬度高度,則會(huì)根據(jù)其中的內(nèi)容自動(dòng)設(shè)置寬度高度
框架容器設(shè)置了寬度高度,如果其內(nèi)存在控件,則寬度高度失效,自動(dòng)適應(yīng)控件大小
如果窗口,框架容器,控件中沒有內(nèi)容,寬度和高度為默認(rèn)的最小尺寸(可能看不到)
頂級(jí)窗口
頂級(jí)窗口:高度寬度,單位像素點(diǎn)
import tkinter as tk from tkinter import ttk win = tk.Tk() win1 = tk.Toplevel() win.geometry('600x600') #設(shè)置頂級(jí)窗口寬度高度,單位像素點(diǎn) win1.geometry('600x600') #設(shè)置頂級(jí)窗口寬度高度,單位像素點(diǎn) #筆記本屏幕分辨率如果是1366x768,那么geometry('1366x768')鋪滿整個(gè)屏幕(當(dāng)然不建議)
框架容器
Frame,LabelFrame,PanedWindow:高度寬度,單位像素點(diǎn)
win = tk.Tk() win.title("TTK_GUI") win.geometry("600x600+10+10") s = ttk.Style() s.configure('1.TFrame',background='DarkGray') frame1 = ttk.Frame(win,width=500,height=100,style='1.TFrame') frame1.pack(pady=10) s.configure('2.TLabelframe') labelframe1 = ttk.LabelFrame(win,text='labelframe1',width=500,height=100, style='2.TLabelframe') labelframe1.pack(pady=10) s.configure('2.TPanedwindow',background='DarkGray') panedwindow1 = ttk.PanedWindow(win,width=500,height=100, style='2.TPanedwindow') panedwindow1.pack(pady=10)
控件-Label
內(nèi)容為文本,寬度單位:字符單位;內(nèi)容為圖片,顯示原始大小,忽略寬度設(shè)置;
內(nèi)容為圖文混合(【圖片寬度-像素點(diǎn)】小于【width值-字符單位】),寬度單位:字符單位,
圖片尺寸過大則寬度設(shè)置失效
import tkinter as tk from tkinter import ttk import tkinter.font as tkFont s = ttk.Style() image1 = tk.PhotoImage(file="001.png") #PhotoImage支持gif,png格式 ft1 = tkFont.Font(family='Consolas',size=10,weight='bold') s.configure('1.TLabel',font=ft1,background='DarkGray',foreground='red', width=40,anchor='center',justify='left',padding=[2,0,2,0]) label1 = ttk.Label(win,text='第一行文字word \nThis is second row',style='1.TLabel') label1.pack() label2 = ttk.Label(win,text='標(biāo)簽23',image=image1,compound='center',style='1.TLabel') label2.pack(pady=2) #anchor='center',justify='left' # 整體居中,多行文本左對(duì)齊
控件-Button類
Button,Checkbutton,Radiobutton,Menubutton:
內(nèi)容為文本,寬度單位:字符單位;內(nèi)容為圖片,顯示原始大小,忽略寬度設(shè)置;
內(nèi)容為圖文混合(【圖片寬度-像素點(diǎn)】小于【width值-字符單位】),寬度單位:字符單位,
圖片尺寸過大則寬度設(shè)置失效
ft1 = tkFont.Font(family='Consolas',size=10,weight='bold') image1 = tk.PhotoImage(file="001.png") #PhotoImage支持gif,png格式 s.configure('1.TButton',foreground='blue',justify='center',width=40,font=ft1) b1 = ttk.Button(win,text='按鈕',style='1.TButton') b1.pack(pady=2) b2 = ttk.Button(win,text='按鈕',image=image1,compound='center',style='1.TButton') b2.pack(pady=2) b3 = ttk.Button(win,image=image1) b3.pack(pady=2) #忽略寬度高度設(shè)置,自動(dòng)顯示原始大小圖片, #若圖片尺寸超過button父控件寬度高度,只顯示部分圖片
控件-Entry
Entry:內(nèi)容文本,寬度單位:字符單位 ,average-size characters of the widget's font
s.configure('1.TEntry',padding=[2,0,2,0]) e1 = ttk.Entry(win,textvariable=etxt,foreground='red',width=40,font=ft1, justify='center',style='1.TEntry') e1.pack(pady=2)
控件-Combobox
width設(shè)置長度,寬度單位:average-size characters of the widget's font;高度單位:行
val1=[1,2,3,4] s.configure('1.TCombobox',padding=[-5,0,-5,0]) cc1 = ttk.Combobox(win,width=40,height=3,values=val1,style='1.TCombobox') cc1.pack(pady=2)
控件-Spinbox
width設(shè)置長度,寬度單位:average-size characters of the widget's font;
val2=['AVC','x264','345f','FRD6hy'] sp1 = ttk.Spinbox(win,values=val2,width=39, foreground='red',justify='center',font=ft1) sp1.pack(pady=2)
控件-Scale
length設(shè)置長度,單位像素點(diǎn)
ttk中Scale屬性(無showvalue,resolution,label),可用性不如tk,建議不要使用ttk.Scale
控件-Progressbar
length設(shè)置長度,單位像素點(diǎn)
prbar = ttk.Progressbar(win,length=300) prbar.pack(pady=2)
控件-Notebook
寬度,高度,單位像素點(diǎn)
labelframe1 = ttk.LabelFrame(win,text='labelframe1',width=500,height=100) labelframe1.pack(pady=2) tabControl = ttk.Notebook(labelframe1,width=300,height=80) tabControl.pack(expand=1,fill='both') tab1 = ttk.Frame(tabControl) tabControl.add(tab1,text='Tab 1') tab2 = ttk.Frame(tabControl) tabControl.add(tab2,text='Tab 2')
控件-Scrollbar
無長度寬度設(shè)置,在pack(fill='x')或pack(fill='y')
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python讀取tif圖片時(shí)保留其16bit的編碼格式實(shí)例
今天小編就為大家分享一篇python讀取tif圖片時(shí)保留其16bit的編碼格式實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python lxml解析HTML并用xpath獲取元素的方法
今天小編就為大家分享一篇Python lxml解析HTML并用xpath獲取元素的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python接口自動(dòng)化淺析如何處理動(dòng)態(tài)數(shù)據(jù)
本文是python接口自動(dòng)化系列文章,主要介紹了接口自動(dòng)化過程中,動(dòng)態(tài)數(shù)據(jù)如何生成、動(dòng)態(tài)數(shù)據(jù)與數(shù)據(jù)庫數(shù)據(jù)進(jìn)行對(duì)比并替換,有需要的朋友可以參考下2021-08-08Python+Opencv實(shí)現(xiàn)數(shù)字識(shí)別的示例代碼
這篇文章主要介紹了Python+Opencv實(shí)現(xiàn)數(shù)字識(shí)別的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03python_array[0][0]與array[0,0]的區(qū)別詳解
今天小編就為大家分享一篇python_array[0][0]與array[0,0]的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02python opencv實(shí)現(xiàn)圖像矯正功能
這篇文章主要為大家詳細(xì)介紹了python opencv實(shí)現(xiàn)圖像矯正功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Django連接數(shù)據(jù)庫并實(shí)現(xiàn)讀寫分離過程解析
這篇文章主要介紹了Django連接數(shù)據(jù)庫并實(shí)現(xiàn)讀寫分離過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11