回复: 回复: bind between event and callback

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

回复: 回复: bind between event and callback

luofeiyu
code1
from Tkinter import *
root = Tk()
def callback(event):
   print "i am here"

frame = Frame(root, width=100, height=100)

frame.bind("<Key>", callback)
frame.focus_set()
frame.pack()
root.mainloop()

code2
from Tkinter import *
root = Tk()
def callback(event):
   print "i am here"

frame = Frame(root, width=100, height=100)
frame.focus_set()
frame.bind("<Key>", callback)

frame.pack()
root.mainloop()

code1,code2 all can run ,i want to know which one is better?
is it  reasonable that 
frame.focus_set() is before frame.bind("<Key>", callback)  or after it??
------------------ 原始邮件 ------------------
发送时间: 2011年8月17日(星期三) 下午5:26
收件人: "tkinter-discuss"<[hidden email]>;
主题: Re: [Tkinter-discuss]回复: bind between event and callback
 
Hi,

Thus spoketh "守株待兔" <[hidden email]>
unto us on Wed, 17 Aug 2011 17:06:28 +0800:

> why in the following code,there is no  "frame.focus_set()" in the
> code ,it can run ,why?? from Tkinter import *
> root = Tk()
> def callback(event):
>    print "i am here"
>
> frame = Frame(root, width=100, height=100)
> frame.bind("<Button-1>", callback)
> frame.pack()
> root.mainloop()

Mouse events are handled differently than key events.
When you click somewhere in the window of course the window manager
"knows" which widget is clicked - the one under the mouse pointer's "hot
spot". But when you press some key on your keyboard - how would the window
manager "know" which window and which of its child widgets "is meant"?
That is where the "keyboard focus" comes in. Keyboard events are always
sent (by the window manager) to the window that has focus, where
"window" does not only mean a main application window like your Tk()
instannce, but also "child windows" as your Tkinter.Frame() .

I hope this helps

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

All your people must learn before you can reach for the stars.
-- Kirk, "The Gamesters of Triskelion", stardate 3259.2
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: 回复: 回复: bind between event and callback

Firat Ozgul
I think there is no difference. Both positions seem reasonable to me.
So you can insert the set_focus line at whichever position you wish.

2011/8/17 守株待兔 <[hidden email]>:

> code1
> from Tkinter import *
> root = Tk()
> def callback(event):
>    print "i am here"
>
> frame = Frame(root, width=100, height=100)
>
> frame.bind("<Key>", callback)
> frame.focus_set()
> frame.pack()
> root.mainloop()
>
> code2
> from Tkinter import *
> root = Tk()
> def callback(event):
>    print "i am here"
>
> frame = Frame(root, width=100, height=100)
> frame.focus_set()
> frame.bind("<Key>", callback)
>
> frame.pack()
> root.mainloop()
>
> code1,code2 all can run ,i want to know which one is better?
> is it  reasonable that  frame.focus_set() is before frame.bind("<Key>",
> callback)  or after it??
> ------------------ 原始邮件 ------------------
> 发件人: "Michael Lange"<[hidden email]>;
> 发送时间: 2011年8月17日(星期三) 下午5:26
> 收件人: "tkinter-discuss"<[hidden email]>;
> 主题: Re: [Tkinter-discuss]回复: bind between event and callback
>
> Hi,
>
> Thus spoketh "守株待兔" <[hidden email]>
> unto us on Wed, 17 Aug 2011 17:06:28 +0800:
>
>> why in the following code,there is no  "frame.focus_set()" in the
>> code ,it can run ,why?? from Tkinter import *
>> root = Tk()
>> def callback(event):
>>    print "i am here"
>>
>> frame = Frame(root, width=100, height=100)
>> frame.bind("<Button-1>", callback)
>> frame.pack()
>> root.mainloop()
>
> Mouse events are handled differently than key events.
> When you click somewhere in the window of course the window manager
> "knows" which widget is clicked - the one under the mouse pointer's "hot
> spot". But when you press some key on your keyboard - how would the window
> manager "know" which window and which of its child widgets "is meant"?
> That is where the "keyboard focus" comes in. Keyboard events are always
> sent (by the window manager) to the window that has focus, where
> "window" does not only mean a main application window like your Tk()
> instannce, but also "child windows" as your Tkinter.Frame() .
>
> I hope this helps
>
> Michael
>
>
> .-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.
>
> All your people must learn before you can reach for the stars.
> -- Kirk, "The Gamesters of Triskelion", stardate 3259.2
> _______________________________________________
> Tkinter-discuss mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
> _______________________________________________
> Tkinter-discuss mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
>
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss