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()