-2

I have a problem with tkinter self.master.bind. When I run the program below, it just calls the bind automatically! I have self.master.bind('<left>', self.moveleft), It just doesn't work! It runs automatically. I have listed where the error is in a comment saying ERROR HERE!!!. __init__ is working fine, self.master is working fine, it's just the bind, that's the only thing!

from tkinter import *
import time

class game_Win_Start:

    def __init__(self, root):
        self.master = root
        self.master.title('Awesome game')
        self.canvas()

    def canvas(self):
        global c
        c = Canvas(self.master, width=root.winfo_screenwidth(),
                   height=root.winfo_screenheight(), bg='black')
        c.pack()
        self.ButtonLabel()

    def ButtonLabel(self):
        global title
        title = Label(self.master, font=('arial', 50, 'bold'),
                  text='A Game', fg='orange')
        title.place(x=750, y=75)
        self.BtnStart()

    def BtnStart(self):
        global BtnStart
        BtnStart = Button(self.master, font=('arial', 45, 'bold'),
                      text='start', relief=RAISED, padx=25,
                      pady=25, bd=8, command=self.MenuDestroy)
        BtnStart.place(x=750, y=700)

    def MenuDestroy(self):
        BtnStart.place(x=2000, y=0)
        title.place(x=2000, y=0)
        GameScreen()

class gameScreen:

    def __init__(self, root):
        self.master = root
        self.drawBg()

    def drawBg(self):
        full = root.winfo_screenwidth()
        middle = full/2
        global third
        third = full/3
        third2 = third*2
        global sixth
        sixth = third/2
        c.create_line(third,0,third,1000,fill='blue')
        c.create_line(third2,0,third2,1000,fill='blue')
        c.create_rectangle(0,0,50,1000,fill='blue')
        c.create_rectangle(full,0,full-50,1000,fill='blue')
        self.spriteMe()

    def spriteMe(self):
        global sprite
        sprite = c.create_oval(0,0,50,50,fill='peru')
        c.move(sprite,sixth*3,850)
        self.master.update()
        Controls()



def GameScreen():
    gameScreen(root)

class controls:

    def __init__(self, root):
        self.master = root
        self.keyBind()

    def moveleft(self, *args):
        c.move(sprite,-608,0)
        self.master.update()
        c.update()
        time.sleep(1)
    def moveright(self, *args):
        c.move(sprite,608,0)
        self.master.update()
        c.update()
        time.sleep(1)
    def keyBind(self):                           # ERROR HERE!!!!!
        self.master.bind('left', self.moveleft())
        self.master.bind('right', self.moveright())



def Controls():
    while True:
    controls(root)


root = Tk()
game_Win_Start(root)
root.mainloop()
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
Guydangerous99
  • 147
  • 1
  • 12

2 Answers2

1

First of all declaring something as foo.bind('<Event>', bar()) will immediately call the function bar() and set the callbacks value to the returned value of bar(). So if for example I set bar() to be the below...

def bar(*args):
    return print(True)

...then the callback would equal print(True). If nothing is returned then the program will simply run the function and return None, meaning the callback is essentially useless (Unless this is what you wanted for some unknown reason).

Secondly, 'left' is not an event in tkinter. '<Left>' however is. The brackets and capitalisation are not optional. Even if you setup the callback correctly, the callback would never be triggered as the event 'left' doesn't exist and would never be raised.

Ethan Field
  • 4,646
  • 3
  • 22
  • 43
0

You should be able to remove the brackets from moveleft and moveright under keyBind and it will not longer be automatically called. Remember, command must be a function, not the returned value of a function.

Benjamin James Drury
  • 2,353
  • 1
  • 14
  • 27