python - Swinging pendulum does not work -
i having little trouble project. have create pendulum using key handles , code have key's , down don't seem working. "up" suppose make pendulum go faster , "down" makes go slower. code have far. can please help.
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 window, may call root, parent, etc self.window.title("pendulum") # set title self.canvas = canvas(self.window, bg = "white", width = width, height = height) self.canvas.pack() self.angle = leftangle # start 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") # used delete(all) in previous lab # here delete pendulum object # in displaypendulum give tag # ovals , line (pendulum) self.displaypendulum() # redraw self.canvas.after(self.delay) # sleep 100 milliseconds self.canvas.update() # update canvas self.window.mainloop() # create event loop def displaypendulum(self): x1 = width // 2; y1 = 20; if self.angle < rightangle: self.angledelta = 1 # swing left elif self.angle > leftangle: self.angledelta = -1 # swing 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()
the root of problem event keysym "up"
comparing all-lowercase "up". naturally, comparison fails every time. change if statement if event.keysym == 'up':
improving animation
in case you're interested, there better way animation in tkinter write own infinite loop. tkinter has infinite loop running (mainloop), can take advantage of that.
create function draws 1 frame, have function arrange called again @ point in future. specifically, remove entire "while" loop single call displayframe()
, , define displayframe
this:
def drawframe(self): if not self.done: self.canvas.delete("pendulum") self.displaypendulum() self.canvas.after(self.delay, self.drawframe)
improving bindings
generally speaking, code marginally easier manage , test if have specific bindings specific keys. instead of single catch-all function, can have specific functions each key.
since app supports key, down key, , "q" key, recommend 3 bindings:
self.window.bind('<up>', self.onup) self.window.bind('<down>', self.ondown) self.window.bind('<q>', self.quit)
you can define each function 1 thing:
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()
Comments
Post a Comment