【问题标题】:tkinter stretching widgets within a frametkinter 在框架内拉伸小部件
【发布时间】:2018-02-21 00:13:58
【问题描述】:

我目前正在尝试制作一个基本的 GUI,其中一个框架内的屏幕底部有 5 个按钮。我目前拥有的是一个框架中的 2 个按钮。框架被包装到底部。我正在尝试让这 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()

如果按钮不在框架中,我可以让它工作。当前发生的是两个按钮没有拉伸,并排放置在中间。我可以/应该怎么做才能让这些按钮展开?

【问题讨论】:

    标签: python user-interface tkinter raspbian


    【解决方案1】:

    这是因为两个 Button 小部件所在的 Frame 没有得到任何关于它应该占用多少父级的指令,因此它只是扩展到它可以包含的最小大小它的所有孩子。

    在此示例中,如果您希望它们仅在 x 轴上扩展(我假设您按照给它们一个静态高度进行操作),您需要将属性 fill="x" 添加到您的 @在 Frame bot_frame 上的 987654324@ 命令,并将属性 sticky="NESW" 添加到两个 .pack() 小部件上的两个 .pack() 命令。

    这将如下所示:

    ##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()
    

    以上应该可以达到你想要的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多