【问题标题】:Tkinter Grid WeirdnessTkinter 网格怪异
【发布时间】:2014-10-31 08:48:11
【问题描述】:

今天做了一个简单的基于网格的 Tkinter Frame 子类。它的主人是一个Tk() 对象。

root = Tk()
root.title('Rep Logger UI')
root.geometry('500x500')

mother = rep_ui(master=root)
mother.mainloop()

我想要做的只是将rep_ui 窗口划分成两个等宽的框架(红色、蓝色),然后在每个框架中放置一些小部件。

但我做的不对。红+蓝框没有完全填满窗口。要么他们没有完全填满rep_ui 类,或者rep_ui 类没有在它的主人(root)内正确扩展?

此外,蓝色和红色框架是不同的 width,尽管我使用相同的 weight-s 列配置了它们。

def __init__(self, master=None):
    Frame.__init__(self, master)

    """to make the rep_ui frame expand into the entire OS window."""

    self.master.rowconfigure(    0, weight = 1 )              # outer-bounds' row[0] weight set
    self.master.columnconfigure( 0, weight = 1 )              # outer-bounds' col[0] weight set

    self.grid(sticky=W+E+N+S)                                 # EWNS-sticky mode set

    self.columnconfigure(        0, weight = 1 )              # column[0] weight set
    self.columnconfigure(        1, weight = 1 )              # column[1] weight set

    self.rowconfigure(           0, weight = 1 )              #    row[0] weight set
    self.rowconfigure(           1, weight = 1 )              #    row[1] weight set


    """ left and right frames"""
    self.fl = Frame(self, bg="red")                           # RED  Frame ( left )
    self.fr = Frame(self, bg="blue")                          # BLUE Frame ( right )

    self.fl.grid( row=0, column=0,  sticky=N+S+W+E)           # .grid() RED  one
    self.fr.grid  row=0, column=1,  sticky=N+S+W+E)           # .grid() BLUE one

这是我描述的问题的图片:

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    为什么这些窗口的宽度不同?

    原因很明显:红框内有一个文本小部件。问题是您将weight 参数添加到了两个框架,但没有添加到小部件inside。对于文本,您可以使用类似

    self.fl.rowconfigure(0, weight=1)
    self.fl.columnconfigure(0, weight=1)
    

    为什么会出现这个空白?

    那是因为你在这里配置了第 1 行:

    self.rowconfigure(1, weight=1)
    

    虽然没用过,但还是会出现。删除或注释掉此行以使其正常工作。

    【讨论】:

      【解决方案2】:

      weight 参数仅影响 额外 空间的分配方式。 IE。网格管理器首先对所有“孙子”小部件进行网格化,然后如果有多余的空间,则根据提供的权重将其分配给子小部件。 (documented here: weight 参数“在分配额外空间时给出该列或行的相对权重”)

      视觉解释:

      至于底部的空白,我认为我们需要查看其余代码才能弄清楚。

      【讨论】:

        【解决方案3】:

        您可以尝试使用place几何管理器:

        self.fl.place( relx = 0.0, rely = 0.0, relwidth = 0.5, relheight = 1.0 )
        self.fr.place( relx = 0.5, rely = 0.0, relwidth = 0.5, relheight = 1.0 )
        

        使用父窗口小部件的相对比例是相当强大的,但仍然可以简单地编码和验证,并且在重新缩放 UI 时不会过时,并且不会受到复杂 UI 设计在其过程中可能遇到的其他工件和意外副作用的影响生命周期。

        root = Tkinter.Tk()
        aLF  = Tkinter.Frame(  root, bg = "red" )
        aRF  = Tkinter.Frame(  root, bg = "blue")
        aLF.place(                   relx = 0.0,
                                         rely = 0.0, relwidth = 0.5, relheight = 1.0 )
        aRF.place(                   relx = 0.5,
                                         rely = 0.0, relwidth = 0.5, relheight = 1.0 )
        aTextR = Tkinter.Label( aRF, bg = "white", text = "aFR := frame right" )
        aTextR.place(                relx = 0.0,
                                         rely = 0.0 )
        aTextL = Tkinter.Label( aLF, bg = "white", text = "aFL := frame left" )
        aTextL.place(                relx = 0.0,
                                         rely = 0.0 )
        root.title( 'Rep Logger UI')
        root.geometry( '500x500' )
        root.lift()
        

        并交叉验证

        root.geometry( '500x500' )
        root.geometry( '300x100' )
        root.geometry( '400x300' )
        root.geometry( '600x150' )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-13
          • 1970-01-01
          • 2018-08-29
          • 1970-01-01
          • 1970-01-01
          • 2016-07-30
          相关资源
          最近更新 更多