簡體   English   中英

tkinter拉伸框架內的小部件

[英]tkinter stretching widgets within a frame

我目前正在嘗試制作一個基本的GUI,其中5個按鈕固定在框架底部的屏幕底部。 我目前只有一個框架中的2個按鈕。 框架裝在底部。 我試圖讓那兩個按鈕填充框架。

##IMPORTS--------------------------------------------
from tkinter import *
import tkinter.font
##GUIWindow------------------------------------------
window = tkinter.Tk()                   
screen_w = window.winfo_screenwidth()  
screen_h = window.winfo_screenheight()
window.geometry('%dx%d' % (screen_w, screen_h))
window.title("betaV00")
myFont = tkinter.font.Font(family = 'Helvetica', size = 12, weight = "bold")
##WIDGET_BottomButtons---------------------------------
h=6
bot_frame = Frame(window, height = int(screen_h/10), width = int(screen_w))
bot_frame.pack(side=BOTTOM)

btn_HOME = Button(bot_frame, text="Home", font=myFont, bg='green', fg='white', height=h)
btn_HOME.grid(row = 0, column = 0)
btn_LEDS = Button(bot_frame, text="LEDS", font=myFont, bg='black', fg='green', height=h)
btn_LEDS.grid(row = 0, column = 1)

bot_frame.columnconfigure(0, weight=1)
bot_frame.columnconfigure(1, weight=1)
##-----------------------------------------------------

window.mainloop()

如果按鈕不在框架中,我可以使它工作。 當前正在發生的事情是兩個按鈕沒有伸展,並排放置在中間。 要使這些按鈕展開,我可以/應該怎么做?

這是因為沒有為兩個Button Widget所在的Frame提供任何有關其應該占據其父級多少的指令,因此它只是擴展到可以容納其所有子級的最小大小。

在此示例中,如果您希望它們僅在x軸上擴展(我假設您這樣做是因為給它們提供了靜態高度),則需要在.pack()添加屬性fill="x" .pack()在命令Frame bot_frame和屬性添加sticky="NESW"到兩個的.pack()在兩個命令Button部件。

如下所示:

##IMPORTS--------------------------------------------
from tkinter import *
import tkinter.font
##GUIWindow------------------------------------------
window = tkinter.Tk()                   
screen_w = window.winfo_screenwidth()  
screen_h = window.winfo_screenheight()
window.geometry('%dx%d' % (screen_w, screen_h))
window.title("betaV00")
myFont = tkinter.font.Font(family = 'Helvetica', size = 12, weight = "bold")
##WIDGET_BottomButtons---------------------------------
h=6
bot_frame = Frame(window)
bot_frame.pack(side=BOTTOM, fill="x") #here

btn_HOME = Button(bot_frame, text="Home", font=myFont, bg='green', fg='white', height=h)
btn_HOME.grid(row = 0, column = 0, sticky="NESW") #here
btn_LEDS = Button(bot_frame, text="LEDS", font=myFont, bg='black', fg='green', height=h)
btn_LEDS.grid(row = 0, column = 1, sticky="NESW") #and here

bot_frame.columnconfigure(0, weight=1)
bot_frame.columnconfigure(1, weight=1)
##-----------------------------------------------------

window.mainloop()

以上應該可以達到您想要的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM