0

So I am trying to code a program, where an "Alien" reacts to specific inputs on the keyboard.

I am using bind_all, but Python does not recognize the Canvas c in c.bind_all('<Right>', move_pupille_rechts()). Does someone know why and how I can solve this?

from tkinter import *
from time import sleep

def create_window():
    global c, körper, hals, mund, auge, pupille, hut, window
    window = Tk()
    c = Canvas(window, height=500, width=500)
    c.pack()

def create_alien():
    global c, körper, hals, mund, auge, pupille, hut, window, words
    körper = c.create_oval(100, 150, 300, 250, fill='green')
    hals   = c.create_line(200, 150, 200, 130)
    mund   = c.create_oval(160, 230, 240, 210, fill='red')
    auge   = c.create_oval(170, 130, 230, 70)
    pupille= c.create_oval(195, 105, 205, 95, fill='black')
    hut    = c.create_polygon(200, 30, 250, 75, 150, 75,fill='blue')
    words  = c.create_text(200, 280, text='Ich bin ein Alien', state=HIDDEN)
    window.update()

def move_pupille_rechts():
    global c, körper, hals, mund, auge, pupille, hut, window
    c.move(pupille, 10, 0)
    window.update()

def move_pupille_links():
    global c, körper, hals, mund, auge, pupille, hut, window
    c.move(pupille, -10, 0)
    window.update()

def mund_schwarz():
    global c, körper, hals, mund, auge, pupille, hut, window
    c.itemconfig(mund, fill='black')
    window.update()

def mund_rot():
    global c, körper, hals, mund, auge, pupille, hut, window
    c.itemconfig(mund, fill='red')
    window.update()

def lid_zu():
    global c, körper, hals, mund, auge, pupille, hut, window
    c.itemconfig(auge, fill='green')
    c.itemconfig(pupille, state=HIDDEN)
    window.update()

def lid_auf():
    global c, körper, hals, mund, auge, pupille, hut, window
    c.itemconfig(auge, fill='white')
    c.itemconfig(pupille, state=NORMAL)
    window.update()

def hut_weg():
    global c, körper, hals, mund, auge, pupille, hut, window, txt
    c.itemconfig(hut, state=HIDDEN)
    sleep(1)
    c.itemconfig(words, text='HEY GIB MIR MEINEN HUT ZURÜCK')
    window.update()

def text():
    global c, körper, hals, mund, auge, pupille, hut, window, txt
    txt = input('text')
    c.itemconfig(words, text=txt, state=NORMAL)
    window.update()
    if txt == 'lol':
        sleep(1)
        hut_weg()


c.bind_all('<Right>', move_pupille_rechts())


create_window()
create_alien()
sleep(1)
move_pupille_rechts()
sleep(1)
move_pupille_links()
sleep(1)
move_pupille_links()
mund_schwarz()
sleep(1)
move_pupille_rechts()
mund_rot()
sleep(1)
lid_zu()
sleep(0.3)
lid_auf()
sleep(1)
text()
sleep(1)
sleep(10)

mainloop()```


martineau
  • 112,593
  • 23
  • 157
  • 280
  • You can't reference the `Canvas` `c` until it's created by calling `create_window()`. There is a similar issue with `pupille` which is created by `create_alien()`. The other problem I noticed that you are calling the function you're wanting to bind in the `c.bind_all('', move_pupille_rechts())` call which should be `c.bind_all('', move_pupille_rechts)`. – martineau Feb 23 '22 at 14:13

0 Answers0