gpt4 book ai didi

python - 如何修复函数tkinter的int float

转载 作者:行者123 更新时间:2023-12-03 08:39:17 25 4
gpt4 key购买 nike

所以我试图创建一个函数,检查用户是否输入了str内容,它将打印出一条错误消息,说print("You can only use a number ranging from 1 - 4, setting minimum_offer to 1.")但是当我运行我的代码时,它说这是错误代码:

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "d:/Python Code stuff I did/print.py", line 218, in <lambda>
button = Button(root,text="Change Config", width=20, height=3, bg="#0f0f0f",fg="#ffffff", command=lambda:[do_it(), do_it1(), do_it2(), do_it3(),do_the_it(),do_the_it1()])
File "d:/Python Code stuff I did/print.py", line 42, in do_it
userInput = number.get()
File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 537, in get
return int(self._tk.getdouble(value))
_tkinter.TclError: expected floating-point number but got "wa"
这是我的代码,给出错误:
number_1 = StringVar()
def do_the_it():
userinput4 = number_1.get()
try:
int(userinput4)
updater.read('settings.ini')
updater['Trading Settings']['minimum_offer'].value = number_1.get()
updater.update_file()
except Exception as error:
print("You can only use a number ranging from 1 - 4, setting minimum_offer to 1.")
updater.read('settings.ini')
updater['Trading Settings']['minimum_offer'].value = 1
updater.update_file()

updater.read('settings.ini')
updater['Trading Settings']['maximum_offer'].value = number_2.get()
updater.update_file()
a = updater['Trading Settings']['minimum_offer'].value
if int(number_1.get()) > 4:
print("Number can only exceed to 4")
updater.read('settings.ini')
updater['Trading Settings']['minimum_offer'].value = 1
updater.update_file()

最佳答案

为了进行错误处理,您必须检查是否确实可以转换为int。您可以使用try块。

accept = True
try:
tmp = int(number_1.get())
except:
accept = False
然后,如果accept为true,则只能执行第二部分(if块):
if accept:
if int(number_1.get()) > 4:
### Rest of your code
else:
print("Enter a number you joker")

关于python - 如何修复函数tkinter的int float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63372089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com