How to get index of current selected row in Gtk.TreeView in Python ?
The other answer is likely more useful in general. However, to answer the actual question posed by the OP, how to get the row index: assuming one row is selected, you can get it with:
index = treeview.get_selection().get_selected_rows()[1][0][0]
You can call gtk.TreeView.get_selection to get the current selection (gtk.TreeSelection). You can then call gtk.TreeSelection.get_selected to get:
a 2-tuple containing a reference to the gtk.TreeModel and a gtk.TreeIter pointing to the currently selected node.
The iter can be used on a gtk.TreeModel (which is obtained by calling gtk.TreeView.get_model. You can then use gtk.TreeModel.get_value to get any of the column values of the node at that position in the tree.
Somewhat late, perhaps not so relevant, but if you have double clicked on the selected row you will get the row index as element of the TreePath tuple, like so
def on_row_activated(self, treeview, path, column):
model = self.treeview.get_model()
tree_iter = model.get_iter(path)
row = path[0]
if tree_iter:
# here you can get the values of the columns
Related
Please see the below code that I am using to scrape content from a dynamically generated page and then placing into a CSV. The problem I am running into now is that each "row" could potentially be missing certain elements that I would like to insert as "blank" or some other placeholder so that all of the rows are correctly located under the correct column header when I view this in excel.
with open(filename, 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile)
for heading in All_Heading:
driver.execute_script("return arguments[0].scrollIntoView(true);", heading)
#print("------------- " + heading.text + " -------------")
ChildElement = heading.find_elements_by_xpath("./../div/div")
for child in ChildElement:
driver.execute_script("return arguments[0].scrollIntoView(true);", child)
#print(heading.text)
#print(child.text)
row = [heading.text, *child.text.split("\n")] # You can also use a tuple here
print (row)
csvwriter.writerow(row)
Is it possible to place the sort of logic I am after in the writer statement or will I need to specifically go after each element I am after to know if it is empty or not? In the end I want each row to contain the same number of elements, even if it means some of them are blank or filler text as this will keep the overall struture of the data intact.
Examples of the output below:
As you can see some elements were empty and as such the following elements end up out of line/in the wrong column. (11,12,13,26)
On the same note, is it possible to know extra information about the element I am dealing with in the loop that prints the row? If I knew the class I could then know if its the title, price, weight, brand or so on.
Thank you!
It’s not possible to identify what’s missing from the text itself, at least not reliably since you’d have to search for certain substrings to identify the fields. However if you do have all these values in separate elements under child, you can associate each element with its field in two ways assuming there is a class uniquely identifying each field:
You can go through all the children using WebElement. find_elements(By.XPATH, ".//*"), record what fields are present using someClass in WebElement.get_attribute("class"), and later fill in blanks for the missing ones.
You can use WebElement .find_elements() (which searches through WebElement's children) and filter by the known class name i.e. find_elements(by=By.CLASS_NAME, value=className):
# ...
fieldClasses = ["Title", "Price", ...] # These are just example classes
for fieldClass in fieldClasses:
element = child.find_elements(by=By.CLASS_NAME, value=fieldClass)
row.append(element.text if element else "Blank")
# ...
You can simply replace empty spaces with some text, before saving the rows.
Ex:
row = [heading.text, *child.text.split("\n")] # You can also use a tuplehere
print (row)
for i in range(len(row)):
if len(row[i]) < 1:
row[i] = "Filler"
csvwriter.writerow(row)
I have list like below.
test = ['firstvalue', 'thirdvalue']
I want to insert the some values to the list.
secondvalue at index 1 and fourthvalue at index 3
so the output list looks like below
test = ['firstvalue', 'secondvalue', 'thirdvalue', 'fourthvalue']
I tried the below way but it doesn't work for me
print test.insert(1, "secondvalue")
Is there any alternate way to do this?
The insert function does not return a value, but rather modifies the array used on it. Here's an example:
test = ['firstvalue', 'thirdvalue']
test.insert(1, "secondvalue")
print test
I am trying to iterate over first column and look for specific value.
If the row contains that specific value I want to get the row number and save it into a variable.
here is the code:
rows_exceptions_file = []
for cell in sheet2.col(0):
if cell.value == "test01":
rows_exceptions_file.append(cell.rowx)
Exception that I am getting is : 'Cell' object has no attribute 'rowx'
If the rows are numbered like 0, 1,.. you can just use enumerate like this:
for i, cell in enumerate(sheet2.col(0)):
if cell.value == "test01":
rows_exceptions_file.append(i)
enumerate returns the number of the iteration and the element, which makes it a perfect fit for this task, I think.
EDIT: enumerate also takes a start-argument, if you want to start the numeration from anything else then 0:
enumerate(sheet2.col(0), start=1)
In my code, I've created a QTableWidget with 50 columns and 2 rows.
By executing a function, Python put in cells list's elements that I've created before. But, I don't know how to modify these cells.
For example, I want to get the current data of a cell at (x,y) and add an integer. So I've tried :
content = int(self.ui.table.item(X, Y).text()) #I've just strings in my cells
self.ui.table.item(X, Y).setText(str(content + 1)
But that part of code, don't work.
I've tried too :
a=self.ui.table.item(X,Y)
print(a.data(0).toString())
But Python return me :
'NoneType' object has no attribute 'data'
Try this,hope this will work:
val = 21 # integer value
row = X # this is your row number
self.ui.table.insertRow(row) # here you decide in which row
item = QTableWidgetItem(str(val)) # create a new Item
self.ui.table.setItem(row,Y, item)
This will override the value at X,Y index of QTableWidget cell.
if you used QtDesigner to create the user interface, particularly the tables, is probably that you have a instances problem, Qtdesigner create the table (QTablewiget) but does not create cells (QTableWidgetItem) inside the table, these is the reason that when you try to get the item X, Y you got a "noneType" object, I know its sound crazy there is 3 ways to solve these:
1st : you can create the individual items as #zeb show it
2do : fill the cell on QtDesigner with a value, with this Qtdesigner create the tables items
3th : you can crare each element you need to use on the setupUi funciton as :
item = self.table.item(X,Y)
item.setText(_translate("MainWindow", " "))
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.