Tkinter binding problem

I have something like this:

from Tkinter import *

root = Tk()
root.title("Test")

def _quit():
    root.destroy()

m = Menu(root)
root.config(menu=m)

fm = Menu(m, tearoff=0)
m.add_cascade(label="File", menu=fm)
fm.add_command(label="Quit", command=_quit, accelerator='Ctrl+Q')

root.bind('<Control-Q>', _quit())
root.bind('<Control-q>', _quit())

root.mainloop()

My question is:
"Why is it _quit()always called?"

+3
source share
2 answers

When you communicate with Tkinter, you usually do not call the function you want to associate.

Supposed to use the line

root.bind('<Control-Q>', _quit) 

instead

root.bind('<Control-Q>', _quit())

Note the lack of parentheses behind _quit .

This code below should work.

from Tkinter import *

root = Tk()
root.title("Test")

def _quit(event):
    root.destroy()

m = Menu(root)
root.config(menu=m)

fm = Menu(m, tearoff=0)
m.add_cascade(label="File", menu=fm)
fm.add_command(label="Quit", command=lambda: _quit(None), accelerator='Ctrl+Q')

root.bind('<Control-Q>', _quit)
root.bind('<Control-q>', _quit)

root.mainloop()

EDIT:

, , . . Tkinter, , GUI, . Tkinter . , , "" None (lambda: _quit (None)). .

+7

. :

root.bind('<Control-Q>', _quit)
+6

All Articles