如果在 OptionMenu 中进行了另一个选择,则销毁小部件
Destroying a widget if another selection is made in an OptionMenu
我正在使用 tkinter 制作 GUI。我希望只有在选择了某些参数时才会出现一些小部件。为此,我使用了 .pack_forget() 方法。但是,现在我有另一个问题。我在 OptionMenu 上设置了一个条件,它会根据用户的选择显示 ComboBox 或 Entry。但是如果用户改变了他的选择,我无法设法让两个小部件之一消失。
这是我的代码:
# Creation of Frame 4 #
Frame4 = tk.Frame(fenetre)
Frame4.pack(anchor = tk.NW, padx = 5, pady = 5)
# label4 creation #
label4 = tk.Label(Frame4, text = "what is the format?", font = ('Arial', '12'))
label4.pack(side = tk.LEFT)
# Création de la commande #
def Format_choice(self):
global Format
if choice.get() != Format:
Format = choice.get()
if Format == "CAPS Client":
# Création de la liste déroulante
Frame6.pack(anchor = tk.NW, padx = 5, pady = 5)
# NOMS is a list of strings
comboExample = ttk.Combobox(Frame6, textvariable = tk.StringVar(), values = NOMS, width = 45)
comboExample.current(0)
def callbackFunc(self):
global nom_site
nom_site = comboExample.get()
comboExample.bind("<<ComboboxSelected>>", callbackFunc)
comboExample.pack(side = tk.LEFT)
elif Format == "ETUDE":
Frame6.pack(anchor = tk.NW, padx = 5, pady = 5)
site = tk.StringVar()
def Name():
global nom_site
nom_site = site.get()
entrysite = tk.Entry(Frame6, textvariable = site)
entrysite.pack(side=tk.LEFT, padx=5, pady=5)
entrysite.focus_set()
valider = tk.Button(Frame6, text="Confirmer", command = Name, activeforeground = 'red')
valider.pack(side = tk.LEFT)
# Creation of choice#
choice = tk.StringVar()
choice.set("Default")
# Creation of the OptionMenu #
option4 = tk.OptionMenu(Frame4, choice, "Format1", "Format2", command = Format_choice)
option4.pack(side = tk.LEFT)
# Creation of Frame6
Frame6 = tk.Frame(fenetre)
# label6 creation
label6 = tk.Label(Frame6, text= "What is the name of the site ?", font = ('Arial', '12'))
label6.pack(side=tk.LEFT)
Frame6.pack_forget()
pack_forget()
只会使 Frame6
在这里不可见,而它仍然存在。
因此,请改用 .destroy()
。正如此处的精美回答:
Python Tkinter clearing a frame
所以,基本上你有两个选择:
- 销毁
Frame6
并在函数 Format_choice()
的每个 if-elif
案例中再次创建其中的所有内容,或者
- 销毁由先前选择的选项创建的
Frame6
的每个小部件。
如果您喜欢第二种方式,您可以在 if-elif 案例之前添加一段代码。使用 winfo_children()
很有魅力:
.
.
def Format_choice(self):
global Format
if choice.get() == Format:
return #so that if user selects same option again, no change occurs in Frame6
else:
Format = choice.get()
for widget in Frame6.winfo_children():
if widget != label6:
widget.destroy()
if Format == "CAPS Client":
# Création de...
.
.
.
这样,您可能不想销毁的 label6
不会被销毁,而其他小部件会被销毁。
我正在使用 tkinter 制作 GUI。我希望只有在选择了某些参数时才会出现一些小部件。为此,我使用了 .pack_forget() 方法。但是,现在我有另一个问题。我在 OptionMenu 上设置了一个条件,它会根据用户的选择显示 ComboBox 或 Entry。但是如果用户改变了他的选择,我无法设法让两个小部件之一消失。
这是我的代码:
# Creation of Frame 4 #
Frame4 = tk.Frame(fenetre)
Frame4.pack(anchor = tk.NW, padx = 5, pady = 5)
# label4 creation #
label4 = tk.Label(Frame4, text = "what is the format?", font = ('Arial', '12'))
label4.pack(side = tk.LEFT)
# Création de la commande #
def Format_choice(self):
global Format
if choice.get() != Format:
Format = choice.get()
if Format == "CAPS Client":
# Création de la liste déroulante
Frame6.pack(anchor = tk.NW, padx = 5, pady = 5)
# NOMS is a list of strings
comboExample = ttk.Combobox(Frame6, textvariable = tk.StringVar(), values = NOMS, width = 45)
comboExample.current(0)
def callbackFunc(self):
global nom_site
nom_site = comboExample.get()
comboExample.bind("<<ComboboxSelected>>", callbackFunc)
comboExample.pack(side = tk.LEFT)
elif Format == "ETUDE":
Frame6.pack(anchor = tk.NW, padx = 5, pady = 5)
site = tk.StringVar()
def Name():
global nom_site
nom_site = site.get()
entrysite = tk.Entry(Frame6, textvariable = site)
entrysite.pack(side=tk.LEFT, padx=5, pady=5)
entrysite.focus_set()
valider = tk.Button(Frame6, text="Confirmer", command = Name, activeforeground = 'red')
valider.pack(side = tk.LEFT)
# Creation of choice#
choice = tk.StringVar()
choice.set("Default")
# Creation of the OptionMenu #
option4 = tk.OptionMenu(Frame4, choice, "Format1", "Format2", command = Format_choice)
option4.pack(side = tk.LEFT)
# Creation of Frame6
Frame6 = tk.Frame(fenetre)
# label6 creation
label6 = tk.Label(Frame6, text= "What is the name of the site ?", font = ('Arial', '12'))
label6.pack(side=tk.LEFT)
Frame6.pack_forget()
pack_forget()
只会使 Frame6
在这里不可见,而它仍然存在。
因此,请改用 .destroy()
。正如此处的精美回答:
Python Tkinter clearing a frame
所以,基本上你有两个选择:
- 销毁
Frame6
并在函数Format_choice()
的每个if-elif
案例中再次创建其中的所有内容,或者 - 销毁由先前选择的选项创建的
Frame6
的每个小部件。
如果您喜欢第二种方式,您可以在 if-elif 案例之前添加一段代码。使用 winfo_children()
很有魅力:
.
.
def Format_choice(self):
global Format
if choice.get() == Format:
return #so that if user selects same option again, no change occurs in Frame6
else:
Format = choice.get()
for widget in Frame6.winfo_children():
if widget != label6:
widget.destroy()
if Format == "CAPS Client":
# Création de...
.
.
.
这样,您可能不想销毁的 label6
不会被销毁,而其他小部件会被销毁。