python - 添加框架来组织我的程序没有什么困难
问题描述
我正在使用此代码在我的 Tkinter 文本编辑器应用程序中的帧之间导航。当我想用多个框架组织页面以收集同一框架中的所有按钮和其他一些可能的框架中的其他小部件时,我得到了一个错误:_tkinter.TclError: cannot use geometry manager grid inside。它已经有由包管理的奴隶。如果你能帮助我,泰。
这是我使用的代码
import tkinter as tk
from tkinter import ttk
LARGE_FONT=("Verdana",12)
def popupmsg(msg):
popup=tk.Tk()
popup.wm_title("!")
label=ttk.Label(popup,text=msg,font=LARGE_FONT)
label.pack()
b1=ttk.Button(popup,text="OKAY",command=popup.destroy)
b1.pack()
popup.mainloop()
class MainWindow(tk.Tk):
def __init__(self,*arg,**kwargs):
tk.Tk.__init__(self,*arg,**kwargs)
container=tk.Frame(self)
container.pack(side="top",fill="both",expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
menubar=tk.Menu(container)
filemenu=tk.Menu(menubar,tearoff=0)
filemenu.add_command(label="Save settings", command=lambda:popupmsg("Not supported yet!"))
filemenu.add_command(label="Exit",command=quit)
menubar.add_cascade(label="File", menu=filemenu)
tk.Tk.config(self,menu=menubar)
self.frames={}
for F in(StartPage,PageOne):
frame=F(container,self)
self.frames[F]=frame
frame.grid(row=0,column=0,sticky="nsew")
self.show_frame(StartPage)
def show_frame(self,cont):
frame=self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
label_startpage=ttk.Label(self,text="Start Page",font=LARGE_FONT)
label_startpage.pack(padx=10,pady=10)
button_to_pageONE=ttk.Button(self,text="Go to Page ONE",command= lambda:
controller.show_frame(PageOne)).pack()
class PageOne(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.rowconfigure(0, minsize=200, weight=1)
self.columnconfigure(1, minsize=200, weight=1)
txt_edit = tk.Text(self).grid(row=0, column=1, sticky="nsew")
button_frame = tk.Frame(self,bg="lightblue").grid(row=0, column=0, sticky="ns")
btn_open = ttk.Button(button_frame, text="Open").grid(row=0, column=0, sticky="ew", padx=5, pady=5)
btn_save = ttk.Button(button_frame, text="Save As...").grid(row=1, column=0, sticky="ew", padx=5)
button_to_startpage = ttk.Button(button_frame, text="Back to Start Page",
command=lambda:
controller.show_frame(StartPage)).grid(row=2, column=0,
sticky="ew", padx=5,
pady=5)
app=MainWindow()
app.geometry("1280x720")
app.mainloop()
解决方案
你在这条线上犯了一个非常常见的错误:
button_frame = tk.Frame(self,bg="lightblue").grid(row=0, column=0, sticky="ns")
因为你在做的tk.Frame(...).grid(...)
,button_frame
是被设定的结果.grid(...)
。这使得button_frame
None
. 当您这样做btn_open = ttk.Button(button_frame, ...)
时,会将按钮作为根窗口的子级而不是button_frame
. 您正在pack
根窗口中使用,因此您不能grid
用于根窗口中的任何其他小部件。
解决方案是通过将小部件创建与小部件布局分开来正确创建button_frame
:
button_frame = tk.Frame(...)
button_frame.grid(...)
推荐阅读
- django - 如何在 django 传单地图中添加搜索栏
- go - 在干净的架构设置中使用 gorm 时是否需要持久性模型?
- python - python日志处理程序中的emit方法返回到哪里?
- swift - SwiftUI 更新 EnvironmentObject 中的编写优化
- python-3.x - 可以在 gpu 上运行 celery 吗?
- debian - 安装 Slack 时缺少 libappindicator3-1
- javascript - 如何在 React 中向引导多色进度条添加垂直标记?
- python - 使用熊猫(Python)从字符串矩阵构造计数矩阵(整数)的方法
- r - 如何从给定的 ggplot 对象中提取条形标签及其大小?
- python - Python中的简单TCP服务器不能在线工作