Python and tkinter: event handling inside class
up vote
0
down vote
favorite
I want my program to do some code when any of the keys on the keyboard is pressed.
from tkinter import *
class MyProgram(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.bind_all('<Key>', key)
return
def key(self, event):
if event.char == event.keysym or len(event.char) == 1:
#do some code on event
print("key pressed")
if __name__ == '__main__':
mp = MyProgram(Tk())
mainloop()
This is my code but I get error that I'm missing an event parameter in key() fuction.
python-3.x events tkinter
add a comment |
up vote
0
down vote
favorite
I want my program to do some code when any of the keys on the keyboard is pressed.
from tkinter import *
class MyProgram(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.bind_all('<Key>', key)
return
def key(self, event):
if event.char == event.keysym or len(event.char) == 1:
#do some code on event
print("key pressed")
if __name__ == '__main__':
mp = MyProgram(Tk())
mainloop()
This is my code but I get error that I'm missing an event parameter in key() fuction.
python-3.x events tkinter
Where is the Thread supposed to get the event to deliver to your function? Handling keys in a Tkinter program is done via the.bind()method on widgets, threads have nothing to do with it (and generally don't get along well with Tkinter anyway).
– jasonharper
Nov 10 at 23:07
Sorry, I forgot one line of code.self.bind_all('<Key>', key)
– CodeMonster
Nov 11 at 6:39
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want my program to do some code when any of the keys on the keyboard is pressed.
from tkinter import *
class MyProgram(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.bind_all('<Key>', key)
return
def key(self, event):
if event.char == event.keysym or len(event.char) == 1:
#do some code on event
print("key pressed")
if __name__ == '__main__':
mp = MyProgram(Tk())
mainloop()
This is my code but I get error that I'm missing an event parameter in key() fuction.
python-3.x events tkinter
I want my program to do some code when any of the keys on the keyboard is pressed.
from tkinter import *
class MyProgram(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.bind_all('<Key>', key)
return
def key(self, event):
if event.char == event.keysym or len(event.char) == 1:
#do some code on event
print("key pressed")
if __name__ == '__main__':
mp = MyProgram(Tk())
mainloop()
This is my code but I get error that I'm missing an event parameter in key() fuction.
python-3.x events tkinter
python-3.x events tkinter
edited Nov 11 at 7:02
asked Nov 10 at 20:02
CodeMonster
84
84
Where is the Thread supposed to get the event to deliver to your function? Handling keys in a Tkinter program is done via the.bind()method on widgets, threads have nothing to do with it (and generally don't get along well with Tkinter anyway).
– jasonharper
Nov 10 at 23:07
Sorry, I forgot one line of code.self.bind_all('<Key>', key)
– CodeMonster
Nov 11 at 6:39
add a comment |
Where is the Thread supposed to get the event to deliver to your function? Handling keys in a Tkinter program is done via the.bind()method on widgets, threads have nothing to do with it (and generally don't get along well with Tkinter anyway).
– jasonharper
Nov 10 at 23:07
Sorry, I forgot one line of code.self.bind_all('<Key>', key)
– CodeMonster
Nov 11 at 6:39
Where is the Thread supposed to get the event to deliver to your function? Handling keys in a Tkinter program is done via the
.bind() method on widgets, threads have nothing to do with it (and generally don't get along well with Tkinter anyway).– jasonharper
Nov 10 at 23:07
Where is the Thread supposed to get the event to deliver to your function? Handling keys in a Tkinter program is done via the
.bind() method on widgets, threads have nothing to do with it (and generally don't get along well with Tkinter anyway).– jasonharper
Nov 10 at 23:07
Sorry, I forgot one line of code.
self.bind_all('<Key>', key)– CodeMonster
Nov 11 at 6:39
Sorry, I forgot one line of code.
self.bind_all('<Key>', key)– CodeMonster
Nov 11 at 6:39
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You forgot to use self., and there is no function named key in __init__ so Python gives you a NameError. The solution is simple; just add self. before key on line 7:
class MyProgram(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.bind_all('<Key>', self.key)
return
Sorry for late response. This worked, thank you.
– CodeMonster
Nov 11 at 19:58
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You forgot to use self., and there is no function named key in __init__ so Python gives you a NameError. The solution is simple; just add self. before key on line 7:
class MyProgram(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.bind_all('<Key>', self.key)
return
Sorry for late response. This worked, thank you.
– CodeMonster
Nov 11 at 19:58
add a comment |
up vote
0
down vote
accepted
You forgot to use self., and there is no function named key in __init__ so Python gives you a NameError. The solution is simple; just add self. before key on line 7:
class MyProgram(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.bind_all('<Key>', self.key)
return
Sorry for late response. This worked, thank you.
– CodeMonster
Nov 11 at 19:58
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You forgot to use self., and there is no function named key in __init__ so Python gives you a NameError. The solution is simple; just add self. before key on line 7:
class MyProgram(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.bind_all('<Key>', self.key)
return
You forgot to use self., and there is no function named key in __init__ so Python gives you a NameError. The solution is simple; just add self. before key on line 7:
class MyProgram(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.bind_all('<Key>', self.key)
return
answered Nov 11 at 11:51
qr7NmUTjF6vbA4n8V3J9
1515
1515
Sorry for late response. This worked, thank you.
– CodeMonster
Nov 11 at 19:58
add a comment |
Sorry for late response. This worked, thank you.
– CodeMonster
Nov 11 at 19:58
Sorry for late response. This worked, thank you.
– CodeMonster
Nov 11 at 19:58
Sorry for late response. This worked, thank you.
– CodeMonster
Nov 11 at 19:58
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242923%2fpython-and-tkinter-event-handling-inside-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where is the Thread supposed to get the event to deliver to your function? Handling keys in a Tkinter program is done via the
.bind()method on widgets, threads have nothing to do with it (and generally don't get along well with Tkinter anyway).– jasonharper
Nov 10 at 23:07
Sorry, I forgot one line of code.
self.bind_all('<Key>', key)– CodeMonster
Nov 11 at 6:39