0

I have the after statement in the move_r function. But when I hit space bar it says

TypeError: move_r() missing 1 required positional argument: 'event'

I relatively know what this means but I can't figure out what to change My program is supposed to draw a rocket, and then when you click space the rocket moves up. Unfortunately my program only goes up 100 everytime i click space instead of going up by itself once a hit space which is my goal. My 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()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Maximus
  • 71
  • 9
  • 1
    You're calling `after` correctly, but your `move_r` function requires an argument that you're not including within the `after` call if that makes sense. – Leb Apr 25 '16 at 01:14
  • It doesn't really could you explain – Maximus Apr 25 '16 at 01:18
  • Callback flaws aside, that rocket will never move if you don't actuate its position - you must get rid of that while loop too! (someone told you in your last question 2 hours ago) – Reblochon Masque Apr 25 '16 at 01:24

1 Answers1

1

Here is a rocket that moves; it doesn't fix all your problems, but it addresses the question you asked:

The event is a key_press - it is generated when you press the space bar, and sent to the event handler.

The event handler receives the event, and acts on it (identifies what event, and directs the flow where it should - here, move the rocket).

Move the rocket does that, it moves the rocket with a 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()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80