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.










share|improve this question























  • 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















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.










share|improve this question























  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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













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





share|improve this answer




















  • Sorry for late response. This worked, thank you.
    – CodeMonster
    Nov 11 at 19:58










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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





share|improve this answer




















  • Sorry for late response. This worked, thank you.
    – CodeMonster
    Nov 11 at 19:58














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





share|improve this answer




















  • Sorry for late response. This worked, thank you.
    – CodeMonster
    Nov 11 at 19:58












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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 11:51









qr7NmUTjF6vbA4n8V3J9

1515




1515











  • 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




Sorry for late response. This worked, thank you.
– CodeMonster
Nov 11 at 19:58

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands