簡體   English   中英

Python - 將String轉換為Integer以進行邊界輸入

[英]Python - Converting String to Integer for boundary inputs

我一直在努力讓邊界輸入在我的小游戲中運行。

起初我得到了這個錯誤:

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 = ''

沒有運氣。 這是從我到目前為止所閱讀的內容開始的,但我仍然從上面得到同樣的錯誤。 有人能告訴我,我做錯了嗎?

提前致謝!

以下陳述:

int (e1.get())   # This is actually doing nothing.
int (e2.get())
entryx = e1.get()
entryy = e2.get()

不會將整數值分配給entryxentryy 也許你想要這樣的東西:

entryx = int (e1.get())
entryy = int (e2.get())

int (e1.get())int (e2.get())實際上什么也沒做。 相反,你應該將e1.get()e2.get()轉換為int同時將它們分配給entryxentryy

entryx = int(e1.get())
entryy = int(e2.get())

int()不會轉換項目:

>>> s = "100"
>>> int(s)
100
>>> s
'100'
>>> type(s)
<type 'str'>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM