When I try to run this code, a ValueError
appears alluding to the function numRandom
. I though Python could pass a string representation of a int to an int
.
import tkinter
import random
window = tkinter.Tk()
window.geometry('600x500')
x = random.randint(1,300)
remainingTime = True
Attempts = 4
def numRamdom():
global Attempts
while Attempts > 0:
numWritten = int(entryWriteNumber.get())
if numWritten > x:
lblClue.configure(text = 'Its a bigger number')
Attempts = Attempts -1
if numWritten < x:
lblClue.configure(text = 'Its a smaller number')
Attempts = Attempts -1
if numWritten == x:
lblClue.configure(text = 'Congratulations ;)')
remainingTime = False
return remainingTime, countdown(0)
if Attempts == 0:
remainingTime = False
return remainingTime, countdown(0), Attempts, gameOver()
entryWriteNumber = tkinter.Entry(window)
entryWriteNumber.grid(column = 0, row = 1, padx = 10, pady = 10)
numRamdom()
window.mainloop()
The problem is because when the code is ran, it directly calls numRamdom()
, that is, initially the entry widgets are empty, and they run it with those empty entry widget and hence the error. So just assign a button and a command, like:
b = tkinter.Button(root,text='Click me',command=numRamdom)
b.grid(row=1,column=0)
Make sure to say this before the mainloop()
after the def numRamdom():
. The button just runs the function only when the button is clicked.
Or if you want button-less then try:
METHOD-1:
root.after(5000,numRamdom) #after 5 sec it will execute function
But keep in mind, if the user doesn't enter properly in 5 sec then some error would pop up.
METHOD-2:
def numRamdom(event):
......
entryWriteNumber.bind('<Return>',numRamdom)
This is so that, if you press enter key in the entry widget(after entering data) it will run the function.
Hope this helps, do let me know if any errors.
Cheers