gpt4 book ai didi

python - 如何修复不工作的 Tkinter 嵌套框架;它不允许小部件

转载 作者:行者123 更新时间:2023-12-01 07:38:18 24 4
gpt4 key购买 nike

我正在制作一个 D&D 角色随机化器,已完成,但我一直在尝试将我原来的单页 GUI 切换到更宽敞、更具包容性的多页 GUI。我制作了 5 个框架,通过升高和下降来创建这种效果,效果很好,但是当我嵌套一个新框架并尝试将小部件绘制到该框架中时,它不起作用。

我在 Visual Studio 2017 上运行 Anaconda 3.4。

import tkinter as tk
from cache import *
class interface(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("D&D Character Randomizer")
#c = tk.Canvas(self, height=boundary_height, width=boundary_width).grid()
frames = (genPage, invPage, featPage, combatPage, spellPage)
#all frames need to be initialized before the UI's are built
self.frames = {}
for F in frames:
frame = F(self)
frame.grid(row=0, column=0, sticky="nsew")
self.frames[F] = frame
for F in self.frames.values():
F.build_UI()
self.frames[genPage].tkraise()
def topborder(self, master):
genButton = tk.Button(master, text="General", width=boundary_width//wc, height=boundary_height//hc, bg=color_frame['gp'], fg="#ffffff", command=self.parent.frames[genPage].tkraise, font=button_font).grid(column=0, row=0, padx=button_pad_x, pady=button_pad_y)
invButton = tk.Button(master, text="Inventory", width=boundary_width//wc, height=boundary_height//hc, bg=color_frame['ip'], fg="#ffffff", command=self.parent.frames[invPage].tkraise, font=button_font).grid(column=1, row=0, padx=button_pad_x, pady=button_pad_y)
featButton = tk.Button(master, text="Features", width=boundary_width//wc, height=boundary_height//hc, bg=color_frame['fp'], fg="#ffffff", command=self.parent.frames[featPage].tkraise, font=button_font).grid(column=2, row=0, padx=button_pad_x, pady=button_pad_y)
combatButton = tk.Button(master, text="Combat Stats", width=boundary_width//wc, height=boundary_height//hc, bg=color_frame['cp'], fg="#ffffff", command=self.parent.frames[combatPage].tkraise, font=button_font).grid(column=3, row=0, padx=button_pad_x, pady=button_pad_y)
spellButton = tk.Button(master, text="Spells", width=boundary_width//wc, height=boundary_height//hc, bg=color_frame['sp'], fg="#ffffff", command=self.parent.frames[spellPage].tkraise, font=button_font).grid(column=4, row=0, padx=button_pad_x, pady=button_pad_y)
generation = tk.Button(master, text="Generate", width=boundary_width//wc-4, height=boundary_height//hc, bg="#000000", fg="#ffffff", command=generate, font=button_font).grid(column=5, row=0, padx=button_pad_x)
#cross-class interface
class genPage(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent, bg=color_frame['gp'], height=boundary_height, width=boundary_width)
self.parent=parent
self.grid()
def build_UI(self):
#init topborder
interface.topborder(self, self)
self.render_frame_meta()
def render_frame_meta(self):
r,o=0,0
m=tk.Frame(self, bg="#000000", height=100, width=100).grid(column=4, columnspan=2, sticky='ew')
self.frame_meta_labels = []
self.frame_meta_values = []
for L in ['Race', 'Class', 'Background', 'Name', 'Level', 'Origin']:
self.frame_meta_labels = tk.Label(m, text=L + ':', bg=color_frame['ip'], relief='ridge', font=metadata_font, anchor='w').grid()
self.frame_meta_values = tk.Label(m, text='None', bg=color_frame['ip'], relief='ridge', font=metadata_font, anchor='e').grid()
r=r+1
if(r==3):
r,o=0,1
def refresh_UI(self):
#c
pass

我省略了其他页面,因为它们在我的开发过程中是占位符 - genPage 是我想要开始工作的页面。该框架实际上在我想要的位置显示为黑色矩形(如预期)(在 def render_frame 下称为 m)。

当我传递小部件时,for 循环中的标签会出现,但不会出现在框架中。他们实际上正在做一些非常奇怪的事情 - 他们甚至没有出现在主框架上 - 他们出现在主框架下方。框架出现,并且清晰可见,但向框架添加小部件只会将它们放在其他位置。

最佳答案

将行 m=to.Frame(... 更改为:

m = tk.Frame(…)
m.grid(…)

这应该可以解决你的问题。请参阅此链接以了解原因:

Tkinter: AttributeError: NoneType object has no attribute

关于python - 如何修复不工作的 Tkinter 嵌套框架;它不允许小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56861945/

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