Python Forum
Button to clear all output labels?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Button to clear all output labels?
#3
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()
Reply


Messages In This Thread
RE: Button to clear all output labels? - by deanhystad - Dec-10-2020, 04:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Button/output help swittler 4 2,648 Jun-24-2020, 04:59 PM
Last Post: menator01
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,138 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  clear button destroy can't work jacklee26 1 4,209 Jul-07-2019, 03:44 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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