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()
Related
I am having a list which having tuple object and I need to search the list for all tuples which contain the string typed in search box. can anyone please help me into this?
I created one search box which having binding function. I can get the text from search box. but for the same text I need to find objects form the list
I need to append searched item in new list. so that I can update them in treeview.
from logging import root
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("500x200")
def searchBox_fun(e):
typed=Search_box.get()
new_list=[]
####### I need to search here #####
for item in data:
if typed in item:
print(item)
new_list.append(item)
data=[
('SS Tubing, Primary Head Lower Transducer', 'ABC324232', 10.0),
('Assy Tubing, L Head- Top Transducer', 'ABC093762', 20.0),
('Assy Tubing, Lower Transducer-Accum Head', 'ABC099762', 13.0),
('Assy Tubing ,Top Transducer- Vent Valve', 'ABC0911562', 23.0)]
Search_box=Entry(font=("Helvetica", 12), width=40)
Search_box.place(x=60, y=30)
Search_box.bind("<KeyRelease>", searchBox_fun)
root.mainloop()
but for the same text I need to find objects form the list
There are many ways to search through a string (i.e. how will you handle spaces in words? If someone types "Transducer Lower" would you want the tuple in index 2 to be a result?
But to access what I think is what you're looking for, you would index into the tuple: item[0]
So something like this:
for item in data:
if typed in item[0]:
...
You can ignore case by, say, comparing the uppercase versions of typed and item[0]:
for item in data:
if typed.upper() in item[0].upper():
...
I am trying to create a simple 'SOS' puzzle game from Tkinter. I am creating an entry widgets grid using the grid method. Now I want an assigned variables for each entry. I try to do it using for loops but I can't use that variable the proper way. Can you help me? My question idea explain given below digram,
Code
for i in range(5):
for j in range(5):
self.entry=Entry(root,textvariable=f'{i}{j}')
self.entry.grid(row=i,column=j)
self.position.update({f"{i}-{j}":f"{i}{j}"})
enter code here
Instead of creating variables on the go, you should store the widgets made into a list or a dictionary. The list seems more reasonable as you can index it more easily.
Below is a simple, but full example that shows how you can make a 2-D list as well as search this list for the entry object:
from tkinter import * # You might want to use import tkinter as tk
root = Tk()
def searcher():
row = int(search.get().split(',')[0]) # Parse the row out of the input given
col = int(search.get().split(',')[1]) # Similarly, parse the column
obj = lst[row][col] # Index the list with the given values
print(obj.get()) # Use the get() to access its value.
lst = []
for i in range(5):
tmp = [] # Sub list for the main list
for j in range(5):
ent = Entry(root)
ent.grid(row=i,column=j)
tmp.append(ent)
lst.append(tmp)
search = Entry(root)
search.grid(row=99,column=0,pady=10) # Place this at the end
Button(root,text='Search',command=searcher).grid(row=100,column=0)
root.mainloop()
You have to enter some row and column like 0,0 which refers to 1st row, 1st column and press the button. Make sure there are no spaces in the entry. You should not worry much about the searching part, as you might require some other logic. But instead of making variables on the go, you should use this approach and store it in a container.
Also a note, you do not have to use textvariable as here the need of those are totally none. You can do whatever you want with the original object of Entry itself. Just make sure you have the list indexing start from 0 rather than 1(or subtract 1 from the given normal values).
Each column item of QTreeWidgetItem is populated with QComboBox.
So let's say I create an QTreeWidgetItem:
tree=QtGui.QTreeWidget()
treeItem=QtGui.QTreeWidgetItem()
tree.addTopLevelItem(treeItem)
for i in range(10):
tree.setItemWidget(item, i, QtGui.QComboBox())
Now if I comment out tree.addTopLevelItem(treeItem) no comboboxes will be added to the QTreeWidgetItem. By other words, I have to add QTreeWidgetItem to Tree in order to be able to add QComboBoxes to QTreeWidgetItem. But at the time QTreeWidgetItem is created I don't know if it is going to be a topLevelItem' or if it will be added to another topLevelItem as a child item using.appendRow()` method.
Should I just go ahead and assign this item to the Tree as a topLevel and then reassign it as a child? What method should I use to make a topLevelItem a child item of another top level item?
It have many way to create data in QTreeWidget in my favorite way to create, I have use constructor of QtGui.QTreeWidgetItem to link it too root item with out using QTreeWidget.addTopLevelItem and suggest not use it.
Example like this;
myQTreeWidget = QtGui.QTreeWidget()
listsHeader = ['A', 'B', 'C']
myQTreeWidget.setHeaderItem(QtGui.QTreeWidgetItem(listsHeader))
dataQTreeWidgetItem = QtGui.QTreeWidgetItem(myQTreeWidget) # assgin QTreeWidget
for i in range(len(listsHeader)):
dataQTreeWidgetItem.setText(0, 'INDEX ' + str(i)) # Test text in column
myQTreeWidget.setItemWidget(dataQTreeWidgetItem, i, QtGui.QComboBox())
Reference : http://pyqt.sourceforge.net/Docs/PyQt4/qtreewidgetitem.html#QTreeWidgetItem-3
EDITED 0:50 7/8/2014
Add header in list.
Regards,
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.