Skip to content

Commit

Permalink
Better listbox validation
Browse files Browse the repository at this point in the history
Improved validation on selecting items in listbox #446 - displays
warning and returns success/failure boolean
  • Loading branch information
jarvisteach committed May 5, 2018
1 parent 375727f commit 503cce4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
39 changes: 29 additions & 10 deletions appJar/appjar.py
Expand Up @@ -7574,26 +7574,45 @@ def setListBoxMulti(self, title, multi=True):

# select the specified item in the list
def selectListItem(self, title, item, callFunction=True):
lb = self.widgetManager.get(self.Widgets.ListBox, title)
positions = self._getListPositions(title, item)
if len(positions) > 0:
self.selectListItemAtPos(title, positions[0], callFunction)
if len(positions) > 1 and lb.cget("selectmode") == EXTENDED:
allOk = True
for pos in positions:
if not self.selectListItemAtPos(title, pos, callFunction):
allOk = False
return allOk
elif len(positions) > 1:
gui.warn("Unable to select multiple items for list: %s. Selecting first item: %s", title, item[0])
return self.selectListItemAtPos(title, positions[0], callFunction)
elif len(positions) == 1:
return self.selectListItemAtPos(title, positions[0], callFunction)
else:
gui.warn("Invalid list item(s): %s for list: %s", item, title)
return False

def selectListItemAtPos(self, title, pos, callFunction=False):
lb = self.widgetManager.get(self.Widgets.ListBox, title)
if lb.size() == 0:
gui.warn("No items in list: %s, unable to select item at pos: %s", title, pos)
return False
if pos < 0 or pos > lb.size() - 1:
gui.warn("Invalid list position: %s for list: %s (max: %s)", pos, title, lb.size()-1)
return False

# clear previous selection if we're not multi
if lb.cget("selectmode") != EXTENDED:
lb.selection_clear(0, END)

# show & select this item
if pos >= 0:
lb.see(pos)
lb.activate(pos)
lb.selection_set(pos)
# now call function
if callFunction and hasattr(lb, 'cmd'):
lb.cmd()
self.topLevel.update_idletasks()
lb.see(pos)
lb.activate(pos)
lb.selection_set(pos)
# now call function
if callFunction and hasattr(lb, 'cmd'):
lb.cmd()
self.topLevel.update_idletasks()
return True

# replace the list items in the list box
def updateListBox(self, title, items, select=False, callFunction=True):
Expand Down
11 changes: 10 additions & 1 deletion examples/issues/issue449.py
Expand Up @@ -6,10 +6,19 @@
def change(): print("changed")
def press(btn):
if btn == "clear": app.clearListBox("list", callFunction=app.check("call"))
elif btn == "select":
app.selectListItemAtPos("list", -5, callFunction=app.check("call"))
app.selectListItemAtPos("list", 0, callFunction=app.check("call"))
app.selectListItemAtPos("list", 40, callFunction=app.check("call"))
app.selectListItemAtPos("list", 4, callFunction=app.check("call"))
app.selectListItemAtPos("list", 3, callFunction=app.check("call"))
app.selectListItem("list", "a", callFunction=app.check("call"))
app.selectListItem("list", ["a", "b"], callFunction=app.check("call"))
app.selectListItem("list", ["a", "f"], callFunction=app.check("call"))
else: app.updateListBox("list", ["d", "e", "f", "g"], callFunction=app.check("call"))

with gui() as app:
app.label('hello world')
app.listbox("list", ["a", "b", "c", "d"], change=change)
app.check("call")
app.buttons(["clear", "update"], press)
app.buttons(["select", "clear", "update"], press)

0 comments on commit 503cce4

Please sign in to comment.