我花了最近几天试图让grid()正确调整窗口大小。如果我只使用pack(),我已经让它调整大小,但是grid()也必须能够做到这一点,对吧?
作为最后的手段,我已经重写了我的代码,只显示了一个退出按钮。然而,当我调整窗口大小时,Quit按钮就停留在角落里!使用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()