Python Forum
Button to clear all output labels? - 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: Button to clear all output labels? (/thread-31375.html)



Button to clear all output labels? - AnunnakiKungFu - Dec-07-2020

from tkinter import *
from random import randint

def rollD4():
    label = Label(root, text=randint(1, 4), font=(None, 12), height=2, width=2).grid(row=0, column=2)
def rollD6():
    label = Label(root, text=randint(1, 6), font=(None, 12), height=2, width=2).grid(row=1, column=2)
def rollD8():
    label = Label(root, text=randint(1, 8), font=(None, 12), height=2, width=2).grid(row=2, column=2)
def rollD10():
    label = Label(root, text=randint(1, 10), font=(None, 12), height=2, width=2).grid(row=3, column=2)
def rollD12():
    label = Label(root, text=randint(1, 12), font=(None, 12), height=2, width=2).grid(row=4, column=2)
def rollD20():
    label = Label(root, text=randint(1, 20), font=(None, 12), height=2, width=2).grid(row=5, column=2)
def rollD100():
    label = Label(root, text=randint(1, 100), font=(None, 12), height=2, width=2).grid(row=6, column=2)


root = Tk()
root.title("Table Top Pal (beta)")
root.geometry("500x392")  # This is how to resize your root box. (width x height)
menubar = Menu(root)
root.config(menu=menubar)

fileMenu = Menu(menubar,tearoff=0)
menubar.add_cascade(label="File",menu=fileMenu)
fileMenu.add_command(label="Exit",command=root.destroy)

button1 = Button(root, text="D4", command=rollD4, width=10, height=3)
button1.grid(row=0, column=0)
button2 = Button(root, text="D6", command=rollD6, width=10, height=3)
button2.grid(row=1, column=0)
button3 = Button(root, text="D8", command=rollD8, width=10, height=3)
button3.grid(row=2, column=0)
button4 = Button(root, text="D10", command=rollD10, width=10, height=3)
button4.grid(row=3, column=0)
button5 = Button(root, text="D12", command=rollD12, width=10, height=3)
button5.grid(row=4, column=0)
button6 = Button(root, text="D20", command=rollD20, width=10, height=3)
button6.grid(row=5, column=0)
button7 = Button(root, text="D100", command=rollD100, width=10, height=3)
button7.grid(row=6, column=0)

root.mainloop()
I am just trying to make a button that clears all of the dice roller outputs.


RE: Button to clear all output labels? - joe_momma - Dec-09-2020

One way is to use textvariables in your label's the main methods are set and get.
I would make a list of the dice and set them all to ''. here's an example with the 4d only
from tkinter import *
from random import randint
 
def rollD4():
    number= str(randint(1,4))
    dice_1.set(number)
def on_clear():
    dice_1.set('')
    

root = Tk()
root.title("Table Top Pal (beta)")
root.geometry("500x592")  # This is how to resize your root box. (width x height)
menubar = Menu(root)
root.config(menu=menubar)
 
fileMenu = Menu(menubar,tearoff=0)
menubar.add_cascade(label="File",menu=fileMenu)
fileMenu.add_command(label="Exit",command=root.destroy)
dice_1= StringVar()
button1 = Button(root, text="D4", command=rollD4, width=10, height=3)
button1.grid(row=0, column=0)
clear_btn= Button(root, text='Clear All', command= on_clear)
clear_btn.grid(row=2,column=0)

label_1 = Label(root, textvariable=dice_1, font=(None, 12), height=2, width=2).grid(row=0, column=2)
root.mainloop()



RE: Button to clear all output labels? - deanhystad - Dec-10-2020

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()



RE: Button to clear all output labels? - AnunnakiKungFu - Dec-11-2020

Jesus Christ I wish I knew Python a lot better. That is a MUCH more efficient way of doing it.


RE: Button to clear all output labels? - deanhystad - Dec-12-2020

I would write the program the same way using almost any language. You don't need to know everything about a language to write effective programs. Extensive knowledge of the language makes programming faster, but you can know everything about Python and still be a crappy Python programmer.

I know almost nothing about Tk. I used it for a job back in 95, but I barely remember anything about it. I am learning a bit about it by answering Tk questions in this forum. I've only been programming in Python for about a year, and mostly in my spare time. I would consider my Python skills to be pretty weak.


RE: Button to clear all output labels? - backoboy10 - Dec-18-2020

Control the text of the label via text variable and on the button click clear that text variable that's it.

Read more here