Search code examples
pythonpyqtqt-designer

PyQt- Button Manipulation


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.


Solution

  • 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