如何在 tkinter 框架中将列和行设置为 window 的大小?

How to Set Columns and Rows to be size of window in tkinter frame?

因此,我正在寻找一种方法,使我的按钮列和行占据“StartPage”上的所有可用 space。我环顾四周,我似乎能找到的是不在 tk.Frame 中使用 columnconfigure 的方法,但是当我将它用作框架时似乎不起作用,因为我得到“AttributeError:'function'当我 运行 代码时,对象没有属性 'rowconfigure'”。 这是我的代码:

import tkinter as tk
import time
import tkinter.font as tkFont
import time
import os
from tkinter import *
class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(StartPage)

    def switch_frame(self, frame_class):
        """Destroys current frame and replaces it with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()


class StartPage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        def admin_pass_check():
            global top
            top= Toplevel(app)
            top.geometry("200x100")
            label = Label(top,text="Input Password")
            label.pack()
            password = Entry(top,show="*",width=20)
            password.pack()
            def check():
                global valid
                pswd = password.get()
                if pswd == "yes":
                    valid = True
                    top.destroy()
                else:
                    valid = False
            button= Button(top, text="Submit", command=check)
            button.pack(pady=5, side= TOP)
        def final_check():
            admin_pass_check()
            app.wait_window(top)
            if not valid == None:
                if valid == True:
                    print("Password Was Correct")
                    master.switch_frame(Admin)

        frame = tk.Frame(self)## This is the bit I need help with
        self.grid_columnconfigure(0, weight=1)##


        label = tk.Label(self, text="This is the start page")
        user = tk.Button(self, text="User", command=lambda: master.switch_frame(User))
        admin = tk.Button(self, text="Admin", command=final_check)
        label.grid(row=0,column=0,sticky="NSEW")
        user.grid(row=1,column=0,sticky="NSEW")
        admin.grid(row=2,column=0,sticky="NSEW")

class User(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.master = master
        self.pack(fill=BOTH, expand=1)

        page_1_label = tk.Label(self, text="This is page one")
        start_button = tk.Button(self, text="Return to start page", command=lambda: master.switch_frame(StartPage))
        page_1_label.pack(side="top", fill="x", pady=10)
        start_button.pack()


class Admin(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        page_2_label = tk.Label(self, text="This is page two")
        start_button = tk.Button(self, text="Return to start page", command=lambda: master.switch_frame(StartPage))
        page_2_label.grid(row=0,column=0,sticky="NSEW")
        start_button.grid(row=1,column=0,sticky="NSEW")
        start_button.grid()



app = App() 
app.geometry("500x500")
app.mainloop()

这是因为框架 StartPage 没有填充其父框架 space。调用pack()时需要加上fill='both', expand=1:

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(StartPage)

    def switch_frame(self, frame_class):
        """Destroys current frame and replaces it with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack(fill='both', expand=1) # use all space of parent

另请注意,您不需要在 User.__init__() 内调用 self.pack(fill=BOTH, expand=1)