-1

How can I use a Qt designer object name in the form 'boxXY' where X is the row value and Y value is the column value for a python 2D list;

import sys,os
from PyQt5 import QtCore, QtGui, uic
rowsColumns=[[],[],[],[],[],[],[],[],[],[]]##Current grid values

allScreens=uic.loadUiType("newSudokuScreens.ui")[0]

class aWindowClass(QtGui.QMainWindow, allScreens):
    def __init__(self, parent=none):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.GPScreen.setVisible(True)
        self.CPScreen.setVisible(False)
        self.DSScreen.setVisible(False)
        self.HTPScreen.setVisible(False)
        self.MMScreen.setVisible(False)
        self.NPSUScreen.setVisible(False)
        self.PSIScreen.setVisible(False)
        self.RUPScreen.setVisible(False)
        self.SPUScreen.setVisible(False)
        self.box00.clicked.connect(self.findRowColumn)
    
    def findRowColumn(self):
        row=#4th character of button name
        column=#5th character of button
        numberWanted=int(input('enter value wanted')) #is part of UI but making row and column value work will help make this work
        rowsColumns[row][column]=numberWanted
    
for i in range(0,9):
    for j in range(0,9):
        rowsColumns[i].append(0) #fill grid with necessary 81 spaces for sudoku
    print(rowsColumns[i])
    
app=QtGui.QApplication(sys.argv)
aWindow=aWindowClass(None)
aWindow.show()
app.exec_()

So for an example if button box56 is clicked I want to pass box56 as a variable then in the procedure when a box is clicked row will be set as 5 and column will be set as 6 then append it to that place in the 2D list.

Thanks in advance.

Samuel W
  • 39
  • 7
  • Unsure whether this will answer your problem, but in the code of the slot, you can use the `sender` method to get a reference to the object that send the clicked message. From the object, it is easy to extract the title or any other value. – Serge Ballesta Oct 14 '20 at 13:08
  • @SergeBallesta can you give an example of sender method – Samuel W Oct 14 '20 at 13:17
  • Of course. But you should first put your code in text in the question itself so that I can copy and paste it. – Serge Ballesta Oct 14 '20 at 15:30
  • Yeah no problem just added code for you to copy and paste – Samuel W Oct 14 '20 at 16:47

1 Answers1

0

Independently of the solution proposed by @eyllanesc, it is easy from a slot to find the object that sent a signal and its properties.

Here you could do:

def findRowColumn(self):
    button_name = self.sender().text()   # gets the text of the button
    row = int(button_name[3]) # 4th character of button name
    column = int(button_name[4]) # 5th character of button
    numberWanted = int(
        input('enter value wanted'))  # is part of UI but making row and column value work will help make this work
    rowsColumns[row][column] = numberWanted
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Hi @Serge Ballesta thanks for an answer but unfortunately the answer given isn't quite what I need as self.sender().text() gives the text inside the object not the actual name of the object, and I can't have the button name in the text so is there a similar or is there not actually a way to pass the object name, the button clicked to run the procedure – Samuel W Oct 19 '20 at 12:43
  • @SamuelW Maybe what you want is `objectName` and not `text`. An other useable property is `windowTitle` – Serge Ballesta Oct 20 '20 at 05:21