【问题标题】:TypeError: unorderable types: str() < int()TypeError:不可排序的类型:str() < int()
【发布时间】:2015-09-16 20:47:33
【问题描述】:

需要有关此错误的帮助。

我的代码:

from tkinter import *

def shouldIEat():

    if calories.get() < 1523 :
     answer.set("Eat food as soon as possible")

    elif calories.get() > 1828 :
     answer.set("Stop eating")

    else:
     answer.set("You can eat something, but don't go over 1828")

window = Tk()

answer = StringVar()
calories = IntVar()



calories.set(0)


caloriesLabel = Label(window, text = "How many calories have you consumed?")
caloriesLabel.grid( row = 1)

calories = Entry(width = 10)
calories.grid (row = 1, column = 1)

eatButton = Button(window, text = "Should I eat?" , command=shouldIEat).grid( row = 2 , column = 1)
quitButton = Button(window, text = "Quit" ,  command=window.quit).grid( row = 2 , column = 2)

window.mainloop()

我的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
    return self.func(*args)
  File "D:/My Documents/School/Scripting Lang/Project", line 8, in shouldIEat
    if calories.get() < 1523 :
TypeError: unorderable types: str() < int()

【问题讨论】:

  • 该代码应该做什么?您在哪里使用 answerIntVar()
  • @ChrisMagnussen:这里实际上不需要显式转换为intIntVar 对象会为您完成转换。

标签: python tkinter


【解决方案1】:

您的代码将 calories 设置为 Tkinter IntVar,但随后它通过创建同名条目来破坏它。您需要为 Entry 指定一个不同的名称,然后在 Entry 构造函数中使用 textvariable 参数附加 calories IntVar。

此外,您从未创建小部件来显示答案。

from tkinter import *

def shouldIEat():
    if calories.get() < 1523 :
        answer.set("Eat food as soon as possible")
    elif calories.get() > 1828 :
        answer.set("Stop eating")
    else:
        answer.set("You can eat something, but don't go over 1828")

window = Tk()

answer = StringVar()
calories = IntVar()

calories.set(0)
#answer.set('')

caloriesLabel = Label(window, text = "How many calories have you consumed?")
caloriesLabel.grid(row = 1, column = 0)

caloriesEntry = Entry(width = 10, textvariable=calories)
caloriesEntry.grid(row = 1, column = 1)

Button(window, text = "Should I eat?", command=shouldIEat).grid(row = 2, column = 1)
Button(window, text = "Quit" , command=window.quit).grid(row = 2, column = 2)

answerLabel = Label(window, textvariable=answer)
answerLabel.grid(row = 2, column = 0)

window.mainloop()

我们真的不需要初始化answer,但这样做更简洁。如果需要,您可以使用它来显示一些简单的说明。

我应该提到您的代码的另一个小问题。

eatButton = Button(window, text = "Should I eat?" , command=shouldIEat).grid( row = 2 , column = 1)

这会创建一个Button 对象,然后调用它的.grid() 方法。这很好,但是.grid() 方法返回None,它确实返回Button 对象,所以将返回值保存到eatButton 是没有意义的。您不需要为该程序保留对该按钮的引用,这就是为什么我将其更改为

Button(window, text = "Should I eat?", command=shouldIEat).grid(row = 2, column = 1)

在我的版本中。但是当您确实需要保留对小部件的引用时,您应该在一行上构造它,然后在另一行上应用.grid()(或.pack())方法,就像您对@ 所做的那样987654337@.


顺便说一句,在 Python 中,calories_label 形式的名称优先于 caloriesLabel。详情请见PEP-008

【讨论】:

    【解决方案2】:

    正如您在 TypeError 中看到的,您将字符串与整数进行比较。

    在进行比较之前将字符串转换为整数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      相关资源
      最近更新 更多