简体   繁体   English

ValueError:int() 的无效文字,基数为 10: '' (Tkinter)

[英]ValueError: invalid literal for int() with base 10: '' (Tkinter)

When I try to run this code, a ValueError appears alluding to the function numRandom .当我尝试运行此代码时,出现ValueError暗指函数numRandom I though Python could pass a string representation of a int to an int .我虽然 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()

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.问题是因为当代码运行时,它直接调用numRamdom() ,也就是说,最初入口小部件是空的,他们用那些空的入口小部件运行它,因此出现错误。 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(): .确保之前说这个mainloop()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.但请记住,如果用户没有在 5 秒内正确输入,则会弹出一些错误。

METHOD-2:方法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.这样,如果您在输入小部件中按 Enter 键(输入数据后),它将运行该功能。

Hope this helps, do let me know if any errors.希望这会有所帮助,如果有任何错误,请告诉我。

Cheers干杯

Here's a fully working example based on your code.这是一个基于您的代码的完整示例。 Your problem was trying to convert the contents of the Entry before anything was inside of it.您的问题是尝试在 Entry 中的任何内容之前转换它的内容。 To fix this problem, you can add a button which calls the command numRamdom()要解决此问题,您可以添加一个调用命令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()

You'll still get an error if the value passed is not able to be converted into an integer, but that's easy to fix with an if statement.如果传递的值无法转换为整数,您仍然会收到错误消息,但使用if语句很容易修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tkinter Trace和ValueError:int()的基数为10的无效文字:&#39;&#39; - Tkinter Trace and ValueError: invalid literal for int() with base 10: '' python tkinter ValueError: int() 的无效文字,基数为 10:'' - python tkinter ValueError: invalid literal for int() with base 10: '' ValueError:以10为底的int()的无效文字:&#39;&#39;python 3.4 tkinter - ValueError: invalid literal for int() with base 10: '' python 3.4 tkinter ValueError: int() 以 10 为底的无效文字:'' 使用 tkinter - ValueError: invalid literal for int() with base 10: '' Using tkinter ValueError:int() 的无效文字,基数为 10:&#39;26.02.2018&#39; - ValueError: invalid literal for int() with base 10: '26.02.2018' 第8行:ValueError:int()以10为底的无效文字:“&#39; - Line 8: ValueError: invalid literal for int() with base 10: ' ' ValueError:int()以10为基的无效文字:“ skip” - ValueError: invalid literal for int() with base 10: 'skip' ValueError:以10为底的int()的无效文字:&#39;&#39; - ValueError: invalid literal for int() with base 10: '' ValueError:以10为底的int()的无效文字:&#39;SOH&#39; - ValueError: invalid literal for int() with base 10: 'SOH' 错误 - ValueError:基数为10的int()的文字无效:&#39;&#39; - Error - ValueError: invalid literal for int() with base 10: ' '
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM