Python Tkinter : Assignment of A Matrix of Entry Boxes - python

I created a matrix of entry boxes using Tkinter and Python 2.3. What I want to do is to check the row of the entry box for a keyword and then insert a text into an entry box with the same row but of different column. For example, take a simple 3x3 entry box matrix:
Food Like Dislike
Apple Yes
Orange Yes
Let's say I have two lists that corresponds to each other as such:
1. [Apple, Orange]
2. [Dislike, Like]
I would like to check that if first element in list 1 is Apple and first element in list 2 is Dislike, then I want to insert a text "YES" into the entry box of 2nd row and 3rd column as shown in the above matrix.
I have appended the entry box indexes into two lists. First list contains the indices of the 1st column of entry boxes. Second list contains the 2nd and 3rd columns of the entry boxes. Problem is, how to determine the correspoding row and column of the entry box to insert the Yes???
Hope you can understand! This example is the most elementary one. My actual matrix consist of more rows and columns. An idea or sample code will help a lot.
Thank you Tkinter Gods.

Store references to the entry widgets in a dictionary of lists. Here's an example using the label as the key:
for item in list_of_things:
label = tk.Label(...)
column0 = tk.Entry(...)
column1 = tk.Entry(...)
matrix[label] = (column1, column2)
You can now get column1 of "Orange" with something like matrix["Orange"][1]

Related

How to correspond a list value to another value in another list in python

My current line of code is shown below:
year = [1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016]
intake = [26242.5,27906.7,29192.7,30407.5,1564.9,2313.5,2989.4,3668.6,22082.6,29964.2,30481.6,35981.7,39597.6,38659.6, 34325.9,6913.5,35780.6,39111.5,37116.5,35385.5]
outtake = [14311.4,12135.1,3381.0,2186.8,22732.6,21519.8,19932.3,18903.2,24347.6,3307.0,3261.5,34668.1,43695.2,36286.6,26648.6,34325.9,36913.5,26580.6,27679.5,28643.0]
I want to find the 3 lowest values in 'outtake' list and label as variables {val1}, {val2}, {val3} which corresponds with the same row[] in the 'year' list as variables {yr1}, {yr2}, {yr3} respectively.
The desired output shld be a print message:
The 3 lowest outtake values are {val1}, {val2}, {val3} at year {yr1}, {yr2}, {yr3} respectively.
How should I go about doing it?
You can use zip to iterate over two lists at the same time, sorted to sort them (by the first element) and slice notation ([start:end]) to get the first three
sorted(zip(outtake, year))[:3]

How to insert multiple values into specific treeview columns?

I have a database returning the total of several column and I am trying to display it in a treeview. If I do
for i in backend2.calc_total()[0]:
treeviewtotal.insert("", END, values=i)
I get
which is not what i want as i want everything to start from "food" column onwards. I cant make date a iid as i already have an iid that I am referencing to my database.
If I do
list2 = ['Date', 'Food', 'Transport', 'Insurance', 'Installments', 'Others']
for i in range(len(backend2.calc_total()[0][0])):
treeviewtotal.insert("", 0, list2[i+1], values=backend2.calc_total()[0][0][i])
I get this
instead, all the totals get stacked into 1 column (which is scrollable).
Any way to achieve my aim of allocating the respective totals to the respective column in a same row? Thanks!
With reference to the first attempt, the following solves the problem:
for i in backend2.calc_total()[0]:
treeviewtotal.insert("", END, values=([], *i))
values= takes in a list. Therefore we add an empty space by using [], but since i itself is already a list, we need to "flatten out" the list by doing *i.
Please correct me if I used any parts of the code wrongly. Still trying to learn =)

How to create arrays which are subgroups of larger parent array based upon values of chosen column in parent array?

I am very new to Python and programming in general. I need to organize a large array of raw data to plot.
The data table has 3 columns: device ID , voltage , current .
The first column has many repeated elements because each 'device ID' has many voltages and currents measured, so there's only a fraction of the rows with unique 'device ID'.
My goal is to plot each device ID in a separate series.
Now, in my mind, I need to separate the array into smaller arrays to use typical plotting commands of matplotlib.
How can I tell python to figure out where in the device ID column does new value appear and either put it into a new array?
I've played around with numpy.unique , but I am now thinking I need for loop to check each row and compare it to previous row, something like:
for row a in Device ID column:
is row n = n-1?
if true then continue populating new array with all columns for this row
if false then start new array with all columns for this row
All I've really accomplished is sorting the big array by the device ID column, since I believe it is first step to using the for loop approach
sort the data by Dev ID (2nd column in raw data)
data_sorted_bydev_id = data[ data[:,1].argsort() ]
Just looking for advice if the 'for loop' approach is best way to go from here.

Fill the gap in every row of ObjectListView how?

Here's the problem, if you don't use check-state column in ObjectListView, there's always this gap in every row:
Especially when you want to use a small column to show row numbers:
This is so NOT looking good, how do you make each row expand to the start?
I've just been adding an extra first empty column that has no no valueGetter and a width of 0 so it doesn't show, that way the following column starts on the left edge.
Add the following as the first column.
ObjectListView.ColumnDefn(title="", valueGetter="", maximumWidth=0)

PyQt Get cell from TableView with header label

I currently load the results of an SQL query into a TableView.
self.projectModel = QSqlQueryModel()
self.projectModel.setQuery(sql,db)
I then need to select a specific cell based on the header label (geometry). This column will move position depending on the different table that is search.
When the user clicks anywhere on the row (NOT the cell of geometry column) I would like to select the geometry column cell.
At the moment I have a this associated with the tableView
self.dlg.tableView.clicked.connect(self.cellClicked)
And in that function I have
row = self.projectModel.currentIndex()
If I use QTableView.model(row, column) to select the index, I have to speciify the row and column number. However, this would vary so I would want to do QTableView.model(row, 'geometry') however the model expects integers.
Any solutions?
thanks
So it seems all you need is a method to find a column from its header label, i.e. something like:
def columnFromLabel(self, label):
model = self.table.horizontalHeader().model()
for column in range(model.columnCount()):
if model.headerData(column, QtCore.Qt.Horizontal) == label:
return column
return -1

Categories