I am making a function that needs to search an for item whose ID is entered in an entry field, and select that item in a listbox.
So far I managed to get it to return the correct item, but I don't know how to select that item, or if it's even possible to.
self.__searchVar=StringVar()
self.__entrySearch=Entry(Panel,textvariable=self.__searchVar)
self.__entrySearch.grid(row=0,column=1,sticky=W)
Button(Panel, text="Search", width=10,
command=self.search).grid(row=0,column=2,sticky=E)
def search(self):
for lbo in self.__patients:
patient=self.__patients[lbo]
if self.__entrySearch.get()==pacijent.lbo:
print(pacijent)
#get index
#select item with that index in
the listbox
I wanted to get the index from that item and then select it, but I don't really know how. If someone could help, that would be great.
Related
I have a listbox and would like to be able to return multiple selections from said listbox.
I have tried changing "seltext" variable to a list(map(int()))) format, but I get the error of bad listbox index.
I'm not sure how to go about this; any help is appreciated.
The way I'm currently identifying the selected variable:
def selecting(self,event):
sel = self.lbox.curselection()
seltext = self.lbox.get(sel)
self.labelVariable.set(seltext)
The way that I'm assigning a single selection.
def OnButtonClick(self):
global confirmedsel
confirmedsel = ""
sel = self.lbox.curselection()
seltext = self.lbox.get(sel)
confirmedsel = seltext
print(confirmedsel)
app.quit()
The curselection method of the listbox returns a tuple of indexes representing the items that were selected. You simply need to iterate over that list and call the get method to get each element.
You do this in one line using a list comprehension, which results in a list that contains the values of the selected items as strings:
seltext = [self.lbox.get(index) for index in self.lbox.curselection()]
If you find list comprehensions difficult to read, here is a solution using a simple loop:
results = []
for index in self.lbox.curselection():
results.append(self.lbox.get(index))
def selecting(self,event):
sel = self.lbox.curselection()
seltext = list(map(int,self.lbox.get(sel)))
self.labelVariable.set(seltext)
Have you tried this?
You can lookup use of curselection here:
http://effbot.org/tkinterbook/listbox.htm
I'm trying to build a simple interface with hypertreelist. Basically, what I want to do, is set the item to a checked state. There is the function .IsChecked() to retrieve the checkbox status on an item, but no SetCheck() or SetValue(). Any idea on how to do this?
I suppose I need to access the checkbox object inside the TreeListItem, but I have no clue on how to do that. The documentation on that isn't really helpful.
Here's part of my code:
self.view.tree_list.AddColumn('Available columns')
root = self.view.tree_list.AddRoot('All columns', ct_type=1)
branches = {}
for item in self.possibleColumns:
branches[item] = self.columnsView.tree_list.AppendItem(root, item, ct_type=1)
if item in self.wantedColumns:
# set to checked
where wantedColumns and possibleColumns are array of strings. (the frame will help decide which columns to show in another tab)
Thanks,
HyperTreeList inherits from GenericTreeItem:
http://wxpython.org/Phoenix/docs/html/lib.agw.customtreectrl.GenericTreeItem.html#lib.agw.customtreectrl.GenericTreeItem
It would appear that you can use its Check() method to toggle whether a tree item is checked or not.
Just been struggling with this also, here my solution:
import wx.lib.agw.hypertreelist as HTL
self.tree_list =self.createTree()
self.tree_list.AddColumn("File")
self.tree_list.AddColumn("Size")
#self.tree_list.AssignImageList(some image_list)
self.root =self.tree_list.AddRoot("abc", ct_type=1)
for my_text in [ 'def', 'ghi', 'jkl' ]:
node =self.tree_list.AppendItem( self.root, testware_path, ct_type =1)
self.tree_list.SetItemText( node, text=my_text, column=1)
self.tree_list.SetItemText( node, text=my_text, column=2)
if my_text.startswith('d'):
node.Check()
Created myComboBox with:
myComboBox = QtGui.QComboBox()
Populated it with three items:
myItemsList = ['item01', 'item02', 'item03']
for item in myItemsList:
myComboBox.addItem(item)
Now I want to set a comboBox to a second item knowing only string value of the item: 'item02'.
Let's assume I can't index myItemsList to find out what indexed position of an item with a value 'item02'.
I want to set a myComboBox without using an item index number but its string value. Thanks in advance!
Use QComboBox.findText:
index = myComboBox.findText('item02')
ing_scroll = Scrollbar(window1_frame1, orient=VERTICAL)
ingredients = ttk.Treeview(window1_frame1, yscrollcommand=ing_scroll.set, height=5, columns=['Ingredient', 'Amount'], show="headings")
ingredients.heading("Ingredient", text='Ingredient')
ingredients.column("Ingredient", width=7)
ingredients.heading("Amount", text='Amount')
ingredients.column("Amount", width=1)
ing_scroll.config(command=ingredients.yview)
ing_scroll.pack(side=RIGHT, fill=Y)
ingredients.pack(side=LEFT, fill='both', expand=1)
def OnRecpSelect(event):
DB = menu_combo.get()
mytable = recipe_combo.get()
ingredient_list = TKengine.pull_ingredients(DB, mytable)
# NEED TO CLEAR THE INGREDIENTS TTK:TREEVIEW OBJECT HERE!
for i in ingredient_list:
ingre = i[1]
amoun = i[2]
value = ingre,amoun
ingredients.insert('',0,values=value)
ingredient_list is a list that displays something like... ('Sugar', '1 Cup') and so on... The def is for a combobox that is selected, so what I would like is for the treeview to clear and not just keep adding more ingredients. Unfortunately I don't see a clear() method.
If theres a programmatic way of identifying what is there first (enumerating a rowcount would be good...) this is driving me nuts. I did notice in the docs that you can use the delete method, but it wants to know what the item is to delete... if I use:
ingredients.delete('',0)
I get
TclError: Item 0 not found
So I would assume it wants something like 'Sugar' as the Item...
of course its a catch 22 because if you select the combobox and want to clear the ingredients treeview, the same ingredient items are not in every recipe, so how do we know what items to delete?...
Please let me know if you need any more details... I am fairly new to working with the treeview object, but its making me want to just work with two listboxes on a canvas.
To just make the code a bit more concise and Pythonic:
map(ingredients.delete, ingredients.get_children())
When you insert an item on the tree, the insert method returns an item id. This is what you give to the delete method.
Also, given an item id (such as the root item), you can get a list of all of its children with the get_children method. If you do not give any arguments to the get_children it will return a list of all the items that belong to the root element. You can then iterate over this list to delete the items.
This is all documented in the treeview docs at docs.python.org.
I have a ttk.Treeview widget with some rows of data. How do I set the focus to and select (highlight) a specified item?
tree.focus_set()
does nothing
tree.selection_set(0)
complains that: Item 0 not found, although the widget is plainly populated with more than zero items. Trying item 1 does no better.
EDIT: to select an item, find its id, then use tree.selection_set(id). Neither tree.focus(id) nor tree.focus_set(id) appears to do anything.
Get the id of treeview item you want to highlight/select
child_id = tree.get_children()[-1] # for instance the last element in tuple
To highlight the item, use both focus() and selection_set(item_id)
tree.focus(child_id)
tree.selection_set(child_id)
Note: I haven't worked with python.
Looking at this link, the focus method with optional parameter item, should highlight the node.
If not, look at selectmode option & set it to "browse".
Come across this question when I'm looking to solve the exact same problem.
Found out this:
tree.selection_set(item) highlights the item
tree.focus(item) or tree.focus_set(item) selects the item
def mycallback(event):
_iid = treeview.identify_row(event.y)
global last_focus
if _iid != last_focus:
if last_focus:
treeview.item(last_focus, tags=[])
treeview.item(_iid, tags=['focus'])
last_focus = _iid
treeview.tag_configure('focus', background='red')
global last_focus
last_focus = None
treeview.bind("<Motion>", mycallback)
Use
tree.selection_add(item_iid)
The reason why
tree.selection_set(0) doesn't work is because 0 is not the item iid, it's the index you're referring to and Treeview is expecting an iid.