![]() |
Creating and destroying dynamic labels in Tkinter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Creating and destroying dynamic labels in Tkinter (/thread-26468.html) |
Creating and destroying dynamic labels in Tkinter - MarcusRoberts - May-02-2020 I am trying to have two windows be created in tkinter, one has buttons for many different genres, the other a blank window. When I press one of the buttons, I want that genre to appear in the other window as a label. I want to be able to click as many buttons as I want and have them appear as labels and if I click the same button again the label should be destroyed and not appear in the list. The problem I believe is the exec("global Label_"+str(n)) on line 56. P.s only code below line 44 is of concern. If I run this code with only one variable so that I dont need to use the exec function, it works, this is the second bit of code. Thanks in advance for any help. import xlrd import tkinter as tk from functools import partial #------------------------------------------------------------------------------ wb = xlrd.open_workbook('Movie Archive.xlsx') sheet = wb.sheet_by_index(0) titles = [sheet.cell_value(n,0) for n in range(sheet.nrows)] titles.pop(0) genre = [sheet.cell_value(n,1) for n in range(sheet.nrows)] genre.pop(0) disk_type = [sheet.cell_value(n,2) for n in range(sheet.nrows)] disk_type.pop(0) genre = [genre[n].split('/') for n in range(len(genre))] unsorted_list = [[titles[n],genre[n],disk_type[n]] for n in range(len(titles))] sorted_titles = sorted(titles) sorted_list = [] for n in sorted_titles: for i in unsorted_list: if n == i[0]: sorted_list.append(i) archive = {sorted_list[n][0]:[sorted_list[n][1],sorted_list[n][2]] for n in range(len(sorted_list))} indiv_genres = [] for n in genre: for i in n: if i not in indiv_genres: indiv_genres.append(i) else: pass indiv_genres = sorted(indiv_genres) #------------------------------------------------------------------------------ genres = tk.Tk() movies = tk.Tk() selection = [] def selector(n,indiv_genres,selection): if indiv_genres[n] not in selection: selection.append(indiv_genres[n]) exec("global Label_"+str(n)) exec("Label_"+str(n)+"= tk.Label(movies,text = indiv_genres[n])") exec("Label_"+str(n)+".pack()") print(selection) else: selection.remove(indiv_genres[n]) exec("Label_"+str(n)+".destroy()") print(selection) def button_creator(n,indiv_genres): button = tk.Button(genres,text = indiv_genres[n],command = partial(selector,n,indiv_genres,selection)) button.pack() for n in range(len(indiv_genres)): button_creator(n,indiv_genres) finish_button = tk.Button(genres,text='Finish!',command = genres.destroy,bg = 'red',fg = 'black') finish_button.pack() genres.mainloop() movies.mainloop() import tkinter as tk from functools import partial root = tk.Tk() root_2 = tk.Tk() list1 = [] def function(list1): if 'test' not in list1: global label label = tk.Label(root_2,text = 'On') list1.append('test') label.pack() else: list1.remove('test') label.destroy() switch = tk.Button(root, text = 'On',command = partial(function,list1)) switch.pack() root_2.mainloop() root.mainloop() RE: Creating and destroying dynamic labels in Tkinter - Yoriz - May-02-2020 Don't use exec use a dictionary import tkinter as tk from functools import partial root = tk.Tk() root_2 = tk.Tk() labels = {} def function(number): if number not in labels.keys(): label = tk.Label(root_2, text=f'On{number}') label.pack() labels[number] = label else: label = labels.pop(number) label.destroy() for number in range(20): switch = tk.Button( root, text=f'On{number}', command=partial(function, number)) switch.pack() root.mainloop() |