gpt4 book ai didi

python - 框架不与 pack() 堆叠

转载 作者:太空宇宙 更新时间:2023-11-03 14:16:09 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,用户可以使用“下一个”和“上一个”按钮在屏幕中导航。编辑所选答案的代码 here我生成了以下代码:

import Tkinter as tk
import tkFont as tkfont

def cancel():
root.destroy()

def disable_event():
pass

#Set the parent (main/root) window
root = tk.Tk()
root.title("Some title")
root.geometry('700x500') #Width x Height
root.resizable(0, 0) #Make root unresizable and force the use of "Cancel" button
root.protocol("WM_DELETE_WINDOW", disable_event)


class InstallerApp(tk.Tk):
def __init__(self, *args, **kwargs):

self.title_font = tkfont.Font(family='Helvetica', size=18)
container = tk.Frame(root)
container.pack(side="top", fill="both", expand=True)
self.frames = {}
for F in (Intro, FirstPage):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
self.show_frame("Intro")

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

class Intro(tk.Frame):
def __init__(self, parent, controller):
Intro = tk.Frame.__init__(self, parent)
self.controller = controller
leftFrame = tk.Frame(Intro, height=500, width=250, bg="#000000")
leftFrame.pack(side="left")
middleFrame = tk.Frame(Intro, height=500, width=5)
middleFrame.pack(side="left")
rightFrame = tk.Frame(Intro, height=500, width=450, bg="#FFFFFF")

buttonFrame = tk.Frame(Intro, height=35, width=450, bg="#FFFFFF")
nextButton = tk.Button(buttonFrame, width=10, text="Next >", command=lambda: controller.show_frame("FirstPage")).grid(row=0, column=0)
div3 = tk.Frame(buttonFrame, bg="#FFFFFF", width=10).grid(row=0, column=1)
cancelButton = tk.Button(buttonFrame, text="Cancel", width=10, command=cancel).grid(row=0,column=2)
buttonFrame.pack_propagate(False)
buttonFrame.pack(side="bottom")
#Other child widgets to rightFrame managed by pack

rightFrame.pack_propagate(False)
rightFrame.pack_forget()
rightFrame.pack(side="right")

class FirstPage(tk.Frame):
#the same code with "previousButton"

但是,当我执行代码时,我注意到即使调用 show_frame 函数,FirstPage 也不会出现,并且如果我调整主窗口的大小,它会逐渐从 Intro 后面出现。当我运行原始代码时,它运行得很好。

问题是因为我使用的是 pack() 管理器,而原始代码使用的是 grid() 还是什么?有人可以提供示例代码吗?

P.S.:我见过其他问题,但它们都使用grid()。我使用的是 python 2.7。

最佳答案

您根本无法使用 pack 将一个小部件覆盖在同一母版中的另一个小部件之上。这不是 pack 可以做到的。 pack 明确设计用于将小部件放置在同一母版中现有小部件的上方、下方或侧面的未分配空间中。

如果要将框架堆叠在一起,则需要使用 gridplace

关于python - 框架不与 pack() 堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48229580/

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