2013-07-16 129 views
0

我想創建一個while循環,它將檢查條目窗口小部件e1和e2從列表的正確答案,如果它是正確的,它會隨機選擇另一個圖像放置到畫布,如果它是錯誤的,那麼它會將不正確的文字打印到屏幕上。 我目前擁有的while循環不會給我任何錯誤,但它掛起了程序,我無法弄清楚是什麼導致了這個問題。Python-雖然循環掛沒有錯誤

編輯:我現在能夠得到的遊戲畫面加載後,把這一行結束canvas.mainloop()然而,如果在while循環語句不工作,我也遇到一個新的錯誤,不會影響我在底部添加的程序。其壓制== 8的價值來自於

def gamescreen(): 
    imagelist = ["1.gif","2.gif","3.gif","4.gif","5.gif","6.gif","7.gif","8.gif","9.gif","10.gif","11.gif","12.gif","13.gif","14.gif","15.gif","16.gif","17.gif","18.gif","19.gif","20.gif","21.gif","22.gif","23.gif","24.gif","25.gif","26.gif","27.gif","28.gif","29.gif","30.gif","31.gif","32.gif","33.gif","34.gif","35.gif","36.gif","37.gif","38.gif","39.gif","40.gif","41.gif","42.gif","43.gif","44.gif","45.gif","46.gif","47.gif","48.gif","49.gif","50.gif"] 
    answerlistx = [-1, -3, 3, 4, -7, 8, 9, 10, -10, 2, -7, 7, -1, -5, -6, -9, -7, 10, -4, 8, 1, -8, -10, -1, -3, -7, -3, 7, 3, -4, 1, -8, -4, 9, -5, -10, 10, 2, 2, -10, 4, 9, -3, 6, 10, -6, 4, 9, -10, -10] 
    answerlisty = [3, -6, -5, 3, -2, 4, -4, -3, 4, -6, 1, 2, 2, 3, 2, -1, -5, 1, -3, 1, -2, -2, -5, -3, -2, -6, -3, 6, 2, 0, -5, 6, -4, 4, 1, -6, 0, -6, 5, 2, 4, -4, -2, 0, -3, -6, -4, 1, -3, 1] 

    canvas.bind("<Button-1>", buttonclick_gamescreen) 
    canvas.pack(expand = YES, fill = BOTH) 
    photo = PhotoImage(file="gamescreen.gif") 
    canvas.create_image(1, 1, image = photo, anchor = NW) 
    e1 = Entry(canvas, width = 11) 
    e2 = Entry(canvas, width = 11) 
    canvas.create_window(390, 501, window=e1, anchor = NW) 
    canvas.create_window(551, 501, window=e2, anchor = NW) 
    canvas.after(1, countdowntimer) 
    while cdtimer > 0: 
     print("start of while loop") 
     randomimage = random.randrange(0,49+1) 
     game = PhotoImage(file=imagelist[randomimage]) 
     images = canvas.create_image(30, 65, image = game, anchor = NW) 
     if pressed == 8 and e1 == answerlistx[randomimage] and e2 == answerlisty[randomimage]: 
      print("correct") 
      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) 
     elif pressed == 8 and e1 != answerlistx[randomimage] or pressed == 8 and e2 != answerlisty[randomimage]: 
      print("incorrect") 
      wronganswer = canvas.create_text(400, 200, text="Incorrect", font="Ubuntu 29 bold", fill='red', anchor=NW) 
      e1.delete(0, END) 
      e2.delete(0, END) 
      canvas.after(1500,(canvas.delete(wronganswer))) 
     canvas.mainloop() 

這就是:

def buttonclick_gamescreen(event): 
    global pressed 
    pressed = "" 

    if event.x >853 and event.x <957 and event.y > 8 and event.y < 56 : pressed = 7 
    if event.x >666 and event.x <947 and event.y > 491 and event.y < 534 : pressed = 8 

    if pressed == 7 : 
     window.destroy() 
    if pressed == 8: 
     print("next button") 

當我按在gamescreen退出按鈕,就會出現此新的錯誤

這裏是更新while循環()或在屏幕上()

start of while loop 
start of while loop 
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 114, in buttonclick_mainscreen 
    gamescreen() 
    File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 60, in gamescreen 
    game = PhotoImage(file=imagelist[randomimage]) 
    File "/usr/lib/python3.2/tkinter/__init__.py", line 3231, in __init__ 
    Image.__init__(self, 'photo', name, cnf, master, **kw) 
    File "/usr/lib/python3.2/tkinter/__init__.py", line 3172, in __init__ 
    raise RuntimeError('Too early to create image') 
RuntimeError: Too early to create image 

如果有人能指出我在正確的方向。 PS:這是完整的代碼incase我還沒有提供足夠的信息Link to full code on pastebin

在此先感謝!

+0

在那裏放一些打印件進行調試。打印cdtimer,按下,e1,e2和答案列表條目。 – mnagel

+0

也使用常量/可變量而不是幻數。 – mnagel

回答

1

你的while循環沒有給主循環一個運行的機會。由於這個原因,一旦buttonclick_mainscreen調用gamescreenpressed保持爲1並且從不改變其值。

從事件循環調用的任何回調都必須立即返回到事件循環,否則程序將無法響應。不要停留在while循環中,只需從gamescreen返回並從處理用戶事件(canvas.bind)和超時(canvas.after)的回調中完成其餘工作。當前循環的每次迭代都將是回調的一次調用。

首先令人費解的是以這種方式重寫算法,但這就是事件循環的工作原理。

+0

嘿那裏謝謝你的答案,但我對你的陳述有點困惑「當前循環的每個迭代都必須作爲一個新的回調函數的調用來執行。」我無法把頭圍繞在那裏,你有沒有機會多解釋它或者用示例代碼來展示它?谷歌沒有太多的幫助:/ 感謝您的時間。 – ThatsNotMyName

+0

@ThatsNotMyName我已經用更詳細的描述更新了第二段。如果你仍然不明白,你可能想拿一本關於Tkinter的書來解釋基本概念。 – user4815162342

+0

此外,[接受的答案](http://stackoverflow.com/a/17538528/1600898)對您之前的問題之一已經包含一個示例,顯示如何執行此處描述的操作。 – user4815162342