简体   繁体   中英

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()

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM