'Bind function to multiple labels named equally - tkinter
I want to allow the user to create as many labels as they want using the following code:
def new_line(event):
global Row_n
Row_n = Row_n + 1
Choose= tk.Label(frame, text="Choose option", background = "white",font = ("Helvetica",13),fg = "blue")
Choose.grid(row = Row_n, column = 0, sticky = 'nsew')
return Row_n
root.bind('<Return>',lambda event:new_line(event))
That way, by pressing "Enter", the user can create as many "Choose" labels as they wish. But then I want for a second label to appear every time the user clicks on one of the "Choose" label. So I use the following code:
def second_l(event):
Row_n = Row_n+1
second_label = tk.Label(frame, text="Second label")
Choose.bind('<Button-1>',lambda event:second_l(event))
When I try to run this I get the following error:
can't invoke "bind" command: application has been destroyed
If I add "Choose" label outside the "new_line" function the "second_l" function will only work for that label. It won´t work for the labels generated by the "new_line" function.
Whole code:
import tkinter as tk#to create the gui
from tkinter import filedialog, Text #filedialog to pick apps and Text to display text
import os #Allows us to run apps
root = tk.Tk()
frame = tk.Frame(root,bg="white")
frame.place(relwidth=0.8,relheight=0.7,relx=0.1,rely=0.1)
Row_n=0
def new_line(event):
global Row_n
Row_n = Row_n + 1
Choose= tk.Label(frame, text="Choose option", background = "white",font = ("Helvetica",13),fg = "blue")
Choose.grid(row = Row_n, column = 0, sticky = 'nsew')
return Row_n
root.bind('<Return>',lambda event:new_line(event))
def second_l(event):
global Row_n
Row_n = Row_n+1
second_label = tk.Label(frame, text="Second label")
second_label.grid(row = Row_n, column = 0, sticky = 'nsew')
Choose.bind('<Button-1>',lambda event:second_l(event))
root.mainloop()
Solution 1:[1]
I am unable to understand why are you getting this error "can't invoke "bind" command" but I think, I understand your problem
you can try this:-
import tkinter as tk#to create the gui
from tkinter import filedialog, Text #filedialog to pick apps and Text to display text
import os #Allows us to run apps
root = tk.Tk()
frame = tk.Frame(root,bg="white")
frame.place(relwidth=0.8,relheight=0.7,relx=0.1,rely=0.1)
Row_n=0
Choose= tk.Label(frame, text="", background = "white",font = ("Helvetica",13),fg = "blue")
def new_line(event):
global Row_n
Row_n = Row_n + 1
Choose.config(text="Choose option")
Choose.grid(row = Row_n, column = 0, sticky = 'nsew')
return Row_n
root.bind('<Return>',lambda event:new_line(event))
def second_l(event):
global Row_n
Row_n = Row_n+1
second_label = tk.Label(frame, text="Second label")
second_label.grid(row = Row_n, column = 0, sticky = 'nsew')
Choose.bind('<Button-1>',lambda event:second_l(event))
root.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Coder |