首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复不工作的Tkinter嵌套框架;它不允许小部件

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

Stack Overflow用户
提问于 2019-07-03 09:58:29
回答 1查看 159关注 0票数 0

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

我在Visual Studio 2017上运行Anaconda 3.4。

代码语言:javascript
复制
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循环中的标签会出现,但不会出现在框架中。它们实际上是在做一些非常奇怪的事情-它们甚至没有出现在主框架上-它们出现在它的下方。框架出现了,并且清晰可见,但是向框架添加小部件只会将它们放在其他地方。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-03 16:38:28

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

代码语言:javascript
复制
m = tk.Frame(…)
m.grid(…)

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

Tkinter: AttributeError: NoneType object has no attribute

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56861945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档