获取嵌套框架以填充已经填充的父单元内的剩余窗口空间

时间:2019-04-12 22:50:10

标签: python tkinter

有人可以帮我嵌套框填充父窗口中的剩余窗口空间吗? 我创建了应用程序窗口,为其提供了一个容器,并根据存在的页面数为容器提供了2行和动态列。我的容器是父母。 通过将权重1赋予第二行,并使第一列扩展为所有可用列,我已经能够用父单元格填充窗口。然后,我将嵌套框架(这是页面)插入第二行,在第一行中每个页面的按钮下方。但是,即使删除所有网格用法,我似乎也对打包和网格层次结构感到麻烦。就像tk.Tk()在调用时由网格自动控制。

第41行似乎是我可以在页面上进行填充和扩展的地方。但是,即使删除了所有网格用法并用pack替换了必要的网格,它也会导致层次结构错误。通过使用网格,在没有此行的情况下运行良好。只是无法填充/展开

import tkinter as tk
from tkinter import font as tkfont


class Application(tk.Tk):

    def __init__(self, title, page_list):
        tk.Tk.__init__(self)

        self.minsize(500, 500)
        self.maxsize(500, 500)
        self.update_idletasks()
        self.title_font = tkfont.Font(family="Helvetica", size=10, weight="bold", slant="italic")

        self.title(title)

#    Create the main container. Row 1 holds page buttons. Row 2 is for the page frame.
        container = tk.Frame(self)
        container.configure(bg="red")
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(1, weight=1)
        container.grid_columnconfigure(len(page_list), weight=1)

#    Begin making the pages and put them in a dictionary to grab by name.
        self.pages = {}
        self.p_names = page_list
        for P in self.p_names:
            page_name = P.__name__
            page = P(parent=container, controller=self, title=page_name.strip("Page"))
            self.pages[page_name] = page
#    Make the buttons from the page list provided
        self.create_buttons(container, self)

        self.show_page(str(self.p_names[0].__name__), self.currentPage)

    def show_page(self, page_name, button):
        self.currentPage.configure(relief="raised", bg="red", fg="black")
        page = self.pages[page_name]

#    Put the page in row 1 below the buttons, in the top left. Expand it so it fills the remainder of the cell/window
        #page.pack(side="top", fill="both", expand="true") #This line will cause error when uncommented.
        page.grid(row=1, columnspan=len(self.pages), sticky="nw")

        for p in self.pages:
            if p != page_name:
                self.pages[p].grid_forget()
        button.configure(relief="sunken", bg="black", fg="white")
        self.currentPage = button

    def create_buttons(self, s, controller):
        pgs = len(self.p_names)*3
#    need to figure out how to accurately get window width and use it properly for button width division
        pages = {}

        for i,p in enumerate(self.p_names):
            pages[i] = tk.Button(s, text=p.__name__.strip("Page"), fg="Black", bg="red",
                                 width=(self.winfo_reqwidth() // pgs) + 1,
                                 command=lambda i=i, p=p: controller.show_page(p.__name__, pages[i]))
            pages[i].grid(row=0, column=i)
        self.currentPage = pages[0]


class NewPage(tk.Frame):
    def __init__(self, parent, controller, title):
        self.title = title
        tk.Frame.__init__(self, parent, bg="Black")
        self.controller = controller

#    Create Page Title Label
        pageTitle = tk.Label(self, text=self.title, font=controller.title_font, bg="Black", fg="white")
#    Attempt to fill cell that page is in
        pageTitle.pack(fill="both", expand="true")
        pageTitle.grid_columnconfigure(1, weight=1)
        pageTitle.grid_rowconfigure(1, weight=1)
#    Pad the page title
        pageTitle.grid(row=0, column=0, pady=10, padx=10, sticky="nsew")


        #extraLabel = tk.Label(self, text="test", bg="blue")
        #extraLabel.grid(row=1, column=1, pady=5, padx=5, sticky="nsew")


class CharacterPage(NewPage):
    def __init__(self, parent, controller, title):
        super().__init__(parent, controller, title)


class OptionsPage(NewPage):
    def __init__(self, parent, controller, title):
        super().__init__(parent, controller, title)


class InventoryPage(NewPage):
    def __init__(self, parent, controller, title):
        super().__init__(parent, controller, title)


class RandomPage(NewPage):
    def __init__(self, parent, controller, title):
        super().__init__(parent, controller, title)


def main():
    app = Application("Game Title Here", [CharacterPage, InventoryPage, OptionsPage])
    app.mainloop()


main()

应该发生的是页面,因为它的背景是黑色,所以应该用黑色覆盖单元格/窗口的其余部分。对我来说,着色是一种视觉效果,而不仅仅是使背景变黑。该页面最终将填充其他元素。

我缺少关于包装或网格的东西吗?我对使用它们还很陌生。大约一周或两周。 在嵌套帧填充中找不到任何内容。

1 个答案:

答案 0 :(得分:0)

不能在同一容器中使用包和网格。

尝试为页面提供不同的背景色,以便您可以看到页面的时间:

class NewPage(tk.Frame):
    def __init__(self, parent, controller, title):
        self.title = title
        tk.Frame.__init__(self, parent, bg="tan")    # bg='tan'

您将看到页面位于左上角。在页面中间或页面上是标签pageTitle

使用sticky="nwse"展开页面以填充单元格。