473,543 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I need a little help with Tkinter Entry widget

I'm trying to make a custom entry widget, as in the code that follows.
There are two problems I'm trying to fix:

1) I would like the widget to behave as myEntry.Escape( ) does now,
except that it happens on loss of focus, not when pressing Esc.

2) TABbing between multiple entry fields does undesired things with
the selection, and with cursor placement.

Can anyone offer any suggestions for how to fix this? I'm attemting to
replicate, more or less, the behavior one would get with, for example,
MS Excel, in navigating and entering data in an array of entry fields.

Also, I'd appreciate any critique of my overall technique,
particularly in how I've implemented the "undo" behavior - it works,
but doesn't seem particularly clean.

Thanks!

------------------------------------------------------

from Tkinter import *

class history:
"""this is a mixin for my custom widgets,
providing for "infinite" undo."""
LOG = []
COUNT = {}
def log(self):
history.LOG.app end((self, self.value))
history.COUNT.s etdefault(id(se lf), 0)
history.COUNT[id(self)] += 1
def undo(self):
try:
s, v = history.LOG.pop ()
if history.COUNT[id(s)]:
s.value = v
history.COUNT[id(s)] -= 1
if not history.COUNT[id(s)]:
del history.COUNT[id(s)]
s.Show()
except IndexError:
pass

class myEntry(Entry, history):
"""
This works pretty well, with one exception: I would like the
widget to behave as Escape() does now, except that it happens
on loss of focus, not when pressing Esc.

Also, TABbing between entry fields causes the text in the next
widget to be selected, with the cursor at the end of the text.
When keys are pressed, the text is appended to the selected
text, rather than replacing the selection as would be expected.
"""
def __init__(self, master=None, state=NORMAL, **config):
Entry.__init__( self, master, **config)
if master == None:
self.pack()
self.insert(INS ERT, str(self))
self.bind('<Ret urn>', self.Return)
self.bind('<Esc ape>', self.Escape)
self.bind('<Key >', self.Key)
self.bind('<Con trol-z>', self.ctrl_z)
self.mode = 'display'
if state == DISABLED:
self.config(sta te=DISABLED)
def Show(self):
self.delete(0, END)
self.insert(INS ERT, str(self))
self.mode = 'display'
try:
history.COUNT[id(self)] # fails if no history
(changes)
self.config(bg= 'pink', fg='black')
except:
self.config(bg= 'white', fg='black')
def Key(self, event):
try:
x = ord(event.char) # fails if not an ASCII key
if event.keysym == 'Tab':
self.Return(Non e)
elif self.mode == 'display':
self.config(bg= 'red', fg='white')
self.mode = 'entry'
E, I = self.index(END) , self.index(INSE RT)
self.delete(0, END)
self.insert(INS ERT, self.STR())
i = self.index(END)
self.icursor(i - (E - I))
except:
pass
def Return(self, event):
if self.focus_get( ):
self.Validate(s elf.get())
self.Show()
def Escape(self, event):
self.Show()
def ctrl_z(self, event):
self.undo()
def Validate(self, value):
pass

class gui_int(myEntry ):
def __init__(self, value=0, master=None, **config):
self.value = 0
myEntry.__init_ _(self, master, **config)
def Validate(self, value):
try:
v = int(value)
if v != self.value:
self.log()
self.value = v
except:
pass
def __str__(self):
return 'INT: ' + str(self.value)
def STR(self):
return str(self.value)

x=gui_int(value =22.34, font='Courier 12', state=DISABLED, width=11,
justify=RIGHT)
y=gui_int(font= 'Courier 12', width=11, justify=RIGHT)
z=gui_int(value =10.99, font='Courier 12', width=11, justify=RIGHT)
Jul 18 '05 #1
3 4917

"Phil Schmidt" <ph************ *****@yahoo.com > schrieb im Newsbeitrag
news:22******** *************** ***@posting.goo gle.com...
I'm trying to make a custom entry widget, as in the code that follows.
There are two problems I'm trying to fix:

1) I would like the widget to behave as myEntry.Escape( ) does now,
except that it happens on loss of focus, not when pressing Esc.

Then why don't you bind <FocusOut> and <FocusIn> ?

Kindly
Michael P
Jul 18 '05 #2
Oh! Seems obvious when you say it! I've been using the Fredrik Lundh
documentation, and saw nothing about those. Perhaps I should follow
the recommendations of other posters and get that book about
Tkinter...

Thanks,
Phil
Jul 18 '05 #3

"Phil Schmidt" <ph************ *****@yahoo.com > schrieb im Newsbeitrag
news:22******** *************** ***@posting.goo gle.com...
Oh! Seems obvious when you say it! I've been using the Fredrik Lundh
documentation, and saw nothing about those. Perhaps I should follow
the recommendations of other posters and get that book about
Tkinter...


Have alook at New Mexico Tech
http://www.nmt.edu/tcc/help/pubs/lang.html
There are two very concise as well as comprehensive documents.

Kndly
Michael P

a.. Python 2.2 programming language quick reference, 46 pp. [HTML]
[PostScript] [PDF]
b.. Tkinter reference: a GUI for Python, 84 pp. [PostScript] [PDF]

Jul 18 '05 #4

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

Similar topics

0
2194
by: Mickel Grönroos | last post by:
Hi, I'm trying to put an Tkinter.Entry of fixed size onto a specific location on a canvas using the place manager. The idea is that one can double-click a rectangle object on a canvas to get an entry field of the same size as the rectangle placed exactly over the rectangle thus creating the effect that the rectangle has entered "input...
3
9335
by: bigbinc | last post by:
I have used the 'entry' tk widget to get text values, I am now using 'Text' but I cant seem to use 'get' method. The TK docs say use get(index1, index2), I tried numbers and get an error print self.textMain.get(1,255) ceback (most recent call last): ile "C:\Python23\lib\lib-tk\Tkinter.py", line 1345, in __call return self.func(*args)
3
2137
by: Matt Hammond | last post by:
Here's a strange one in Tkinter that has me stumped: (I'm running python 2.4 on Suse Linux 9.3 64bit) I'm trying to make a set of Entry widgets with Label widgets to the left of each one, using the grid layout. If I make and grid the Label *before* the Entry then the Entry widget doesn't seem to work - it lets me put the cursor in it, but I...
2
2861
by: Dustan | last post by:
How do I limit what the user can enter in an Entry Widget? I know I can set it to display '*' to hide a password, but what I want to do is limit the contents to numeric characters. What is the easiest way of doing this?
5
5975
by: vagrantbrad | last post by:
I've created a short test program that uses tkFileDialog.askdirectory to help the user input a path into a tk entry widget. The problem I'm having is that when I run the code as listed below, the getPath function is called when the program initially runs, not when the button is pressed. from Tkinter import * import tkFileDialog class...
4
5423
by: BartlebyScrivener | last post by:
Using Python on Debian Etch. What is the best way to paste a block of text in at the command prompt. I'm trying something like: Quote = raw_input("Paste quote here: ") Which works great for one line of text with a single newline. It gets
3
1897
Elias Alhanatis
by: Elias Alhanatis | last post by:
Hello to everybody!! I am running Python 2.5.1 on Windows Vista and i have a problem with the "Entry" widget of Tkinter. Take a look at this code: from Tkinter import * def fetch(): q=(e.get()) print q
8
2012
by: Lie | last post by:
Inspect the following code: --- start of code --- import Tkinter as Tk from Tkconstants import * root = Tk.Tk() e1 = Tk.Entry(root, text = 'Hello World') e2 = Tk.Entry(root, text = 'Hello World')
0
1490
by: Leonhard Vogt | last post by:
Hello I have the following problem in Python 2.5 on Windows XP. On Ubuntu I do not see the problem. I have a Tkinter application as in the following example The entry-widget is somehow blocked (i cannot type characters into it) when I call askopenfilename before I create the widget. Calling askopenfile again (by clicking the button)...
0
7408
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7349
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7590
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. ...
0
7735
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...
1
5271
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...
0
3391
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...
0
3391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
968
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
636
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...

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.