Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:无序类型:IntVar()>;int()_Python_Tkinter_Int - Fatal编程技术网

Python TypeError:无序类型:IntVar()>;int()

Python TypeError:无序类型:IntVar()>;int(),python,tkinter,int,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 =

我试图在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
>>> 


正如评论所暗示的,你在其他地方也有这些问题。因此,使用
.get
设置为整数和
IntVar
对象的混合位置。

我们至少需要对异常进行完整的回溯。如果将
IntVar
转换为
int
,会发生什么情况:
如果int(计数器)>79:
计数器
是一个
IntVar
,您将其与一个整数进行比较。也许您想使用
counter.get()>79
来获取存储在
IntVar
中的值?还有,为什么要声明两次全局值?接下来您将遇到
counter=counter-80
的问题。您也需要在那里使用
counter.set()
counter.get()
。您的术语很混乱。在
IntVar
上调用
get
并不是将其强制转换为整数。“强制转换”通常意味着“转换”,而不是转换
IntVar
,而是获取它的值。你是对的。最近C++太多:=谢谢它现在工作:D
if counter > 79:
if counter.get() > 79: