Find an item by its position in a grid tkinter - python

In tkinter, is there a way for me to reference a widget within a grid by its row and column, in the same way that you would be able to reference an item within a list (or list of lists) by knowing its position in the list?

You can call the .grid_slaves(row, column) method on the parent widget; this will return a list (possibly empty) of the widgets in that cell.
You could also iterate over all of the child widgets (.grid_slaves() with no parameters, or .winfo_children()) and call .grid_info() on each one. This returns a dictionary with 'row' and 'column' keys, along with various other grid parameters.

Actually, I realised that I could solve my own problem in a much simpler way, by literally making a list of lists, with each sub-list containing all of the widgets for a single row, and therefore I can refer to each item through it's row and column.

Related

Looping over grid to get label and entry text values in tkinter

I have created a window using tkinter, this window contains a grid of Labels and Entries. In the Entries I have edited some text that I want to save back to the source.
Everything I'm reading so far says that I need to create a separate list and save each entry text variable in the list.
But isn't there a better way to get the values directly from the controls themselves? I know I can loop over mywindow.winfo_children or mywindow.children. So if I can do this, then I should be able to get the text values directly, no?
I just don't know which property to get the value from.
Any ideas out there?
This is the answer.
for child in context.grid_slaves():
if(type(child) is label):
print (child['text'])
if(type(child) is entry):
print(child.get())
I can also find out where I am in the grid like this: child.grid_info() and so I can synch back to the source.
Just to elaborate on the answer.
Loop through the window grid items using the grid_slaves method.
Get the row and column using the widget's grid_info method:
for child in window.grid_slaves():
g_info = child.grid_info()
if type(child) is Button:
row = g_info['row']
col = g_info['column']
text = child['text']
print(row, col, text)

Pop method removes random element instead of top element in Python?

In Python when we use pop method to remove an element from set why does it remove any random element? Instead of removing top element as per the stack's nature.
Because a set is an un-ordered collection of items, by definition. There it no "first" or "last" element. So, it removes a random one. If you want order, use a list instead.

How can I delete an element from a multidimensional array in Python?

I'm trying to make a tree structure on python using a multidimensional array on python. I'm using this line of code to create my list:
matrix = [[-1 for x in range(5)] for x in range(3)]
If I print the list it will return this:
[[-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1]]
This next paragraph is a small explanation of what I'm doing just in case anyone is interested or if it helps you give a more complete solution for my problem. The actual question is in the next paragraph.
The first position within the array is the key or name of the element, the second position is who is the parent node of the element, the third position tells you if the node has any children and the last two positions tell you who the predecessor and successor of the element are, if there are any.
The question starts here:
Anyway I'm also required to create a garbage collector that collects any node and its children if I were to choose to delete the node. However I don't know how to delete the entire element. I found another thread that suggested using something called pop to delete things from the multidimensional array. But from what I gathered after reading the answers the pop method only deletes an element within the list meaning it would only delete a -1 instead of deleting everything. So is there any method of deleting one lists within the list in its entirety.
pop deletes an element in a list even if that element is a list so to remove the first list in matrix you can use
matrix.pop(0)

How to sort View items

Setting an setSortingEnabled() to True makes it possible to click the column header name to sort the view's items:
tableView=QTableView()
tableView.setSortingEnabled(True)
But even while the attribute has been set the view will display the items unsorted.
In order to sort the items the header must be clicked.
Question: How to make view go ahead and to sort its items before the header is clicked.
So the view is sorted straight from the start.
You can use QHeaderView.setSortIndicator(logicalIndex, order)
For your example, this would mean calling tableView.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder) to sort the first column in ascending order.
Note that you are passing in logicalIndex which may not correspond to the visualIndex if the columns have been reordered. QHeaderView provides methods for translating between the two if you need it (but I think it is unlikely you will need it).
To sort QTableView() without clicking on its header (assuming the tableView.setSortingEnabled(True) was set) use:
tableView.sortByColumn(0, Qt.AscendingOrder)

Referring to objects inside a list without using references or indices

I'm using python for my shopping cart class which has a list of items. When a customer wants to edit an item, I need to pass the JavaScript front-end some way to refer to the item so that it can call AJAX methods to manipulate it.
Basically, I need a simple way to point to a particular item that isn't its index, and isn't a reference to the object itself.
I can't use an index, because another item in the list might be added or removed while the identifier is "held" by the front end. If I were to pass the index forward, if an item got deleted from the list then that index wouldn't point to the right object.
One solution seems to be to use UUIDs, but that seems particularly heavyweight for a very small list. What's the simplest/best way to do this?
Instead of using a list, why not use a dictionary and use small integers as the keys? Adding and removing items from the dictionary will not change the indices into the dictionary. You will want to keep one value in the dictionary that lets you know what the next assigned index will be.
A UUID seems perfect for this. Why don't you want to do that?
Do the items have any sort of product_id? Can the shopping cart have more than one of the same product_id, or does it store a quantity? What I'm getting at is: If product_id's in the cart are unique, you can just use that.

Categories