Python Forum
Creating and destroying dynamic labels in Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating and destroying dynamic labels in Tkinter
#1
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()
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating labels Wenkar7000 2 336 Oct-20-2020, 10:49 AM
Last Post: DeaD_EyE
  Tkinter having problems with packing labels? wallgraffiti 0 338 Aug-02-2020, 09:26 AM
Last Post: wallgraffiti
  Centering labels and buttons Heyjoe 8 1,752 Jul-16-2020, 02:39 PM
Last Post: deanhystad
  [Tkinter] how to draw dynamic moving scale and potting trace point on waveform in tkinter pytho sameer_1985 0 444 May-31-2020, 01:52 PM
Last Post: sameer_1985
  [Tkinter] Tkinter custom widget styling and creating custom theme karolp 6 1,529 May-06-2020, 06:11 PM
Last Post: karolp
  [Tkinter] Problem with tkinter when creating .exe file Jan_97 2 1,708 Feb-27-2020, 05:17 PM
Last Post: Jan_97
  Spacing Between two labels are very far and Top label is not in Center using Tkinter barry76 2 2,405 Jul-30-2019, 10:49 AM
Last Post: wuf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020