TypeError: unorderable types: IntVar() > int() [du

2019-07-28 16:26发布

This question already has an answer here:

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

1条回答
甜甜的少女心
2楼-- · 2019-07-28 16:46

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.

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

in your case, change:

if counter > 79:

by

if counter.get() > 79:

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

查看更多
登录 后发表回答