简体   繁体   中英

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. 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...

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.

In your specific case, the root of the problem is that you have expand=YES for just about everything. As a general rule of thumb, you only want to set that to YES for one widget in an given parent window. 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. For the left side, add fill=BOTH for self.lb , and expand=NONE for lblabel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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