【问题标题】:How to fix invalid literal for int() with base 10: ''?如何使用基数 10 修复 int() 的无效文字:''?
【发布时间】:2019-07-08 00:28:06
【问题描述】:

使用 python3.7 和 tkinter 制作 GUI,我从 2 个文本框中获取输入,然后使用数学公式将其导出。

我查看了其他表单并尝试了他们的建议,但找不到解决方法。我在函数内部和外部都尝试过全局,在函数之前设置变量。

def retrieve_input():
    global InputValue
    global InputValue2
    global Delay
    InputValue=tasks.get()
    InputValue2=proxies.get()
    print(InputValue)
    print(InputValue2)
    Delay=3500/int(InputValue)*int(InputValue2)
    print(Delay)
retrieve_input()
Label (window, width=12, text=Delay,bg="white",fg="Pink", font="none 22 bold") .grid(row=5, column=0,sticky=W)

错误:

  File ", line 29, in retrieve_input
    Delay=3500/int(InputValue)*int(InputValue2)
ValueError: invalid literal for int() with base 10: ''

【问题讨论】:

  • 您正在尝试将空字符串转换为 int,如错误所示。检查您的输入。
  • @Carcigenicate 是的,我知道,我只是不知道如何让它不为空,因为我需要等待客户使用它。
  • 当文本框被修改(stackoverflow.com/questions/6548837/…)或按下某种提交按钮时,您将调用此函数。
  • 答案就在您刚才提出的问题中。您需要等待客户使用教科书。所以在这发生之前不要运行代码。
  • 您可以使用0 if not InputValue else int(InputValue) 代替int(InputValue2)(将0 替换为您想要的默认值)。

标签: python python-3.x tkinter


【解决方案1】:

这意味着您将一个空字符串传递给 int 类构造函数。您实际上是在致电int('')。看起来tasks.get()proxies.get() 正在返回空字符串。

简而言之:不要将空字符串传递给 int()。

至于你留下的评论:

try:
    Delay=3500/int(InputValue)*int(InputValue2)
except ValueError:
    pass
    #Handle a case in which the input is ''

【讨论】:

  • 谢谢,您如何建议不要将其设为空字符串,因为它正在等待客户输入?
  • 已编辑! @isuckatcoding
猜你喜欢
  • 2021-09-17
  • 2021-10-24
  • 1970-01-01
  • 2013-05-31
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 2021-06-25
  • 2017-06-18
相关资源
最近更新 更多