473,785 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter default bindings

I have an Entry widget inside a Frame. The Frame contains other
widgets as well. I have bound the <Key> event to the Frame, but I
don't want the Frame to receive the event when the Entry widget has
focus.

So, I bound the <Key> event to the Entry widget (bound to method
a_key()). This works, except that both the Entry and Frame widgets get
the event. This also forces me to implement event handlers, which is
fine for the Frame since that's what I want to do. But I want the
Entry to use its default handler, not my own handler. Rather than
duplicate all that functionality, I found I could accomplish this by
temporarily unbinding the events, re-generating the event, and then
re-binding the events, as follows:

def a_key(self, e):
self.top.unbind ('<Key>')
self.entry.unbi nd('<Key>')
self.entry.even t_generate('<Ke y>',
keycode=e.keyco de,
keysym=e.keysym ,
)
self.entry.bind ('<Key>', self.a_key)
self.top.bind(' <Key>', self._parent_cl ass__keypress)
return 'break'

This works, but it's kludgy. Is there a better way to do this? I just
want the Entry widget to receive the event when it has focus, behave
in its default manner, and not have the event propagate upward to
containing widgets.
Jul 18 '05 #1
1 2418
Phil Schmidt wrote:
I have an Entry widget inside a Frame. The Frame contains other
widgets as well. I have bound the <Key> event to the Frame, but I
don't want the Frame to receive the event when the Entry widget has
focus.

So, I bound the <Key> event to the Entry widget (bound to method
a_key()). This works, except that both the Entry and Frame widgets get
the event. This also forces me to implement event handlers, which is
fine for the Frame since that's what I want to do. But I want the
Entry to use its default handler, not my own handler. Rather than
duplicate all that functionality, I found I could accomplish this by
temporarily unbinding the events, re-generating the event, and then
re-binding the events, as follows:

def a_key(self, e):
self.top.unbind ('<Key>')
self.entry.unbi nd('<Key>')
self.entry.even t_generate('<Ke y>',
keycode=e.keyco de,
keysym=e.keysym ,
)
self.entry.bind ('<Key>', self.a_key)
self.top.bind(' <Key>', self._parent_cl ass__keypress)
return 'break'

This works, but it's kludgy. Is there a better way to do this? I just
want the Entry widget to receive the event when it has focus, behave
in its default manner, and not have the event propagate upward to
containing widgets.


You could just find out who has focus (root.focus_get () and compare with
the entry... something like (untested)

def a_key(self, event):
has_focus = root.focus_get( )
if theEntry==has_f ocus:
print "the entry binding"
return "break"
else:
print "the frame binding"
Martin

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3213
by: Eric Brunel | last post by:
Hi all, I was creating a Tkinter widget in the style of the reversed tabs below Excel worksheets and I stepped in a serious problem: the code I made makes python crash with a seg fault, bus error or X11 BadGC error on both Solaris (2.6 and 2.7) and Linux (Mandrake 8.0); it doesn't crash on Windows. I tried to simplify the script, but I couldn't reproduce the crash with a simpler code. So the code below is somewhat long; sorry for that. ...
4
3131
by: pavel.kosina | last post by:
It seems to me that in my "again and again repainting canvas" script the rendering is slowing down as the time goes. It is visible even after 10 seconds. Any idea why? -- geon The exception is rule.
1
2977
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the scroll-bars work as advertised. That's great. However, if you click the Left Mouse button, it calls code which expands the width of the canvas by 100 pixels. The area being viewed expands correspondingly..... BUT I DON'T WANT IT TO!!
7
18028
by: William Gill | last post by:
Is there a simple way to cut and paste from a tkinter text widget to an entry widget? I know I could create a mouse button event that triggers a popup (message widget) prompting for cut/paste in each of the widgets using a temp variable to hold the text, but I don't wnat to reinvent the wheel if there already is something that does the job. Thanks, Bill
0
3587
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust accordingly. However, what I really want to happen is that the area of the canvas that the scrollbars show (the Scrollregion) should expand as the window grows. It doesn't currently do this. although, if the window shrinks smaller than the...
2
1451
by: Avi Kak | last post by:
Does Tkinter provide a function that returns all the event descriptors for a given widget class? I am looking for something similar to what you get in Perl/Tk when you call bind() with a single explicit argument. For example, in Perl/Tk, $widget->bind( Tk::Button ) returns a list like <Key-Return>
3
4300
by: Kevin Walzer | last post by:
I'm trying to set the active item in a Tkinter listbox to my application's currently-defined default font. Here's how I get the fonts loaded into the listbox: self.fonts=list(tkFont.families()) self.fonts.sort() for item in self.fonts: self.fontlist.insert(END, item) #self.fontlist is the
10
22544
by: Stephen M. Gava | last post by:
Hi all, I prefer using tkinter to wxpython (so sue me :) and i need to display a lot of html in a particular app. does anyone know if one of the existing add on tk html widgets have been wrapped for tkinter already? TIA for any reply, Stephen
4
2884
by: Davy | last post by:
Hi all, I have written a simple Tkinter program, that is draw a rectangle in a canvas, when I press Up key, the rectangle move up. But the program seems work not properly? My environment is Python2.5+PythonWin. ##---------------------- from Tkinter import * class MyApp:
8
3011
by: akineko | last post by:
Hi everyone, This is a memorandum so that other people can share the info. The following methods are declared in the Tkinter Button class. tkButtonDown(), tkButtonEnter(), tkButtonInvoke(), tkButtonLeave(), tkButtonUp() However, they are not working, when you try, you will get: _tkinter.TclError: invalid command name "tkButtonLeave"
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.