在页面框架中,我已将两个框架加权到内部框架中,使它们各占屏幕的一半,但是当我向其中一个框架添加小部件时,我以列表框为例,因为其大小较大,其中一个框架现在占用了比另一个更多的空间。如何使框架在添加小部件时不改变大小,且每个都保持窗口的一半大小?
这是我的代码:
import tkinter as tk
from tkinter import ttk
class Program(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default = "")
tk.Tk.wm_title(self, "")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (Page, Other):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(Page)
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
class Page(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
innerFrame = tk.Frame(self, bg="red")
innerFrame.place(relx=.5, rely=.5, anchor="c", relwidth=1.0, relheight=1.0)
innerFrame.grid_rowconfigure(0, weight=1)
innerFrame.grid_rowconfigure(1, weight=1)
innerFrame.grid_columnconfigure(0, weight=1)
#First Half
frameOne = tk.Frame(innerFrame, bg="pink")
frameOne.grid(row=0, sticky="NSWE")
#Second Half
frameTwo = tk.Frame(innerFrame, bg="green")
frameTwo.grid(row=1, sticky="NSWE")
lb = tk.Listbox(frameTwo)
lb.pack(fill="both", expand=True)
class Other(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
app = Program()
app.state('zoomed')
app.mainloop()
innerFrame
上首先放置它,然后使用grid配置。我通常只使用grid,但也许有经验的使用place和pack的人可以详细说明。 - oystein-hr