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 - from Tkinter import *
-
import pdb
-
-
class entryDate(Entry):
-
def __init__(self, parent=None, template="@2###@-##@-##"):
-
Entry.__init__(self, parent)
-
self.ptr=-1 # '-1' to accomadate nextwriteable's ptr +=mov
-
self.next=object #for linked list
-
self.prior=object #for linked list
-
self.refrList=self.makeReferenceList(template)
-
self.workingList=self.makeWorkingList(self.refrList)
-
self.bind('<Key>', self.onKeyPress)
-
self.config(self, background = "green", width = 10)
-
self.insert(0, (''.join(self.workingList)))
-
self.nextWriteable(1)
-
-
def makeReferenceList(self, templateStr):
-
ts = templateStr
-
tl=[]
-
i=0
-
while i < len(ts):
-
if ts[i]=='@':
-
tl.append(ts[i:i+2])
-
i += 2
-
else:
-
tl.append(ts[i : i +1])
-
i+=1
-
return tl
-
-
def makeWorkingList(self, tmplt):
-
s=[]
-
displayChar = '_'
-
for itm in tmplt:
-
if itm[0]=='@':
-
s.append(itm[1])
-
else:
-
s.append(displayChar)
-
return s
-
-
def onKeyPress(self, event):
-
if event.char==event.keysym: #normal key
-
self.delete(0, END)
-
if self.refrList[self.ptr]=='#': #ie writeable
-
if event.char.isdigit():
-
self.workingList[self.ptr]=event.char
-
self.insert(0, (''.join(self.workingList)))
-
self.nextWriteable(1)
-
else: # if alpha key
-
s = ''.join(self.workingList)
-
self.insert( 0, (''.join(self.workingList)))
-
elif len(event.char)==1: # punctuation
-
pass
-
else:
-
pass
-
-
def nextWriteable(self, mov):
-
self.ptr += mov
-
if self.ptr < 0:
-
self.ptr=len(self.refList) #replace with linked list
-
self.icursor=self.ptr # ''
-
self.prior.focus_set()
-
elif self.ptr == len(self.refrList):
-
self.ptr=0 #replace with linked list
-
self.icursor=0 # ''
-
elif self.refrList[self.ptr][0]=='@':
-
self.nextWriteable(mov)
-
else:
-
self.icursor(self.ptr)
-
-
if __name__=='__main__':
-
root = Tk()
-
entr= entryDate(root)
-
entr.pack()
-
entr.focus_set()
-
root.mainloop()
2 1880
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: -
if __name__=='__main__':
-
root = Tk()
-
entr= entryDate(root)
-
root.bind('<Key>',entr.onKeyPress)
-
entr.pack()
-
entr.focus_set()
-
root.mainloop()
Thank you,
That worked like a champ.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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
| |