我正在制作一个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循环中的标签会出现,但不会出现在框架中。他们实际上在做一些很奇怪的事情-他们甚至没有出现在主机上-而是出现在主机的下面。该框架会出现,并且清晰可见,但是将小部件添加到框架中只会将它们放置在其他位置。
答案 0 :(得分:0)
将行m=to.Frame(…
更改为:
m = tk.Frame(…)
m.grid(…)
那应该可以解决您的问题。 请参阅此链接以了解原因: