ValueError:以 10 为基数的 int() 的文字无效:'' (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()


白衣非少年
浏览 168回答 2
2回答

qq_遁去的一_1

问题是因为当代码运行时,它直接调用numRamdom(),也就是说,最初条目小部件是空的,并且它们使用这些空条目小部件运行它,因此出现错误。因此,只需分配一个按钮和一个命令,例如:b = tkinter.Button(root,text='Click me',command=numRamdom)b.grid(row=1,column=0)mainloop()请务必在之前、之后说这句话def numRamdom():。仅当单击按钮时该按钮才运行该功能。或者,如果您想要无按钮,请尝试:方法一:root.after(5000,numRamdom) #after 5 sec it will execute function但请记住,如果用户在 5 秒内没有正确输入,则会弹出一些错误。方法2:def numRamdom(event):......entryWriteNumber.bind('<Return>',numRamdom)这样,如果您在输入小部件中按 Enter 键(输入数据后),它将运行该功能。希望这有帮助,如果有任何错误请告诉我。

一只萌萌小番薯

这是一个基于您的代码的完整工作示例。您的问题是尝试在其中包含任何内容之前转换条目的内容。要解决此问题,您可以添加一个调用命令的按钮numRamdom()import tkinterimport randomwindow = tkinter.Tk()window.geometry('600x500')x = random.randint(1,300)remainingTime = TrueAttempts = 4def numRamdom():&nbsp; &nbsp; global Attempts, lblClue, x&nbsp; &nbsp; if Attempts > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numWritten = int(entryWriteNumber.get())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if numWritten < x:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lblClue.configure(text = 'Its a bigger number')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Attempts = Attempts -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif numWritten > x:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lblClue.configure(text = 'Its a smaller number')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Attempts = Attempts -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lblClue.configure(text = 'Congratulations ;)')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remainingTime = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #return remainingTime, countdown(0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Attempts == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remainingTime = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #return remainingTime, countdown(0), Attempts, gameOver()&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; 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。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python