【问题标题】:how to make a greater than or lower than with tkinter StringVar如何使用 tkinter StringVar 使大于或小于
【发布时间】:2020-08-12 06:07:45
【问题描述】:

我的代码一直报这个错误:

Exception in Tkinter callback
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 209, 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 149, in do_the_it
    if str(number_1) > 4:
TypeError: '>' not supported between instances of 'str' and 'int'

我希望它检查该特定数字是否大于 4,例如我输入一个数字,例如说它的 7 我希望它打印,that number is too high 这是我的代码:

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

【问题讨论】:

  • 试试int(number_1.get()) &gt; 4

标签: python-3.x function if-statement tkinter


【解决方案1】:

您不能将字符串与数字进行比较。使用:

if float(number_1.get()) > 4:

您也可以使用int(),但如果某些小丑输入小数点,这将防止错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    相关资源
    最近更新 更多