samedi 30 mai 2020

Solving problem to generate custom number from inserted values with GUI in Python

Hello im having python learning project. I want to insert in GUI two numbers, which are defining range for program to generate random number from.

I am really having problems with calling function with press of the button. And constantly getting error ValueError: invalid literal for int() with base 10: '', when trying to convert string from entry in GUI to int and then inserting them into random.randint.

Thx for Help!

from tkinter import *
import random

root = Tk()
root.title("Generator custom random number")

#function that gets number from entry converts string to int and defines target number in stevilo
def function():
    string_answer1 = prvo_stevilo.get()
    int1 = int(string_answer1)
    string_answer2 = drugo_stevilo.get()
    int2 = int(string_answer2)
    stevilo = random.randint(int1, int2)


#Defining GUI
navodilo = Label(root, text="Choose custom lower and upper number to chose random number from", width=60)
navodilo2 = Label(root, text="From", width=20, borderwidth=3)
navodilo3 = Label(root, text="To", width=20, borderwidth=3)
prvo_stevilo = Entry(root, width=20, borderwidth=3)
drugo_stevilo = Entry(root, width=20, borderwidth=3)
gumb = Button(root, text="Generate number", width=17, height=2, command=function)
praznavrstica = Label(root, text="")
izpis = Label(root, text="Random number is: ", width=20)
izpis_stevila = Label(root, text="" + stevilo)

#Showcase of gui
navodilo.grid(row=0, column=0, columnspan=1)
navodilo2.grid(row=1, column=0, columnspan=1)
navodilo3.grid(row=3, column=0, columnspan=1)
prvo_stevilo.grid(row=2, column=0, columnspan=1)
drugo_stevilo.grid(row=4, column=0, columnspan=1)
praznavrstica.grid(row=5, column=0, columnspan=1)
gumb.grid(row=6, column=0, columnspan=1)
praznavrstica.grid(row=7, column=0, columnspan=1)
izpis.grid(row=8, column=0, columnspan=1)
izpis_stevila.grid(row=9, column=0, columnspan=1)


#Loop
root.mainloop()



Aucun commentaire:

Enregistrer un commentaire