【发布时间】:2016-03-17 19:08:48
【问题描述】:
我似乎在这里遇到了死胡同。我在网上进行了大量研究,但未能找到解决方案。
My issue is, i have an "optionmenu" (#1) in my GUI, when a certain option is chosen, a new "optionmenu" (#2) is created.然后用户可以在#2 中做出选择。根据他在#2 中的选择,条目小部件会随着选项的更改出现并被销毁。我的问题在这里,当显示选项菜单#2 并且用户决定更改选项菜单#1 时,我能够从#1 和#2 选项菜单中销毁所有条目小部件;但是,我仍然在后台留下选项菜单#2。
我只能找到在线解决方案
条目和标签
但是,我无法找到任何解决方案
选项菜单
关于如何销毁选项菜单的任何想法?代码的 sn-p 如下所示,因为它目前的行为如上所述。
from Tkinter import *
neper_tessellation_type={'hard-core':'1','centroid':'3','weight':'0'}
neper_weight_type={'dirac':'1','gaussian':'2','flat':'2','bernoulli':'3'}
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
#Setting up widgets onLoad
self.grid()
self.create_widgets()
# Tessellations Type Option Builder
def tessellation_type(self, req_dim):
global tess_container
global labels_container
global weight_container
for wid in tess_container:
wid.destroy() ## Destroy the OPTIONMENU1 ENTRY CONTAINER fields
for label in labels_container:
label.destroy() ## Supposed to destroy the OptionMenu2 ITSELF, but does not do as requried.
for lid in weight_container:
lid.destroy() ## Destroy the OPTIONMENU2 ENTRY CONTAINER fields
weight_container = []
labels_container = []
tess_container = []
for type, req_dim in neper_tessellation_type.iteritems():
self.s = StringVar()
choice = self.tess_type.get()
if type == self.tess_type.get() and choice != 'weight':
u = int(req_dim)
elif choice == 'weight': ## OPTIONMENU 2 - When weight is chosen a new drop down menu is made and the function command moves to weighttype
weight_dropdown = OptionMenu(self, self.s, *neper_weight_type, command=self.weight_type).grid(row=13, column=2)
u = 0
for b in range(u):
c = Entry(self)
c.grid(row=13, column=2 + b)
tess_container.append(c) # Append widget to container list
def weight_type(self, req_dim1):
global weight_container
for lid in weight_container:
lid.destroy()
weight_container = []
for type1, req_dim1 in neper_weight_type.iteritems():
if type1 == self.s.get():
u1 = int(req_dim1)
for bf in range(u1):
t = Entry(self)
t.grid(row=13, column=3 + bf)
weight_container.append(t) # Append widget to container list
# *** MAIN FRAMES ***
def create_widgets(self):
## OPTIONMENU 1
Label(self, text="Voronoi Type").grid(row=13, column=0)
self.tess_type = StringVar()
tess_type_dropdown = OptionMenu(self, self.tess_type, *neper_tessellation_type, command=self.tessellation_type).grid(row=13, column=1)
## Reset for containers of choice
tess_container = []
labels_container = []
weight_container = []
root = Tk()
app = Application(root)
root.mainloop()
在 Bryan 澄清后修改了问题。
【问题讨论】:
-
有没有可能把你想销毁的所有东西放在一个单独的Frame里,然后销毁frame。请注意,所有小部件都继承了一个销毁方法 AFAIK。你试过 destroy() 吗?
-
+" 销毁函数的工作原理是创建一个小部件容器,然后销毁该容器"_.不,这不是破坏功能的工作方式。 destroy 函数只是删除调用它的小部件。它对 OptionMenu 的作用与对 Label 或 Entry 小部件的作用相同。
-
您不需要所有代码来重现问题。请阅读stackoverflow.com/help/mcve,然后把代码尽量删减。
-
@CurlyJoe 框架与用户手册一致,修改它们会很麻烦。
-
labels_container是空的,所以它甚至从不尝试破坏任何东西。您可以通过非常基本的调试发现这一点。destroy()永远不会在 OptionMenu 上工作,因为它从一开始就不会被调用。
标签: python python-2.7 tkinter optionmenu