TypeError:不可排序的类型:IntVar()> int() [英] TypeError: unorderable types: IntVar() > int()

查看:88
本文介绍了TypeError:不可排序的类型:IntVar()> int()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python上制作答题器游戏,但我不断收到错误"TypeError: unorderable types: IntVar() > int()",我看过其他帖子,但仍然不了解.get的内容.到目前为止,这是我的代码:

I'm trying to make a clicker game on python but I keep getting the error "TypeError: unorderable types: IntVar() > int()" I have looked at other posts and still don't understand the .get thing. here is my code so far:

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()

推荐答案

您必须比较相同的类型(或兼容的类型).在这种情况下,似乎IntVar对象不能直接与int比较.但是它有一个get方法,该方法返回一个整数.

you have to compare identical types (or compatible types). In that case, it seems that the IntVar object cannot be directly compared to int. But it has a get method that returns an integer.

我不是tk专家,但这重现了问题并提供了解决方法:

I'm not a tk specialist but this reproduces the issue and provides a fix:

>>> 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,其中整数& IntVar对象混合在一起.

As comments suggested, you have those issues at other places. So use .get and .set where integers & IntVar objects are mixed.

这篇关于TypeError:不可排序的类型:IntVar()&gt; int()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆