我一直在努力让边界输入在我的小游戏中运行。
起初我收到了这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__
return self.func(*args)
File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 173, in buttonclick_gamescreen
if entryx > 10 or entryx < -10 or entryy > 10 or entryy < -10 :
TypeError: unorderable types: str() > int()
所以我意识到我必须将从入口窗口小部件中获得的字符串转换为整数,我查找了在stackoverflow和web上的其他地方执行的代码,但它似乎没有工作。
我试过了两个:
int (e1.get())
int (e2.get())
和
int (entryx)
int (entryy)
内
while pressed == 8 :
int (e1.get())
int (e2.get())
entryx = e1.get()
entryy = e2.get()
answerx = answerlistx[randomimage]
answery = answerlisty[randomimage]
if entryx == answerx and entryy == answery:
canvas.delete(images)
randomimage = random.randrange(0,49+1)
scorecounter = scorecounter + 1
game = PhotoImage(file=imagelist[randomimage])
images = canvas.create_image(30, 65, image = game, anchor = NW)
e1.delete(0, END)
e2.delete(0, END)
pressed = ''
if entryx > 10 or entryx < -10 or entryy > 10 or entryy < -10 :
wrong = canvas.create_image(30, 65, image = outside, anchor = NW)
e1.delete(0, END)
e2.delete(0, END)
pressed = ''
else:
wrong = canvas.create_image(30, 65, image = incorrect, anchor = NW)
e1.delete(0, END)
e2.delete(0, END)
pressed = ''
没有运气。这是从我到目前为止所阅读的内容开始的,但我仍然从上面得到同样的错误。有人可以告诉我,我做错了吗?
提前致谢!
答案 0 :(得分:1)
以下陈述:
int (e1.get()) # This is actually doing nothing.
int (e2.get())
entryx = e1.get()
entryy = e2.get()
不会将整数值分配给entryx
或entryy
。也许你想要这样的东西:
entryx = int (e1.get())
entryy = int (e2.get())
答案 1 :(得分:1)
行int (e1.get())
和int (e2.get())
实际上什么也没做。相反,您应该将e1.get()
和e2.get()
转换为int
,同时将其分配给entryx
和entryy
:
entryx = int(e1.get())
entryy = int(e2.get())
int()
不会转换商品:
>>> s = "100"
>>> int(s)
100
>>> s
'100'
>>> type(s)
<type 'str'>