This question already has answers here:
Add tkinter's intvar to an integer
                                
                                    (2个答案)
                                
                        
                                2年前关闭。
            
                    
我正在尝试在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()

最佳答案

您必须比较相同的类型(或兼容的类型)。在那种情况下,似乎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将整数和IntVar对象混合在一起。

09-27 05:59