473,385 Members | 1,958 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,385 software developers and data experts.

autocomplete

hello.
I have been working all too hard trying to figure out how to get
TextCtrlAutoComplete.py to start another word after it finishes the
last word. I want it to start the autocomplete process all over again
after it finishes the autocomplete process. I have striped down the
program to a smaller version than the orginal. it now works in a
multiline wx.textctrl box. belive that the program only needs one or
two lines to complete the taks and start all over again.

here is a copy of the code
'''

wxPython Custom Widget Collection 20060207
Written By: Edward Flick ()
Michele Petrazzo (michele -=dot=- petrazzo -=at=- unipex
-=dot=- it)
Will Sadkin (wsadkin-=at=- nameconnector -=dot=- com)
Copyright 2006 (c) CDF Inc. ( http://www.cdf-imaging.com )
Contributed to the wxPython project under the wxPython project's
license.

'''

import wx
import sys

import wx.lib.mixins.listctrl as listmix
class myListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
def __init__(self,
parent,
ID=-1,
pos=wx.DefaultPosition,
size=wx.DefaultSize,
style=0):
wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
listmix.ListCtrlAutoWidthMixin.__init__(self)

class TextCtrlAutoComplete (wx.TextCtrl, listmix.ColumnSorterMixin ):

def __init__ ( self, parent, choices = None,
showHead=False, dropDownClick=True,
colFetch=-1, colSearch=0, hideOnNoMatch=True,
selectCallback=None, entryCallback=None,
matchFunction=None,
**therest) :
'''
Constructor works just like wx.TextCtrl except you can pass in
a
list of choices. You can also change the choice list at any
time
by calling setChoices.
'''

if therest.has_key('style'):
therest['style']=wx.TE_PROCESS_ENTER | therest['style']
else:
therest['style']=wx.TE_PROCESS_ENTER
wx.TextCtrl.__init__(self,parent,-1,'',size=(500,500),style=wx.TE_MULTILINE)
#wx.TextCtrl.__init__(self, parent, **therest )
self._hideOnNoMatch = hideOnNoMatch
self._selectCallback = selectCallback
self._matchFunction = matchFunction
self._screenheight = wx.SystemSettings.GetMetric(
wx.SYS_SCREEN_Y )

#sort variable needed by listmix
self.itemDataMap = dict()

#widgets
self.dropdown = wx.PopupWindow( self )

#Control the style
flags = wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_SORT_ASCENDING
if not (showHead) :
flags = flags | wx.LC_NO_HEADER

#Create the list and bind the events
self.dropdownlistbox = myListCtrl( self.dropdown, style=flags,
pos=wx.Point( 0, 0) )
gp = self
while ( gp != None ) :
gp = gp.GetParent()
self.Bind( wx.EVT_TEXT , self.onEnteredText, self )
self.Bind( wx.EVT_KEY_DOWN , self.onKeyDown, self )
self.dropdownlistbox.Bind(wx.EVT_LEFT_DCLICK,
self.onListDClick)

def onListDClick(self, evt):
self._setValueFromSelected()

def onEnteredText(self, event):
text = event.GetString()
#print('onEnterdText text: ',text)
if self._entryCallback:
self._entryCallback()
if not text:
# control is empty; hide dropdown if shown:
if self.dropdown.IsShown():
self._showDropDown(False)
event.Skip()
return
found = False
choices = self._choices

for numCh, choice in enumerate(choices):
if self._matchFunction and self._matchFunction(text,
choice):
found = True
elif choice.lower().startswith(text.lower()) :
found = True
if found:
self._showDropDown(True)
item = self.dropdownlistbox.GetItem(numCh)
toSel = item.GetId()
self.dropdownlistbox.Select(toSel)
break

if not found:

self.dropdownlistbox.Select(self.dropdownlistbox.G etFirstSelected(),
False)
if self._hideOnNoMatch:
self._showDropDown(False)

self._listItemVisible()

event.Skip ()

def onKeyDown ( self, event ) :
""" Do some work when the user press on the keys:
up and down: move the cursor
left and right: move the search
"""
skip = True

sel = self.dropdownlistbox.GetFirstSelected()
visible = self.dropdown.IsShown()

KC = event.GetKeyCode()
if KC == wx.WXK_DOWN :
if sel < (self.dropdownlistbox.GetItemCount () - 1) :
self.dropdownlistbox.Select ( sel+1 )
self._listItemVisible()
self._showDropDown ()
skip = False
elif KC == wx.WXK_UP :
if sel > 0 :
self.dropdownlistbox.Select ( sel - 1 )
self._listItemVisible()
self._showDropDown ()
skip = False

if visible :
if event.GetKeyCode() == wx.WXK_RETURN :
self._setValueFromSelected()
skip = False
if event.GetKeyCode() == wx.WXK_ESCAPE :
self._showDropDown( False )
skip = False
if skip :
event.Skip()

def onListItemSelected (self, event):
self._setValueFromSelected()
event.Skip()

def onClickToggleDown(self, event):
self._lastinsertionpoint = self.GetInsertionPoint()
event.Skip ()

def onClickToggleUp ( self, event ) :
if ( self.GetInsertionPoint() == self._lastinsertionpoint ) :
self._showDropDown ( not self.dropdown.IsShown() )
event.Skip ()
# -- Interfaces methods

def SetChoices(self, choices):

#Sets the choices available in the popup wx.ListBox.
#The items will be sorted case insensitively.
self._choices = choices
flags = wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_SORT_ASCENDING
| wx.LC_NO_HEADER
self._updateDataList(self._choices)
self.dropdownlistbox.InsertColumn(0, "")
for num, colVal in enumerate(self._choices):
index =
self.dropdownlistbox.InsertImageStringItem(sys.max int, colVal, -1)
self.dropdownlistbox.SetStringItem(index, 0, colVal)
self.dropdownlistbox.SetItemData(index, num)

self._setListSize()

# there is only one choice for both search and fetch if setting
a single column:
self._colSearch = 0
self._colFetch = -1
def SetSelectCallback(self, cb=None):
self._selectCallback = cb

def SetEntryCallback(self, cb=None):
self._entryCallback = cb

#-- Internal methods
def _setValueFromSelected( self ) :
'''
Sets the wx.TextCtrl value from the selected wx.ListCtrl item.
Will do nothing if no item is selected in the wx.ListCtrl.
'''
sel = self.dropdownlistbox.GetFirstSelected()
if sel > -1:
if self._colFetch != -1: col = self._colFetch
else: col = self._colSearch
itemtext = self.dropdownlistbox.GetItem(sel, col).GetText()

if self._selectCallback:
dd = self.dropdownlistbox
values = [dd.GetItem(sel, x).GetText()
for x in xrange(dd.GetColumnCount())]
self._selectCallback( values )
self.SetValue (itemtext)
self.SetInsertionPointEnd ()
self.SetSelection ( -1, -1 )
self._showDropDown ( False )

def _showDropDown ( self, show = True ) :
'''
Either display the drop down list (show = True) or hide it
(show = False).
'''

if show :
size = self.dropdown.GetSize()
width, height = self . GetSizeTuple()
x, y = self . ClientToScreenXY ( wx.CentreX+6, wx.CentreY+6
)
if size.GetWidth() != width :
size.SetWidth(width)
self.dropdown.SetSize(size)

self.dropdownlistbox.SetSize(self.dropdown.GetClie ntSize())
if (y + size.GetHeight()) < self._screenheight :
self.dropdown . SetPosition ( wx.Point(x, y) )
else:
self.dropdown . SetPosition ( wx.Point(x, y - height -
size.GetHeight()) )
self.dropdown.Show ( show )

def _listItemVisible( self ) :
'''
Moves the selected item to the top of the list ensuring it is
always visible.
'''
toSel = self.dropdownlistbox.GetFirstSelected ()
if toSel == -1: return
self.dropdownlistbox.EnsureVisible( toSel )

def _updateDataList(self, choices):
#delete, if need, all the previous data
if self.dropdownlistbox.GetColumnCount() != 0:
self.dropdownlistbox.DeleteAllColumns()
self.dropdownlistbox.DeleteAllItems()

#and update the dict
if choices:
for numVal, data in enumerate(choices):
self.itemDataMap[numVal] = data
#print("data is %s",data)
else:
numVal = 0
self.SetColumnCount(numVal)

def _setListSize(self):
choices = self._choices
longest = 0
for choice in choices :
longest = max(len(choice), longest)

#longest += 3
itemcount = min( len( choices ) , 7 ) + .5
charheight = self.dropdownlistbox.GetCharHeight()
charwidth = self.dropdownlistbox.GetCharWidth()
self.popupsize = wx.Size( charwidth*longest,
charheight*itemcount )
self.dropdownlistbox.SetSize ( self.popupsize )
self.dropdown.SetClientSize( self.popupsize )

class test:
def __init__(self):
args = dict()
args["selectCallback"] = self.selectCallback

self.dynamic_choices = [
'hello world', 'abandon', 'acorn', 'acute',
'adore',
'aegis', 'ascertain', 'asteroid',
'beautiful', 'bold', 'classic',
'daring', 'dazzling', 'debonair', 'definitive',
'effective', 'elegant',
'http://python.org', 'http://www.google.com',
'fabulous', 'fantastic', 'friendly',
'forgiving', 'feature',
'sage', 'scarlet', 'scenic', 'seaside',
'showpiece', 'spiffy',
'www.wxPython.org', 'www.osafoundation.org'
]
app = wx.PySimpleApp()
frm = wx.Frame(None,-1,"Test",
style=wx.CLEAR
|wx.DEFAULT_FRAME_STYLE
)
panel = wx.Panel(frm)
self._ctrl = TextCtrlAutoComplete(panel, **args)
self.onBtDynamicChoices(self.onBtDynamicChoices)

frm.Show()
app.MainLoop()
def onBtDynamicChoices(self, event):
self._ctrl.SetChoices(self.dynamic_choices)
self._ctrl.SetEntryCallback(self.setDynamicChoices )
def match(self, text, choice):
t = text.lower()
c = choice.lower()
#print('The Value is %S',choice)
return c.startswith(t)

def setDynamicChoices(self):
if wx.EVT_KEY_DOWN == ' ': print "Space bar pressed"
text = self._ctrl.GetValue().lower()
choices = [choice for choice in self.dynamic_choices if
self.match(text, choice)]
self._ctrl.SetChoices(choices)
#print('The Value is %S',text)

def selectCallback(self, values):
""" Simply function that receive the row values when the
user select an item
"""
print "Select Callback called...:", values
self.wordCheck(values)

def wordCheck(self,values):
pass #print"Word to check:",values
if __name__ == "__main__":
test()

Jun 10 '06 #1
0 2055

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

Similar topics

4
by: bc | last post by:
Hi, Is it possible to detect if the IE autocomplete function in a current context such as a textbox is activated (ie. dropdown with previously entered info is visible)? Problem: I use a script...
40
by: Alex | last post by:
Hello, does anybody know how to turn off the autocomplete feature for a certain text field? I am aware of the "autocomplete" attribute, but I have seen other implementions achieving it...
0
by: Thorsten Ottosen | last post by:
Hi, I'm using the AutoComplete attribute to control transactions. What happens if I call functions with the AutoComplete attribute from within other functions with the AutoComplete attribute?...
8
by: moondaddy | last post by:
I have a form for entering a user's address and all fields have a required validating control associated with them and the error msg for each field displays right next to it. The normal behavior...
2
by: Mrinal Kamboj | last post by:
Hi , Need to get some basic info regarding AutoComplete Attribute in COM+ . I am having a class with TransactionOption set to Required , now for this class , can i mark all the methods with...
1
by: thubba2000 | last post by:
We have a web application developed using IBuySpy. In older versions, Autocomplete on all web forms works. In our latest version, it stopped working on all clients. I have gone through all the...
1
by: rbg.net | last post by:
I know that there is a autocomplete property for the HTML "INPUT type=text" control which if set to OFF, disables autocomplete of the input textbox (doesn't remember previously entered values) ...
8
by: nil | last post by:
Hello all, It's urgent... i want to add autocomplete textbox facility in my application like google. as you type it suggests option to the user..i want the same kind of facility...i know i...
1
by: wkerplunk | last post by:
Below is what I have build with several different languages. It works great but I need help, I am stuck. When you click on an item in the dropdown autocomplete div it does a mousedown function...
5
by: Doug | last post by:
Hi I have a simple form, just with one combo box and an OK button - and i have tried to use the autocomplete routine and I have enabled autocomplete in the combo box properties. ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...

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.