TypeError:unorderable类型:IntVar()> INT()

时间:2017-09-26 12:00:42

标签: python tkinter int

我正在尝试在python上制作一个点击游戏,但我一直收到错误"TypeError: unorderable types: IntVar() > int()"我看过其他帖子但仍然不理解.get这个问题。这是我目前的代码:

import tkinter
from tkinter import *
import sys

root = tkinter.Tk()
root.geometry("160x100")
root.title("Cliker game")
global counter
counter = tkinter.IntVar()
global multi
multi = 1

def onClick(event=None):
    counter.set(counter.get() + 1*multi)

tkinter.Label(root, textvariable=counter).pack()
tkinter.Button(root, text="I am Cookie! Click meeeeee", command=onClick, 
fg="dark green", bg = "white").pack()


clickable = 0
def button1():
        global multi
        global counter
        if counter > 79:   # this is the line where the error happens
            counter = counter - 80
            multi = multi + 1
            print ("you now have a multiplier of", multi)
        else:
            print ("not enough moneys!")
b = Button(text="+1* per click, 80 cookies", command=button1)
b.pack()


root.mainloop()

1 个答案:

答案 0 :(得分:2)

您必须比较相同类型(或兼容类型)。在这种情况下,似乎无法将IntVar对象直接与int进行比较。但它有get方法返回整数。

我不是tk专家,但这会重现问题并提供修复:

>>> root = tkinter.Tk()
>>> counter = tkinter.IntVar()
>>> counter.get()
0
>>> counter < 10
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
TypeError: unorderable types: IntVar() < int()
>>> counter.get() < 10
True
>>> 

在你的情况下,改变:

if counter > 79:

通过

if counter.get() > 79:

正如评论所示,您在其他地方遇到了这些问题。因此,使用.get.set整数&amp; IntVar个对象混合在一起。

相关问题