python - Why does this after statement not work? -
this question has answer here:
- tkinter: how use after method 1 answer
i have after statement in move_r function. when hit space bar says
typeerror: move_r() missing 1 required positional argument: 'event'
i relatively know means can't figure out change program supposed draw rocket, , when click space rocket moves up. unfortunately program goes 100 everytime click space instead of going once hit space goal. code is:
from tkinter import * import random, time tk = tk() tk.wm_attributes('-topmost', 1) canvas = canvas(tk, width=1000, height=1000, bd=0) canvas.pack() def rocket(): #draws rocket rocketbase=canvas.create_rectangle(x2+50, y2-50, x3-50, y3-90, fill ='blue') rrocketfin=canvas.create_rectangle(x2+60, y2-50, x3-40, y3-20, fill ='#e69100') lrocketfin=canvas.create_rectangle(x2+40, y2-50, x3-60, y3-20, fill ='#e69100') mrocketfin=canvas.create_rectangle(x2+54, y2-50, x3-53, y3-20, fill ='#e69100') lrockethead=canvas.create_rectangle(x2+45, y2-125, x3-60, y3-70, fill ='#e69100') rrockethead=canvas.create_rectangle(x2+60, y2-125, x3-45, y3-70, fill ='#e69100') def move_r(event): if event.keysym == 'space': global y2 global y3 y2-=100 y3-=100 canvas.after(100, move_r ) x2=460 y2=940 x3=570 y3=900 r1=none tk.bind('<keypress-space>', move_r) while true: background = canvas.create_rectangle(0, 0, 1000, 1000, fill = 'red') base=canvas.create_rectangle(0, 1000, 1000, 900, fill='#86592d') rocket() tk.update() tk.mainloop()
here rocket moves; doesn't fix problems, addresses question asked:
the event key_press - generated when press space bar, , sent event handler.
the event handler receives event, , acts on (identifies event, , directs flow should - here, move rocket).
move rocket that, moves rocket callback loop.
from tkinter import * import random, time tk = tk() tk.wm_attributes('-topmost', 1) canvas = canvas(tk, width=1000, height=1000, bd=0) canvas.pack() def rocket(): #draws rocket rocketbase=canvas.create_rectangle(x2+50, y2-50, x3-50, y3-90, fill ='blue') rrocketfin=canvas.create_rectangle(x2+60, y2-50, x3-40, y3-20, fill ='#e69100') lrocketfin=canvas.create_rectangle(x2+40, y2-50, x3-60, y3-20, fill ='#e69100') mrocketfin=canvas.create_rectangle(x2+54, y2-50, x3-53, y3-20, fill ='#e69100') lrockethead=canvas.create_rectangle(x2+45, y2-125, x3-60, y3-70, fill ='#e69100') rrockethead=canvas.create_rectangle(x2+60, y2-125, x3-45, y3-70, fill ='#e69100') def handle_key_press(e): if e.keysym == 'space': move_r() def move_r(): global y2 global y3 y2 -= 50 y3 -= 50 rocket() tk.update() canvas.after(100, move_r) x2=460 y2=940 x3=570 y3=900 r1=none tk.bind('<keypress-space>', handle_key_press) background = canvas.create_rectangle(0, 0, 1000, 1000, fill = 'red') base = canvas.create_rectangle(0, 1000, 1000, 900, fill='#86592d') rocket() tk.update() tk.mainloop()
Comments
Post a Comment