簡體   English   中英

Python Tkinter:調整框架大小的問題

[英]Python tkinter: issues resizing frames

我正在嘗試在GUI中調整窗口大小,但是其中一個框架被忽略了,我不確定為什么。 窗口在水平方向上可以調整大小,但是當我嘗試在垂直方向上調整按鈕時,框架消失。 這是我的第一個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()

您絕對可以做的最好的事情是重新開始,並逐步進行布局。 首先創建主要區域,並確保它們正確調整大小。 在您的情況下,請創建左側和右側。 再次,使這兩個方面彼此正確調整大小。

完成后,專注於某一部分。 由於您知道主要部分的大小可以正確調整,因此您只需要關注該特定側的元素即可。 同樣,將其分解成多個部分,並在處理主要部分中的任何小部件之前使這些部分工作。

當您以這種方式進行布局時,使整個GUI正常工作容易得多 ,因為您不會嘗試同時處理六個小部件的行為。

在您的特定情況下,問題的根源expand=YES幾乎所有內容都具有expand=YES 通常,您只想將給定父窗口中的一個窗口小部件的設置為YES 例如,在主窗口中,您希望向右擴展而不是在左側(我猜測是),在右窗口中,您希望擴展文本小部件,而不是其他小部件。

scrollbar_fvwrself.lbl_fvwr_searchbtnFrm設置為expand=NO ,可以在右側正確調整大小。 對於左側,添加fill=BOTHself.lb ,並expand=NONElblabel

暫無
暫無

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

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