Python Forum

Full Version: Calling Input for Random Generation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, I'm a returning python coder, but it's been a while since the last time I've done anything with functions. I was trying to make a GUI application for generating pseudo-random numbers, but I got a bit puzzled with trying to tell TKinter to get the user input value and use it to generate between 0 and the fetched input. Huh Constructive criticism and advice is appreciated. Big Grin

import tkinter as tk
import random as rand
window = tk.Tk()

label = tk.Label(
    text='Enter a number to inclusively generate up to:',
    background='antique white',
    )

value = tk.Entry(
    bg='antique white',
    width=6,
    )

button = tk.Button(
    text='Generate!',
    width=12,
    height=3,
    )

label.pack()
value.pack()
button.pack()

# The code above should open a window, a label up top,
# an entry field in the middle, and a button on the bottom.


valin = str(value.get())
RNG = rand.randrange(valin())
output = entry.insert(RNG())


# What I want to accomplish is have the program take "valin",
# and use the given integer as an input for "RNG", which is supposed to generate a number
# from 0 to the given input (inclusive), then return the generated value below the button.

window.mainloop()
If your code is generating an error, please include it. Otherwise we're just guessing about what's going on.

valin = str(value.get())
RNG = rand.randrange(valin())
valin is a str. randrange takes a number. Also, strs can't be called (you shouldn't have parentheses after it. Perhaps something more like:

valin = int(value.get())
RNG = rand.randrange(valin)
That might work for integer inputs, but you probably want the program to validate the input first. Right now it will just error out if the entry is not an integer.