我已经遍布谷歌找到我的代码的解决方案,但我找不到任何。
这是我的代码,它是一个简单的点击器,如游戏(只是想学习Tkinter的基础知识)。我想要做的是收集油,然后卖掉它,但我需要现在的石油计数器去0,所以我不赚钱:
from tkinter import *
import sys
root = Tk()
root.geometry("200x300")
root.title("oil clicker")
counter = IntVar()
cash = IntVar()
def onClick(event=None):
counter.set(counter.get() + 2)
def negOnClick(event=None):
cash.set(cash.get() * 45)
counter.set(counter.get() - counter)
addLabel = Label(root, textvariable=counter).pack()
addButton = Button(root, text="Increase", command=onClick, fg="dark green", bg = "white").pack()
negetiveLabel = Label(root, textvariable= cash).pack()
negetiveButton = Button(root, text="Sell oil for cash", command=negOnClick, fg="dark green", bg = "white").pack()
root.mainloop()
当我运行该程序然后尝试卖掉它给我这个错误的油:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:/Users/Blank/Documents/python clicker game/test2.py", line 16, in negOnClick
counter.set(counter.get() - counter)
TypeError: unsupported operand type(s) for -: 'int' and 'IntVar'
感谢任何可以提供帮助的人。