Python Forum
Calling Input for Random Generation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling Input for Random Generation
#1
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()
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 938 May-21-2023, 08:17 AM
Last Post: snippsat
  Node Flow Generation in Python Linenloid 0 630 Feb-21-2023, 07:09 PM
Last Post: Linenloid
  Allure Report Generation rotemz 0 751 Jan-24-2023, 08:30 PM
Last Post: rotemz
  Why doesnt chunk generation work? LotosProgramer 1 1,909 Apr-02-2022, 08:25 AM
Last Post: deanhystad
  Random data generation sum to 1 by rounding juniorcoder 9 3,350 Oct-20-2021, 03:36 PM
Last Post: deanhystad
Question PDF generation / edit SpongeB0B 2 2,042 Jul-28-2021, 05:59 AM
Last Post: SpongeB0B
  Generate Random operator, take user input and validate the user mapypy 4 5,459 Feb-03-2021, 08:41 PM
Last Post: nilamo
  calling a function and argument in an input phillup7 3 2,555 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  trying to input a variable using random.choice python63 9 3,530 Aug-13-2020, 05:37 PM
Last Post: python63
  Help with calling list from user input farispython 5 2,755 Nov-03-2019, 03:13 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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