473,320 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ghost chars in Tkinter.Entry

Strider1066
Is it a bug or a feature?

I'm trying to sub-class an Entry widget that displays a template (i.e. '____-__-__') for users and does keystroke validation. It is starting to work but with one problem. Every key is echoed in the Entry widget. If I were to type a '2', '22__-__-__' is displayed. The next entry replaces the first duplicate with another, that is if I were to then type a '3' the display now shows '233__-__-__', and so on. The correct data is held by the widget as shown by a entry.get() but the display is wrong.

I have started poking at the code with a sharp stick, Please, any suggestions.

steve

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import pdb
  3.  
  4. class entryDate(Entry):
  5.     def __init__(self, parent=None, template="@2###@-##@-##"):
  6.         Entry.__init__(self, parent)
  7.         self.ptr=-1                 # '-1' to accomadate nextwriteable's ptr +=mov
  8.         self.next=object        #for linked list
  9.         self.prior=object       #for linked list
  10.         self.refrList=self.makeReferenceList(template)
  11.         self.workingList=self.makeWorkingList(self.refrList)
  12.         self.bind('<Key>', self.onKeyPress)
  13.         self.config(self, background = "green", width = 10) 
  14.         self.insert(0, (''.join(self.workingList)))
  15.         self.nextWriteable(1)
  16.  
  17.     def makeReferenceList(self, templateStr):
  18.         ts = templateStr
  19.         tl=[]
  20.         i=0
  21.         while i < len(ts):
  22.             if ts[i]=='@':
  23.                 tl.append(ts[i:i+2])
  24.                 i += 2
  25.             else:
  26.                 tl.append(ts[i : i +1])
  27.                 i+=1
  28.         return tl
  29.  
  30.     def makeWorkingList(self, tmplt):
  31.         s=[]
  32.         displayChar = '_'
  33.         for itm in tmplt:
  34.             if itm[0]=='@':
  35.                 s.append(itm[1])
  36.             else:
  37.                 s.append(displayChar)
  38.         return s
  39.  
  40.     def onKeyPress(self, event):
  41.         if event.char==event.keysym: #normal key
  42.             self.delete(0, END)
  43.             if self.refrList[self.ptr]=='#': #ie writeable
  44.                 if event.char.isdigit():
  45.                     self.workingList[self.ptr]=event.char
  46.                     self.insert(0, (''.join(self.workingList)))
  47.                     self.nextWriteable(1)
  48.                 else:                                   # if alpha key
  49.                     s = ''.join(self.workingList)
  50.                     self.insert( 0, (''.join(self.workingList)))
  51.         elif len(event.char)==1:        # punctuation
  52.             pass
  53.         else:
  54.             pass
  55.  
  56.     def nextWriteable(self, mov):
  57.         self.ptr += mov
  58.         if self.ptr < 0:
  59.             self.ptr=len(self.refList)   #replace with linked list
  60.             self.icursor=self.ptr           # ''
  61.             self.prior.focus_set()
  62.         elif self.ptr == len(self.refrList):
  63.             self.ptr=0                    #replace with linked list
  64.             self.icursor=0                # ''
  65.         elif self.refrList[self.ptr][0]=='@':
  66.             self.nextWriteable(mov)
  67.         else:
  68.             self.icursor(self.ptr)
  69.  
  70. if __name__=='__main__':
  71.     root = Tk()
  72.     entr= entryDate(root)
  73.     entr.pack()
  74.     entr.focus_set()
  75.     root.mainloop()
Mar 14 '08 #1
2 1880
jlm699
314 100+
The problem here is that while you have binded the keypress event to your function onKeyPress, it is an instance binding only, Tkinter is still performing its own keypress event at the root level. Remove the binding from your class's __init__ and place it after the line as such:
Expand|Select|Wrap|Line Numbers
  1. if __name__=='__main__':
  2.     root = Tk()
  3.     entr= entryDate(root)
  4.     root.bind('<Key>',entr.onKeyPress)
  5.     entr.pack()
  6.     entr.focus_set()
  7.     root.mainloop()
Mar 14 '08 #2
Thank you,
That worked like a champ.
Mar 15 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Thomas Nücker | last post by:
Hi! I am creating a dialog-box within my application, using tkinter. The problem is the following: After the dialogbox is started, the main application window comes again to top and the...
0
by: phil | last post by:
Sorry for the repost, but moderator jeld the last one, In a Tkinter entry field (or Pmw entry) how could I eat charactres? Say a certain char is keyed in. Say & I notice in the event handler...
1
by: phil | last post by:
In a Tkinter entry field (or Pmw entry) how could I eat charactres? Say a certain char is keyed in. Say & I notice in the event handler for <key>. I don't want any more charactres to display or...
3
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...
2
by: Martyn Quick | last post by:
If I create a Tkinter.Entry widget, I can adjust the background and the text colours, using the background and foreground options. However, if the state is "disabled", then this has no effect and...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
1
by: C Martin | last post by:
What am I doing wrong in this code? The callback doesn't work from the Entry widget. ##start code import Tkinter tk = Tkinter.Tk() var = Tkinter.StringVar() print var._name def cb(name,...
2
by: skanemupp | last post by:
so my little calculator works perfectly now. just having some trouble with the layout. this whole tkinter-thing seems to be more tricky than it should be. how can i make the 4 column of buttons...
0
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.