Python之tkinter面板PanedWindow的使用
PanedWindow的基本概念
PanedWindow可以翻譯為面板,是一個(gè)Widget 控件,可以在此容器內(nèi)建立任意數(shù)量的子控件,不過一般在此控件內(nèi)建立兩三個(gè)子控件,而控件是以水平方向或垂直方向排列。
它的構(gòu)造方法語法如下:
PanedWindow(父對象, options, ...)
參數(shù):
- 第一個(gè)參數(shù):父對象,表示這個(gè)面板將建立在哪一個(gè)窗口內(nèi)
- 第二個(gè)參數(shù):
options,參數(shù)如下
| 參數(shù) | 含義 |
|---|---|
| bg 或 background | 當(dāng)鼠標(biāo)光標(biāo)不在此控件上時(shí),若是有滾動(dòng)條或方向盒時(shí),滾動(dòng)條或方向盒的背景色彩 |
| bd | 3D顯示時(shí)的寬度默認(rèn)是2 |
| borderwidth | 邊界線寬度默認(rèn)是2 |
| cursor | 當(dāng)鼠標(biāo)光標(biāo)在標(biāo)簽上方時(shí)的形狀 |
| handlepad | 面板顯示寬度默認(rèn)是8 |
| handlesize | 面板顯示大小默認(rèn)是8 |
| height | 高度沒有默認(rèn)高度 |
| opaqueresize | 該選項(xiàng)定義了用戶調(diào)整窗格尺寸的操作如果該選項(xiàng)的值為 True(默認(rèn)),窗格的尺寸隨用戶鼠標(biāo)的拖拽而改變?nèi)绻撨x項(xiàng)的值為 False,窗格的尺寸在用戶釋放鼠標(biāo)的時(shí)候才更新到新的位置 |
| orient | 面板配置方向默認(rèn)是HORIZONTAL |
| relief | 可由此控制文字外框默認(rèn)是relief=FLAT |
| sashcursor | 分割線光標(biāo),沒有默認(rèn)值 |
| sashpad | 設(shè)置每一條分割線到窗格間的間距 |
| sashrelief | 面板分隔線外框默認(rèn)值是RAISED |
| sashwidth | 設(shè)置分割線的寬度 |
| showhandle | 滑塊屬性,可設(shè)定是否顯示默認(rèn)值為 False |
| width | 面板整體寬度,沒有默認(rèn)值 |
插入子控件 add()
add(child, options)
可以插入子控件
例子:
import tkinter panedWindow = tkinter.PanedWindow(orient=tkinter.VERTICAL) panedWindow.pack(fill=tkinter.BOTH, expand=True) # 創(chuàng)建標(biāo)簽Top top = tkinter.Label(panedWindow, text='Top') panedWindow.add(top) # 創(chuàng)建標(biāo)簽Bottom bottom = tkinter.Label(panedWindow, text='Bottom') panedWindow.add(bottom) panedWindow.mainloop()
運(yùn)行結(jié)果:

放大后

建立LabelFrame 當(dāng)做子對象
PanedWindow 是一個(gè)面板,最常的應(yīng)用是將它分成兩三份,然后可以將所設(shè)計(jì)的控件適度分配位置。
例子:
import tkinter root = tkinter.Tk() panedWindow = tkinter.PanedWindow(orient=tkinter.HORIZONTAL) panedWindow.pack(fill=tkinter.BOTH, expand=True) leftFrame = tkinter.LabelFrame(panedWindow, text='Left', width=120, height=150) panedWindow.add(leftFrame) centerFrame = tkinter.LabelFrame(panedWindow, text='Center', width=120, height=150) panedWindow.add(centerFrame) rightFrame = tkinter.LabelFrame(panedWindow, text='Right', width=120, height=150) panedWindow.add(rightFrame) panedWindow.mainloop()
運(yùn)行結(jié)果:

tkinter.ttk 模塊的weight 參數(shù)
如果我們單純的根據(jù)上面的程序進(jìn)行縮放,結(jié)果只有最右邊一個(gè)變大或者變小
圖片示例:

但是如果我們想要同比例縮放或放大呢?
我們可以在add()方法內(nèi)使用weight參數(shù),但是使用這個(gè)需要導(dǎo)入tkinter.ttk
例子:
from tkinter import * from tkinter.ttk import * root = Tk() panedWindow = PanedWindow(orient=HORIZONTAL) panedWindow.pack(fill=BOTH, expand=True) leftFrame = LabelFrame(panedWindow, text='Left', width=120, height=150) panedWindow.add(leftFrame, weight=1) centerFrame = LabelFrame(panedWindow, text='Center', width=120, height=150) panedWindow.add(centerFrame, weight=1) rightFrame = LabelFrame(panedWindow, text='Right', width=120, height=150) panedWindow.add(rightFrame, weight=1) panedWindow.mainloop()
運(yùn)行結(jié)果:

如果三個(gè)子對象設(shè)置不同的 weight,更改窗口大小時(shí),會(huì)產(chǎn)生不同的效果
例子:
from tkinter import * from tkinter.ttk import * root = Tk() panedWindow = PanedWindow(orient=HORIZONTAL) panedWindow.pack(fill=BOTH, expand=True) leftFrame = LabelFrame(panedWindow, text='Left', width=120, height=150) panedWindow.add(leftFrame, weight=2) centerFrame = LabelFrame(panedWindow, text='Center', width=120, height=150) panedWindow.add(centerFrame, weight=2) rightFrame = LabelFrame(panedWindow, text='Right', width=120, height=150) panedWindow.add(rightFrame, weight=1) panedWindow.mainloop()
運(yùn)行結(jié)果:

在PanedWindow 內(nèi)插入不同的控件
例子:
import tkinter panedWindow = tkinter.PanedWindow(orient=tkinter.HORIZONTAL) panedWindow.pack(fill=tkinter.BOTH, expand=True) entry = tkinter.Entry(panedWindow, bd=3) panedWindow.add(entry) # 在panedWindow內(nèi)創(chuàng)建PanedWindow子對象,名叫panedWindowIn panedWindowIn = tkinter.PanedWindow(panedWindow, orient=tkinter.VERTICAL) panedWindow.add(panedWindowIn) # 在panedWindowIn創(chuàng)建尺度條 scale = tkinter.Scale(panedWindowIn, orient=tkinter.HORIZONTAL) panedWindowIn.add(scale) panedWindow.mainloop()
運(yùn)行結(jié)果:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python虛擬機(jī)pyc文件結(jié)構(gòu)的深入理解
這篇文章主要為大家介紹了python虛擬機(jī)之pyc文件結(jié)構(gòu)的深入探究理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Python實(shí)現(xiàn)圖片指定位置加圖片水印(附Pyinstaller打包exe)
這篇文章主要介紹了Python實(shí)現(xiàn)圖片指定位置加圖片水印,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
python合并已經(jīng)存在的sheet數(shù)據(jù)到新sheet的方法
今天小編就為大家分享一篇python合并已經(jīng)存在的sheet數(shù)據(jù)到新sheet的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
matplotlib自定義鼠標(biāo)光標(biāo)坐標(biāo)格式的實(shí)現(xiàn)
這篇文章主要介紹了matplotlib自定義鼠標(biāo)光標(biāo)坐標(biāo)格式的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python接口自動(dòng)化淺析yaml配置文件原理及用法
本文主要給大家介紹了yaml語法、yaml儲存數(shù)據(jù),封裝類讀取yaml配置文件,以及yaml的用法和其原理,有需要的朋友可以參考下,希望可以有所幫助2021-08-08

