Refreshing Tkinter root window for Multiplication Table Refreshing Tkinter root window for Multiplication Table tkinter tkinter

Refreshing Tkinter root window for Multiplication Table


You just delete the labels from the label list. They still exist. You need to use destroy method to remove a widget from its container. Also the C-style formatting (%) is almost obsolete today, str.format or f-strings are much better.

Also you could place all the labels into a Frame object and then destroy only that Frame. In this case you will not have to use a collection for the labels. And probably if some values are used only once, it is not necessary to create a variable for them. Of course, it is not the case, when a value is taken from some complicated expressions or you need to clarify its meaning through its name.

Also only one method of packing element can be used for a window. You cannot use one method and then another. I. e. you select pack, grid or place and then use only that selected method. If you want to place the widgets at top center, pack does it by default and it is more handy to use than place. pack has side parameter used to select packing direction and pady with padx parameters used to select outer padding.

And entry variable name is not a good approach, because it gives no information about what data would be entered there.

For random color you can use random.choice function.

Here is your code with my changes:

from tkinter import *import randomroot = Tk()sizex = 600;sizey = 400;posx  = 0;posy  = 0root.wm_geometry('{}x{}+{}+{}'.format(sizex, sizey, posx, posy))main_outer_pady = 4bg_colors = 'red', 'yellow', 'blue', 'green', 'gray'labels = []  # creates an empty list for your labelsdef showTable():    for l in labels:        l.destroy()    labels.clear()    entered_num = num_to_multiply_entry.get()    for i in range(10):  # iterates over your nums        txt = f'{entered_num} x {str(i + 1)} = {int(entered_num) * (i + 1)}'        # Here we randomize background color. We can        # randomize foreground color with the same way.        label = Label(root, text=txt, bg=random.choice(bg_colors))        label.pack(pady=main_outer_pady)        labels.append(label)  # appends the label to the list for further usenum_to_multiply_entry = Entry(root)num_to_multiply_entry.pack(pady=main_outer_pady)btn = Button(root, text='Show Table', command=showTable)btn.pack(pady=main_outer_pady)root.mainloop()

Edits:

Labels reusing example:

from tkinter import *from random import choicesizex = 600sizey = 400posx = 0posy = 0main_outer_pady = 4bg_colors = 'red', 'yellow', 'blue', 'green', 'gray'labels = []def update_table():    entered_num = num_to_multiply_entry.get()    # Iterates over your nums.    for i in range(1, 11):        txt = f'{entered_num} x {i} = {int(entered_num) * i}'        # Here we randomize background color. We can        # randomize foreground color with the same way.        labels[i-1].configure(text=txt, bg=choice(bg_colors))root = Tk()root.wm_geometry('{}x{}+{}+{}'.format(sizex, sizey, posx, posy))num_to_multiply_entry = Entry(root)num_to_multiply_entry.pack(pady=main_outer_pady)make_table_btn = Button(root, text='Show Table', command=update_table)make_table_btn.pack(pady=main_outer_pady)for i in range(10):    labels.append(Label(root))    labels[-1].pack(pady=main_outer_pady)root.mainloop()


del labels[:] could work normally, but it wouldn't update the widgets of your window.For efficiency, I recommend you put them on your window firstly, then just update the text and the "bg".

from tkinter import *root = Tk()sizex = 600;sizey = 400;posx = 0;posy = 0root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))def showTable():    tbl = entry.get()    # change the text and "bg" instead of create them again.    for i, widget in enumerate(labels):        txt = tbl + ' x ' + str(i + 1) + ' = ' + str(int(tbl) * (i + 1))        widget.config(text=txt)        widget.config(bg='cyan')entry = Entry(root)entry.place(x=50, y=50)  # doesn't place the textbox at given positions?entry.pack()btn = Button(root, text='Show Table', command=showTable)btn.place(x=100, y=100)  # doesn't place the button at given positions?btn.pack()nums = range(10)# save them in a list, and use pack layout manager to put them on your window.labels = [label.pack() or label for i in nums for label in [Label(root)]]root.mainloop()