python tkinter self.master.bind()在没有事件

时间:2017-10-23 08:34:25

标签: python tkinter

我的tkinter self.master.bind有问题。当我运行下面的程序时,它只是自动调用绑定!我有self.master.bind('<left>', self.moveleft),它只是不起作用!它会自动运行。我已经在ERROR HERE!!!的评论中列出了错误的位置。 __init__工作正常,self.master工作正常,只是绑定,这是唯一的事情!

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

2 个答案:

答案 0 :(得分:1)

首先声明为foo.bind('<Event>', bar())的内容会立即调用函数bar()并将回调值设置为返回值bar()。因此,如果我将bar()设置为以下...

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

...然后回调将等于print(True)。如果没有返回任何内容,那么程序将只运行该函数并返回None,这意味着回调基本上是无用的(除非这是你想要的原因)。

其次,'left'不是tkinter中的事件。 '<Left>'但是。括号和大小写不是可选的。即使您正确设置回调,也不会触发回调,因为事件'left'不存在且永远不会被引发。

答案 1 :(得分:0)

您应该可以从moveleft下的moverightkeyBind删除括号,但不会再自动调用它。请记住,命令必须是函数,而不是函数的返回值。