ValueError: invalid literal for int() with base 10: '' (Tkinter)
ValueError: invalid literal for int() with base 10: '' (Tkinter)
当我尝试 运行 这段代码时,出现 ValueError
暗指函数 numRandom
。我虽然 Python 可以将 int 的字符串表示形式传递给 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()
问题是因为当代码是运行时,它直接调用numRamdom()
,也就是说,最初入口小部件是空的,它们运行它与那些空入口小部件,因此错误。所以只需分配一个按钮和一个命令,例如:
b = tkinter.Button(root,text='Click me',command=numRamdom)
b.grid(row=1,column=0)
确保在 def numRamdom():
之后的 mainloop()
之前说这句话。该按钮只是 运行 只有在单击该按钮时才起作用。
或者如果你想要 button-less 那么试试:
方法一:
root.after(5000,numRamdom) #after 5 sec it will execute function
但请记住,如果用户没有在 5 秒内正确输入,则会弹出一些错误消息。
方法二:
def numRamdom(event):
......
entryWriteNumber.bind('<Return>',numRamdom)
这样一来,如果您在输入小部件中按回车键(在输入数据后),它将 运行 该函数。
希望对您有所帮助,如有错误请告诉我。
干杯
这是一个基于您的代码的完整示例。您的问题是试图在 Entry 的内容包含任何内容之前转换它的内容。要解决此问题,您可以添加一个调用命令 numRamdom()
的按钮
import tkinter
import random
window = tkinter.Tk()
window.geometry('600x500')
x = random.randint(1,300)
remainingTime = True
Attempts = 4
def numRamdom():
global Attempts, lblClue, x
if Attempts > 0:
numWritten = int(entryWriteNumber.get())
if numWritten < x:
lblClue.configure(text = 'Its a bigger number')
Attempts = Attempts -1
elif numWritten > x:
lblClue.configure(text = 'Its a smaller number')
Attempts = Attempts -1
else:
lblClue.configure(text = 'Congratulations ;)')
remainingTime = False
#return remainingTime, countdown(0)
if Attempts == 0:
remainingTime = False
#return remainingTime, countdown(0), Attempts, gameOver()
else:
lblClue.configure(text = "You ran out of attempts!")
entryWriteNumber = tkinter.Entry(window)
entryWriteNumber.grid(column = 0, row = 1, padx = 10, pady = 10)
entryWriteButton = tkinter.Button(window, text = "Push me!", command = numRamdom)
entryWriteButton.grid(column = 1, row = 1)
lblClue = tkinter.Label(window)
lblClue.grid(row = 2, column = 1)
window.mainloop()
如果传递的值无法转换为整数,您仍然会收到错误消息,但这很容易通过 if
语句修复。
当我尝试 运行 这段代码时,出现 ValueError
暗指函数 numRandom
。我虽然 Python 可以将 int 的字符串表示形式传递给 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()
问题是因为当代码是运行时,它直接调用numRamdom()
,也就是说,最初入口小部件是空的,它们运行它与那些空入口小部件,因此错误。所以只需分配一个按钮和一个命令,例如:
b = tkinter.Button(root,text='Click me',command=numRamdom)
b.grid(row=1,column=0)
确保在 def numRamdom():
之后的 mainloop()
之前说这句话。该按钮只是 运行 只有在单击该按钮时才起作用。
或者如果你想要 button-less 那么试试:
方法一:
root.after(5000,numRamdom) #after 5 sec it will execute function
但请记住,如果用户没有在 5 秒内正确输入,则会弹出一些错误消息。
方法二:
def numRamdom(event):
......
entryWriteNumber.bind('<Return>',numRamdom)
这样一来,如果您在输入小部件中按回车键(在输入数据后),它将 运行 该函数。
希望对您有所帮助,如有错误请告诉我。
干杯
这是一个基于您的代码的完整示例。您的问题是试图在 Entry 的内容包含任何内容之前转换它的内容。要解决此问题,您可以添加一个调用命令 numRamdom()
import tkinter
import random
window = tkinter.Tk()
window.geometry('600x500')
x = random.randint(1,300)
remainingTime = True
Attempts = 4
def numRamdom():
global Attempts, lblClue, x
if Attempts > 0:
numWritten = int(entryWriteNumber.get())
if numWritten < x:
lblClue.configure(text = 'Its a bigger number')
Attempts = Attempts -1
elif numWritten > x:
lblClue.configure(text = 'Its a smaller number')
Attempts = Attempts -1
else:
lblClue.configure(text = 'Congratulations ;)')
remainingTime = False
#return remainingTime, countdown(0)
if Attempts == 0:
remainingTime = False
#return remainingTime, countdown(0), Attempts, gameOver()
else:
lblClue.configure(text = "You ran out of attempts!")
entryWriteNumber = tkinter.Entry(window)
entryWriteNumber.grid(column = 0, row = 1, padx = 10, pady = 10)
entryWriteButton = tkinter.Button(window, text = "Push me!", command = numRamdom)
entryWriteButton.grid(column = 1, row = 1)
lblClue = tkinter.Label(window)
lblClue.grid(row = 2, column = 1)
window.mainloop()
如果传递的值无法转换为整数,您仍然会收到错误消息,但这很容易通过 if
语句修复。