Home > Blockchain >  Random Number Generator with Tkinter module - Unable to clear old label data and need to populate it
Random Number Generator with Tkinter module - Unable to clear old label data and need to populate it

Time:01-24

This morning I wrote a simple random number generator (using Tkinter module), which generates a random number from 1-100. With my below code, it is functional and not generating any errors, however when I roll the next number, if it is a single digit vs a double digit.. The single digit will have overlapping text on the next generated label.

Essentially what I am aiming to do is either use configure() or destroy() to clear the old number / text that was generated on click, and update it with the new text / number data. I want this to occur after every click so I do not get any overlapping or funky looking output.

I tried researching on stackoverflow, and other people's code was confusing to me and didn't really apply to what I wrote. Searched through the Python Docs and it gave me the commands to utilize, but not how to utilize it in my example program... it was quite confusing. I also googled a bunch and watched several YouTube videos... and I still can't solve this supposedly simple task. Any help is much needed and appreciated! Expecting it to be cleared and then populated with the new text and rolled number. Thanks!

# Creating a Program to roll a # between 1-100.

import random
from tkinter import *
import tkinter as tk


root = tk.Tk()


def action():
    roll = random.randint(1, 100)
    result = tk.Label(text=f"You rolled a {roll}! ", font='Helsinki, 30')
    result.place(x=300, y=220)


root.geometry("800x600")
title = root.title("Random Roll Generator")


description = tk.Label(text="Welcome you degenerate! Click the button below to roll a random number from 1-100.", font='Helsinki, 20')
description.place(x=20, y=100)
roll_button = tk.Button(text="Roll", padx=20, pady=0, command=action)
roll_button.place(x=360, y=520)


root.mainloop()

CodePudding user response:

My solution is to create the label first, and then use the config command to update its text, without the need to create and package a new one.

Note that the fstring allows formatting, that is, you can always leave your number with 3 digits, avoiding unnecessary movements in the layout



# Creating a Program to roll a # between 1-100.

import random
import tkinter as tk

root = tk.Tk()
root.geometry('300x300')

rand_label = tk.Label(master=root, text='Click to roll New randon number.')
rand_label.pack()


def new_rng():
    rand_label.config(text=f'New randon number: {random.randint(0, 100):03}')


rand_button = tk.Button(master=root, text='Roll', command=new_rng)
rand_button.pack()

root.mainloop()

Example working

CodePudding user response:

You are placing a new label each time you roll with the current script, and that is why sometimes you see the overlapping label.

Use a string variable and update the variable.

# Creating a Program to roll a # between 1-100.

import random
from tkinter import *
import tkinter as tk


root = tk.Tk()


def action():
    roll = random.randint(1, 100)
    result_text.set(f"You rolled a {roll}! ")


root.geometry("800x600")
title = root.title("Random Roll Generator")


description = tk.Label(text="Welcome you degenerate! Click the button below to roll a random number from 1-100.", font='Helsinki, 20')
description.place(x=20, y=100)
result_text = StringVar("")  # string variable to store result
result = tk.Label(textvariable = result_text, font='Helsinki, 30')
result.place(x=300, y=220)
roll_button = tk.Button(text="Roll", padx=20, pady=0, command=action)
roll_button.place(x=360, y=520)


root.mainloop()

CodePudding user response:

The correct way to used widget.config in line 11. No needed to used StringVar(). Do not use this from tkinter import * is not widely used.

Code:

import random
import tkinter as tk


root = tk.Tk()

def action():
    roll = random.randint(1, 100)
    result.config(text=f"You rolled a {roll}! ")
     
root.geometry("800x600")
title = root.title("Random Roll Generator")

description = tk.Label(text="Welcome you degenerate! Click the button below to roll a random number from 1-100.", font='Helsinki, 20')
description.place(x=20, y=100)

result = tk.Label( font='Helsinki, 30')
result.place(x=300, y=220)
    
roll_button = tk.Button(text="Roll", padx=20, pady=0, command=action)
roll_button.place(x=360, y=520)


root.mainloop()

Screenshot:

enter image description here

  • Related