2015-04-26 40 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()