Dec-10-2020, 04:59 AM
import tkinter as tk from random import randint dice = [4, 6, 8, 10, 12, 20, 100] def roll(label, sides): """Roll die and update label""" label['text'] = str(randint(1, sides)) def clear_all(labels): """Blank out all the labels""" for l in labels: l['text'] = '' root = tk.Tk() # Make a bunch of buttons and label. Each button calls "roll()" which rolls # a die and displays the value in the label. all_labels = [] for row, sides in enumerate(dice): label = tk.Label(root, text = '', width=3) label.grid(column=1, row=row, padx=5, pady=5, sticky='NEWS') all_labels.append(label) button = tk.Button(root, text=f'D{sides}', command=lambda x=label,y=sides: roll(x, y)) button.grid(column=0, row=row, padx=5, pady=5, sticky='NEWS') # Make a button to clear all the labels button = tk.Button(root, text='Clear All', command=lambda: clear_all(all_labels)) button.grid(column=0, row=len(dice), columnspan=2, padx=5, pady=5, sticky='NEWS') root.mainloop()