简体   繁体   English

Python tkinter 框架不会使用网格填充父容器

[英]Python tkinter frames do not fill parent container using Grid

I'm kind of stuck with this problem.我有点被这个问题困住了。 I am trying to build an application with several pages.我正在尝试构建一个包含多个页面的应用程序。 To that end, I create a main Frame and then I raise the frame I need in each moment.为此,我创建了一个主框架,然后每时每刻都提升了我需要的框架。 This is the code I wrote until now:这是我写到现在的代码:

class Main(tk.Tk):

def __init__(self, *args, **kwargs):

    tk.Tk.__init__(self, *args, **kwargs)

    main_container = tk.Frame(self)
    main_container.grid(sticky = "nsew")
    main_container.grid_rowconfigure(0, weight = 1)
    main_container.grid_columnconfigure(0, weight = 1)

    menu_bar = tk.Menu(main_container)
    file_menu = tk.Menu(menu_bar, tearoff = 0) 
    file_menu.add_command(label = "Save settings", command = lambda: popupmsg("Not supported yet!"))
    file_menu.add_separator()
    file_menu.add_command(label = "Exit", command = quit)
    menu_bar.add_cascade(label = "File", menu = file_menu)

    tk.Tk.config(self, menu = menu_bar)

    self.frames = {}

    for fr in (MainPage, GraphsPage, Page2):

        frame = fr(main_container, self)

        self.frames[fr] = frame

        frame.grid(row = 0, column = 0, sticky = "nsew")

    self.show_frame(MainPage)


def show_frame(self, pointer):

    frame = self.frames[pointer]
    frame.tkraise()

Then, MainPage, for example is:然后,MainPage,例如:

class MainPage(tk.Frame):

def __init__(self, parent, controller):

    tk.Frame.__init__(self, parent)

    #Tried this but it doesn't work
    #self.columnconfigure(0, weight = 1)
    #self.rowconfigure(0, weight = 1)
    #self.rowconfigure(1, weight = 1)
    #self.rowconfigure(2, weight = 1)
    #self.rowconfigure(3, weight = 1)

    label = tk.Label(self, text = "Main Page", font = LARGE_FONT)
    label.grid(row = 0, padx = 10, pady = 10)

    button1 = ttk.Button(self, text = "Graphs", command = lambda: controller.show_frame(GraphsPage))
    button1.grid(row = 1, sticky = 'nswe')

    button2 = ttk.Button(self, text = "Page 2", command = lambda: controller.show_frame(Page2))
    button2.grid(row = 2, sticky = 'nswe')

    button3 = ttk.Button(self, text = "Exit", command = quit)
    button3.grid(row = 3, sticky = 'nswe')

(I am not showing GraphsPage and Page 2 for simplicity). (为简单起见,我没有显示 GraphsPage 和 Page 2)。 Finally, I run the program as:最后,我将程序运行为:

app = Main()
app.geometry("1280x720")
app.mainloop()

And I am able to make the buttons fit the entire columns, but the columns do not fit the "1280x720" main container.而且我能够使按钮适合整个列,但这些列不适合“1280x720”主容器。 What am I missing?我错过了什么? Thanks in advance for your time.在此先感谢您的时间。

you had the right idea in the MainPage constructor, but then commented it out, but you missed doing it in the top level Tk object:您在MainPage构造函数中有正确的想法,但随后将其注释掉,但您没有在顶级Tk对象中执行此操作:

import tkinter as tk
from tkinter import ttk

LARGE_FONT = ("ariel", 20) # dont know what you had here

class Main(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        self.grid_rowconfigure(0, weight=1) # this needed to be added
        self.grid_columnconfigure(0, weight=1) # as did this

        main_container = tk.Frame(self)
        main_container.grid(column=0, row=0, sticky = "nsew")
        main_container.grid_rowconfigure(0, weight = 1)
        main_container.grid_columnconfigure(0, weight = 1)

        menu_bar = tk.Menu(main_container)
        file_menu = tk.Menu(menu_bar, tearoff = 0) 
        file_menu.add_command(label = "Save settings", command = lambda: popupmsg("Not supported yet!"))
        file_menu.add_separator()
        file_menu.add_command(label = "Exit", command = quit)
        menu_bar.add_cascade(label = "File", menu = file_menu)

        tk.Tk.config(self, menu = menu_bar)

        self.frames = {}

        for fr in (MainPage,):
            frame = fr(main_container, self)
            self.frames[fr] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")
        self.show_frame(MainPage)

    def show_frame(self, pointer):
        frame = self.frames[pointer]
        frame.tkraise()

class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # uncommented these lines
        self.columnconfigure(0, weight = 1)
        self.rowconfigure(0, weight = 1)
        self.rowconfigure(1, weight = 1)
        self.rowconfigure(2, weight = 1)
        self.rowconfigure(3, weight = 1)

        label = tk.Label(self, text = "Main Page", font = LARGE_FONT)
        label.grid(row = 0, padx = 10, pady = 10)

        button1 = ttk.Button(self, text = "Graphs", command = lambda: controller.show_frame(GraphsPage))
        button1.grid(row = 1, sticky = 'nswe')

        button2 = ttk.Button(self, text = "Page 2", command = lambda: controller.show_frame(Page2))
        button2.grid(row = 2, sticky = 'nswe')

        button3 = ttk.Button(self, text = "Exit", command = quit)
        button3.grid(row = 3, sticky = 'nswe')

app = Main()
app.geometry("1280x720")
app.mainloop()

To clarify, the reason why the GUI was not filling the entire window is because main_container (your main Tkinter object) has not been assigned a weight.澄清一下,GUI 没有填满整个窗口的原因是因为main_container (您的主要 Tkinter 对象)尚未分配权重。 In Tkinter, weight describes how much a row or column will grow if there is extra space.在 Tkinter 中,权重描述了如果有额外空间,行或列将增长多少。 By default, everything in Tkinter has a weight of 0 so nothing gets resized unless you modify stuff.默认情况下,Tkinter 中的所有内容的权重均为 0,因此除非您修改内容,否则不会调整大小。

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

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