简体   繁体   English

Python Tkinter:调整框架大小的问题

[英]Python tkinter: issues resizing frames

I'm trying to resize a window in my GUI but one of my frames is getting left out and I'm not sure why. 我正在尝试在GUI中调整窗口大小,但是其中一个框架被忽略了,我不确定为什么。 The window resizes fine horizontally, but when I try to resize vertically the frame with the button disappears. 窗口在水平方向上可以调整大小,但是当我尝试在垂直方向上调整按钮时,框架消失。 This is my first GUI so I'm sure there is something I'm missing... 这是我的第一个GUI,所以我确定我缺少某些东西...

from Tkinter import *
from ttk import *

class GUI(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)

        self.root = root

        lbFrame = Frame(self.root)
        nbFrame = Frame(self.root)

        self.note = Notebook(nbFrame)
        self.note.pack(fill=BOTH, expand=YES)

        lbFrame.pack(side=LEFT, fill=BOTH, expand=YES)
        nbFrame.pack(side=RIGHT, fill=BOTH, expand=YES)

        self.make_file_viewer()

        # Label
        lblabel = Label(lbFrame, text='Files', background='#E8E8E8')
        lblabel.pack(side=TOP, expand=YES, padx=10, pady=10)

        # Listbox
        self.lb = Listbox(lbFrame, height=49, borderwidth=0, font=('Purisa', 11), selectmode=EXTENDED)
        self.lb.pack(side=BOTTOM, expand=YES, padx=10, pady=10)

    def make_file_viewer(self):
        fvwr = Frame(self.note)

        dataFrm = Frame(fvwr)
        btnFrm = Frame(fvwr)
        dataFrm.pack(side=TOP, fill=BOTH, expand=YES)
        btnFrm.pack(side=BOTTOM, fill=BOTH, expand=YES)

        fvwr.config(borderwidth=2)
        self.note.add(fvwr, text='File View')

        # Label
        self.lbl_fvwr_search = Label(dataFrm, text='Search Hits\t0', justify=LEFT)
        self.lbl_fvwr_search.pack(side=TOP, anchor=W, expand=YES)

        # Scrollbar
        scrollbar_fvwr = Scrollbar(dataFrm)
        scrollbar_fvwr.pack(side=RIGHT, fill=Y, expand=YES)

        # Textbox
        self.outputPanel_fvwr_text = Text(dataFrm, wrap='word', height=40, width=115, yscrollcommand=scrollbar_fvwr.set)
        self.outputPanel_fvwr_text.pack(side=LEFT, fill=BOTH, expand=YES)
        scrollbar_fvwr.config(command=self.outputPanel_fvwr_text.yview)

        # Start button
        viewBtn = Button(btnFrm, text='Start', width=8)
        viewBtn.pack(anchor=W, expand=YES)

if __name__ == '__main__':
    root = Tk()
    app = GUI(root)
    root.mainloop()

The absolute best thing you can do is to start over, and do your layout step-by-step. 您绝对可以做的最好的事情是重新开始,并逐步进行布局。 Start by creating the main areas, and make sure they resize properly. 首先创建主要区域,并确保它们正确调整大小。 In your case, create the left and right sides. 在您的情况下,请创建左侧和右侧。 Again, get those two sides resizing properly with respect to each other. 再次,使这两个方面彼此正确调整大小。

Once you are done, focus on one section. 完成后,专注于某一部分。 Since you know the main section resizes properly, you only need to focus on the elements within that particular side. 由于您知道主要部分的大小可以正确调整,因此您只需要关注该特定侧的元素即可。 Again, break it down into pieces, and get those pieces working before tackling any widgets inside the main pieces. 同样,将其分解成多个部分,并在处理主要部分中的任何小部件之前使这些部分工作。

When you do your layout this way, it's much easier to get the whole GUI working right, because you aren't trying to juggle the behavior of a half dozen widgets at once. 当您以这种方式进行布局时,使整个GUI正常工作容易得多 ,因为您不会尝试同时处理六个小部件的行为。

In your specific case, the root of the problem is that you have expand=YES for just about everything. 在您的特定情况下,问题的根源expand=YES几乎所有内容都具有expand=YES As a general rule of thumb, you only want to set that to YES for one widget in an given parent window. 通常,您只想将给定父窗口中的一个窗口小部件的设置为YES For example, in your main window you want the right to expand but not the left (I'm guessing), and in the right window you want the text widget to expand but not the other widgets. 例如,在主窗口中,您希望向右扩展而不是在左侧(我猜测是),在右窗口中,您希望扩展文本小部件,而不是其他小部件。

Set expand=NO for scrollbar_fvwr , self.lbl_fvwr_search , and btnFrm to get the right side to resize properly. scrollbar_fvwrself.lbl_fvwr_searchbtnFrm设置为expand=NO ,可以在右侧正确调整大小。 For the left side, add fill=BOTH for self.lb , and expand=NONE for lblabel . 对于左侧,添加fill=BOTHself.lb ,并expand=NONElblabel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM