Reputation: 27
I want to add and delete labels to my Toplevel window frame by clicking buttons.
def Option_1_Function():
def Add_Label_Function():
My_Label_1 = Label(root_Option_1, text="First Label")
My_Label_1.pack(side=BOTTOM)
My_Label_2 = Label(root_Option_1, text="Second Label")
My_Label_2.pack(side=BOTTOM)
My_Label_3 = Label(root_Option_1, text="Third Label")
My_Label_3.pack(side=BOTTOM)
return
def Delete_Label_Function():
My_Label_1.Destroy()
My_Label_2.Destroy()
My_Label_3.Destroy()
return
root_Option_1 = Toplevel()
root_Option_1.geometry("300x300")
My_Button_3 = Button(root_Option_1, text="Add Label", command=Add_Label_Function)
My_Button_3.pack(side=LEFT)
My_Button_4 = Button(root_Option_1, text="Delete Label", command=Delete_Label_Function)
My_Button_4.pack(side=RIGHT)
root_Option_1.mainloop()
return
def Option_2_Function():
return
root = Tk()
root.geometry("200x200")
My_Button_1 = Button(root, text="Option 1", command=Option_1_Function)
My_Button_1.pack(side=LEFT)
My_Button_2 = Button(root, text="Option 2", command=Option_2_Function)
My_Button_2.pack(side=RIGHT)
root.mainloop()
So I get this error.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\jlabonte\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Users\jlabonte\Desktop\test.py", line 22, in Delete_Label_Function
My_Label_1.Destroy()
NameError: name 'My_Label_1' is not defined
I am really new to coding so please let me know if I am going about this all wrong.
1) I want to create a first window that has many different option buttons.
2) I then want to create a second window, by selecting an option button, that will have an add and a delete button.
3) I then want to be able to add and delete labels to my second window by selecting those buttons as many times as I want.
What I found so far to be able to do this is to create a function for my buttons that I can call with command=. But since variables exist only within the functions they were created in, I can't destroy what does not exist.
So how can I get my add label and delete label buttons to work properly?
Thanks.
Upvotes: 0
Views: 263
Reputation: 11
use:
global My_Label_X
to make your labels available as a variable and that's all
and for the one who answered, simpler is more ;)
Upvotes: 0
Reputation:
You have to keep a reference to each label. The references you create in Add_Label_Function() are local (within the function only) and so are garbage collected when the function exits. This is a program I had sitting around. It keeps a reference to the buttons by adding it to a dictionary. Note that buttons and grid.forget() are used instead of labels and destroy but the principle is the same.
from Tkinter import *
from functools import partial
class ButtonsTest:
def __init__(self):
self.top = Tk()
self.top.title("Click a button to remove")
self.top_frame = Frame(self.top, width =400, height=400)
self.button_dic = {}
self.buttons()
self.top_frame.grid(row=0, column=1)
Button(self.top_frame, text='Exit', bg="orange",
command=self.top.quit).grid(row=10,column=1, columnspan=5)
self.top.mainloop()
##-------------------------------------------------------------------.........
def buttons(self):
b_row=1
b_col=0
for but_num in range(1, 11):
## create a button and send the button's number to
## self.cb_handler when the button is pressed
b = Button(self.top_frame, text = str(but_num),.
command=partial(self.cb_handler, but_num))
b.grid(row=b_row, column=b_col)
## dictionary key=button number --> button instance
self.button_dic[but_num] = b
b_col += 1
if b_col > 4:
b_col = 0
b_row += 1
##----------------------------------------------------------------
def cb_handler( self, cb_number ):
print "\ncb_handler", cb_number
self.button_dic[cb_number].grid_forget()
##===================================================================
BT=ButtonsTest()
Upvotes: 1