2017-09-26 27 views
1

我想在python上做一個點擊者遊戲,但我不斷收到錯誤"TypeError: unorderable types: IntVar() > int()"我看了其他帖子,仍然不明白.get的事情。這裏是我的代碼到目前爲止:TypeError:無法處理的類型:IntVar()> int()

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

我們至少需要*,例外的完整回溯。 –

+0

如果你把'IntVar'變成像這樣的'int'會發生什麼:'如果int(counter)> 79:' – Joe

+1

'counter'是一個'IntVar',你將它與一個整數進行比較。也許你想用'counter.get()> 79'來獲取存儲在'IntVar'中的值? –

回答

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其中整數& IntVar對象是混合的。

+0

你開始回到我的話你的術語是混亂。在'IntVar'上調用'get'不是將它變成一個整數。 「投射」通常意味着「轉換」,並且您不轉換「IntVar」,您正在提取它的值。 –

+0

你是對的。最近太多的C++:= –

+0

謝謝你現在的工作:D –

相關問題