gpt4 book ai didi

python - tkinter 中的帧不堆叠在一起

转载 作者:太空狗 更新时间:2023-10-30 02:39:07 25 4
gpt4 key购买 nike

我在 tkinter 中将“页面”堆叠在一起时遇到问题。

我有一个主 Frame,它包含两个包含不同信息的子帧。第一个子框架包含一个 Listbox 和几个按钮,位于主框架的左侧。第二个框架应该包含不同的“页面”(现在是两个)并让它们填满整个框架。我的问题是两个“页面”并排显示,而不是彼此重叠显示。

import tkinter as tk


class Settings(tk.Tk):

def __init__(self, master=None):
tk.Tk.__init__(self, master)
self.focus_force()
self.grab_set()
# set focus to settings window
# Main window title
self.title("Settings")


# set up grid containers
container_main = tk.Frame(self, width=500, height=700)
container_main.pack(side='top', fill='both', expand=True)
container_main.grid_rowconfigure(0, weight=1)
container_main.grid_columnconfigure(0, weight=1)

container_listbox = tk.Frame(container_main, bg='blue', width=200, height=700)
container_listbox.pack(side='left', fill='both', expand=True)
container_listbox.grid_rowconfigure(0, weight=1)
container_listbox.grid_columnconfigure(0, weight=1)

container_settings = tk.Frame(container_main, bg='red', width=300, height=700)
container_settings.pack(side='right', fill='both', expand=True)
container_settings.grid_rowconfigure(0, weight=1)
container_settings.grid_columnconfigure(0, weight=1)

# build settings pages
self.frames = {}

self.frames["Options"] = Options(parent=container_listbox, controller=self)
self.frames["General"] = General(parent=container_settings, controller=self)
self.frames["Future"] = Future(parent=container_settings, controller=self)

如果我取消注释这两行。我收到一条错误消息,提示我无法在内部使用几何管理器网格。

        # self.frames["General"].grid(row=0, column=0, sticky='nsew')
# self.frames["Future"].grid(row=0, column=0, sticky='nsew')

.

    def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()


class Options(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(parent, text='List Box')
label.grid(row=0, column=0, sticky='nsew', padx=1, pady=1)
button1 = tk.Button(parent, text='General', command=lambda: controller.show_frame('General'))
button2 = tk.Button(parent, text='Future', command=lambda: controller.show_frame('Future'))
button1.grid(row=1, column=0, sticky='ew')
button2.grid(row=2, column=0, sticky='ew')


class General(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(parent, text='General')
label.pack(side='left', fill='both', expand=True, )
print("Hi I'm General")

class Future(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(parent, text='Future')
label.pack(side='left', fill='both', expand=True)
print("Hi I'm Future")

app = Settings()
app.mainloop()

两个“页面”同时初始化和显示,这是有道理的。我只是不知道如何让一个超越另一个,因为 frame.tkraise() 应该这样做,但事实并非如此。我还希望能够在一个或多个不在顶部的页面上执行 grid_forget(),以避免将来可能不小心将值输入到隐藏的输入框中。

编辑:如果我注释掉“ future ”页面,那么“常规”页面将占用整个框架空间,因此使用 grid_forget() 我会产生相同的结果。我只是不知道除了 grid_forget() 我会在哪里,然后我会在哪里重新配置或进行 grid() 调用?

最佳答案

My issue is that both 'Pages' are displayed side by side instead of on top of each other.

如果您使用 pack() 在您的根窗口上放置一个框架,然后在该框架内使用 grid() 那么它会起作用,但是如果您尝试在一个框架内使用 pack() 然后尝试在同一框架内使用 grid() 它将失败。

根窗口和框架也是如此。如果 pack() 根窗口中的一个框架,则您不能使用 grid() 将任何内容放入同一个根窗口中。

grid()pack() 的问题是因为类 General 和类 Future 的位置 正在配置标签小部件是使用 pack() 的父框架。这阻止了在同一个父框架中使用 grid() 来放置 General 框架和 Future 框架。

为了解决这个问题,我们改变了:

label = tk.Label(parent, text='General')

and

label = tk.Label(parent, text='Future')

到:

label = tk.Label(self, text='General')

and

label = tk.Label(self, text='Future')

以上是正常工作所需的唯一修复。

import tkinter as tk


class Settings(tk.Tk):

def __init__(self, master=None):
tk.Tk.__init__(self, master)
self.focus_force()
self.grab_set()
# set focus to settings window
# Main window title
self.title("Settings")

container_main = tk.Frame(self, width=500, height=700)
container_main.pack(side='top', fill='both', expand=True)
container_main.grid_rowconfigure(0, weight=1)
container_main.grid_columnconfigure(0, weight=1)

container_listbox = tk.Frame(container_main, bg='blue', width=200, height=700)
container_listbox.pack(side='left', fill='both', expand=True)
container_listbox.grid_rowconfigure(0, weight=1)
container_listbox.grid_columnconfigure(0, weight=1)

container_settings = tk.Frame(container_main, bg='red', width=300, height=700)
container_settings.pack(side='right', fill='both', expand=True)
container_settings.grid_rowconfigure(0, weight=1)
container_settings.grid_columnconfigure(0, weight=1)

self.frames = {}

self.frames["Options"] = Options(parent=container_listbox, controller=self)
self.frames["General"] = General(parent=container_settings, controller=self)
self.frames["Future"] = Future(parent=container_settings, controller=self)


self.frames["General"].grid(row=0, column=0, sticky='nsew')
self.frames["Future"].grid(row=0, column=0, sticky='nsew')

def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()

class Options(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(parent, text='List Box')
label.grid(row=0, column=0, sticky='nsew', padx=1, pady=1)
button1 = tk.Button(parent, text='General', command=lambda: controller.show_frame('General'))
button2 = tk.Button(parent, text='Future', command=lambda: controller.show_frame('Future'))
button1.grid(row=1, column=0, sticky='ew')
button2.grid(row=2, column=0, sticky='ew')


class General(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text='General')
label.pack(side='left', fill='both', expand=True)
print("Hi I'm General")

class Future(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text='Future')
label.pack(side='left', fill='both', expand=True)
print("Hi I'm Future")

app = Settings()
app.mainloop()

关于python - tkinter 中的帧不堆叠在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45329754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com