我似乎无法让tkinter网格重新调整大小

2024-10-02 02:43:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我花了几天时间试图让grid()正确调整窗口大小。如果我只使用pack(),我就可以调整它的大小,但是grid()也必须能够做到这一点,对吗

作为最后的手段,我重写了代码,只显示一个退出按钮。然而,当我调整窗口大小时,退出按钮就停留在角落里!使用pack(),随着车窗的展开,它将移动到车窗的中心

我已经添加了对grid_columnconfigure()grid_rowconfigure()的调用,但这似乎没有任何作用。有人有什么想法吗

谢谢

from Tkinter import *

class myBase(Tk):

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

        # By inheriting from Tk, this is already the "root":
        Tk.__init__(self, *args, **kwargs)


        # Build the base container, which itself is a frame:
        container = Frame(self)
        container.grid(row=0, column=0, sticky="eswn")
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)


        # This is the "series" of frames (pages):
        self.frames = {}

        # Create instances of each "page":
        for page in (myPage01, myPage02):
            frame = page(container, self)
            self.frames[page] = frame
            frame.grid(row=0, column=0, sticky="eswn")


        # This is the first page:
        self.showFrame(myPage01)


    def showFrame(self, pageObject):

        # Finds the correct frame (page), and "raises" it:
        frame = self.frames[pageObject]
        frame.tkraise()



class myPage01(Frame):

    def __init__(self, parent, objCall):

        # Inheriting from a frame, so initialize that part first:
        Frame.__init__(self, parent)


        # "Pointer" to the calling object:
        self.objCall = objCall


        # Create the widgets for a simple login screen:
        buttonQuit = Button(self, text="Quit", command=self.quit, width=10)


        # Place everything on the grid:
        buttonQuit.grid(row=0, column=0)


        # Shortcut to put some nice padding around each widget:
        for child in self.winfo_children():
            child.grid_configure(padx=5, pady=5)


        # Some key bindings:
        buttonQuit.bind("<Return>", self.hQuit)

    def hQuit(self, event):
        self.quit()


class myPage02(Frame):

    def __init__(self, parent, objCall):

        # Inheriting from a frame, so initialize that part first:
        Frame.__init__(self, parent)


        # "Pointer" to the calling object:
        self.objCall = objCall


        # Create the widgets for a simple login screen:
        buttonQuit = Button(self, text="Out", command=self.quit, width=10)


        # Place everything on the grid:
        buttonQuit.grid(row=0, column=0, sticky="W")


        # Shortcut to put some nice padding around each widget:
        for child in self.winfo_children():
            child.grid_configure(padx=5, pady=5)


        # Some key bindings:
        buttonQuit.bind("<Return>", self.hQuit)

    def hQuit(self, event):
        self.quit()



app = myBase()
app.mainloop()

Tags: thefromselfforinitiscontainerdef

热门问题