2015-04-26 41 views
0

我對這個項目有點麻煩。我必須使用鑰匙手柄來創建鐘擺,而我爲鑰匙上下的代碼似乎沒有工作。 「向上」是爲了使鐘擺更快,「下」使其變慢。這是迄今爲止的代碼。有人可以請幫忙嗎?擺動擺不起作用

from tkinter import * # Import tkinter 
import math 
width = 200 
height = 200 
pendulumRadius = 150 
ballRadius = 10 
leftAngle = 120 
rightAngle = 60 

class MainGUI: 
    def __init__(self): 
     self.window = Tk() # Create a window, we may call it root, parent, etc 
     self.window.title("Pendulum") # Set a title 

     self.canvas = Canvas(self.window, bg = "white", 
          width = width, height = height) 
     self.canvas.pack() 

     self.angle = leftAngle # Start from leftAngle 
     self.angleDelta = -1 # Swing interval 
     self.delay = 200 
     self.window.bind("<Key>",self.key) 
     self.displayPendulum() 
     self.done = False 
     while not self.done: 
      self.canvas.delete("pendulum") # we used delete(ALL) in previous lab 
              # here we only delete pendulum object 
              # in displayPendulum we give the tag 
              # to the ovals and line (pendulum) 
      self.displayPendulum()   # redraw 
      self.canvas.after(self.delay) # Sleep for 100 milliseconds 
      self.canvas.update() # Update canvas 


     self.window.mainloop() # Create an event loop 

    def displayPendulum(self): 
     x1 = width // 2; 
     y1 = 20; 

     if self.angle < rightAngle: 
      self.angleDelta = 1 # Swing to the left 
     elif self.angle > leftAngle: 
      self.angleDelta = -1 # Swing to the right 

     self.angle += self.angleDelta 
     x = x1 + pendulumRadius * math.cos(math.radians(self.angle)) 
     y = y1 + pendulumRadius * math.sin(math.radians(self.angle)) 

     self.canvas.create_line(x1, y1, x, y, fill="blue", tags = "pendulum") 
     self.canvas.create_oval(x1 - 2, y1 - 2, x1 + 2, y1 + 2, 
           fill = "red", tags = "pendulum") 
     self.canvas.create_oval(x - ballRadius, y - ballRadius, 
           x + ballRadius, y + ballRadius, 
           fill = "green", tags = "pendulum") 
    def key(self,event): 
     print(event.keysym) 
     print(self.delay) 
     if event.keysym == 'up': 
      print("up arrow key pressed, delay is",self.delay) 
      if self.delay >10: 
       self.delay -= 1 
     if event.keysym == 'Down': 
      print("Down arrow key pressed,delay is",self.delay) 
      if self.delay < 200: 
       self.delay += 1 
     if event.keysym=='q': 
      print ("press q") 
      self.done = True 
      self.window.destroy() 

    MainGUI() 
+1

我已經從您的問題標題中刪除了python這個詞,並分割了一些行,以便更少的滾動來讀取您的代碼。想想什麼是不正常工作,然後將其添加到問題中,也許重寫問題以提出問題。 – icedwater

回答

0

問題的根源是,事件的keysym是"Up"但你比較它的全部小寫的「向上」。自然,每次比較失敗。更改if語句if event.keysym == 'Up':

提高動畫

在你感興趣的情況下,有一個更好的方式做動畫的Tkinter比寫自己的無限循環。 Tkinter已經有一個無限循環運行(mainloop),所以你可以利用它。

創建一個繪製一幀的函數,然後讓該函數安排自己在將來的某個點再次被調用。具體來說,刪除整個「while」循環一起displayFrame()單呼,然後定義displayFrame這樣的:

def drawFrame(self): 
    if not self.done: 
     self.canvas.delete("pendulum") 
     self.displayPendulum() 
     self.canvas.after(self.delay, self.drawFrame) 

改善綁定

一般來說,你的代碼會稍微容易管理並測試您是否具有特定鍵的特定綁定。因此,您可以爲每個密鑰設置特定的功能,而不是單個全部捕獲功能。

由於您的應用程序支持的向上鍵,向下鍵和「Q」鍵,我推薦三個綁定:

self.window.bind('<Up>', self.onUp) 
self.window.bind('<Down>', self.onDown) 
self.window.bind('<q>', self.quit) 

然後,您可以定義每個函數來只做一兩件事:

def onUp(self, event): 
    if self.delay > 10: 
     self.delay -= 1 

def onDown(self, event): 
    if self.delay < 200: 
     self.delay += 1 

def onQuit(self, event): 
    self.done = True 
    self.window.destroy()