0

I'm trying to connect multiple buttons to a single function where depending on the button chosen, the location where the results would be returned will differ. I want the function to

  1. recognize which button is chosen
  2. return the results to the slot that corresponds to a button

Here is an example code that may help you guys to understand my question. So, for example, when I press button2, I want the function to recognize that button2 was pressed and then change the text in lineedit2 to whatever (Later on, I would like to do some calculations in the function and want the corresponding line edit to display the results). I would like to connect all 3 buttons to the same function 'clickme' and do something. Or is there a better way to do this?

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
 
 
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("Python ")
 
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
 
        # calling method
        self.UiComponents()
 
        # showing all the widgets
        self.show()
 
    # method for widgets
    def UiComponents(self):
        
        
        # creating a push button
        button1 = QPushButton("button 1", self)
        button2 = QPushButton("button 2", self)
        button3 = QPushButton("button 3", self)
 
        # setting geometry of button
        button1.setGeometry(50, 150, 100, 40)
        button2.setGeometry(220, 150, 100, 40)
        button3.setGeometry(400, 150, 100, 40)
 
        # adding action to a button
        button1.clicked.connect(self.clickme)
        button2.clicked.connect(self.clickme)
        button3.clicked.connect(self.clickme)
        
        lineedit1 = QLineEdit("slot1",self)
        lineedit2 = QLineEdit("slot2",self)
        lineedit3 = QLineEdit("slot3",self)
        
        lineedit1.setGeometry(50, 300, 250, 40)
        lineedit2.setGeometry(220, 300, 250, 40)
        lineedit3.setGeometry(400, 300, 250, 40)
        
        # creating label to print text
        # label = QLabel(text, self)
        # label.move(200, 200)
 
    # action method
    def clickme(self):
        
        #########
        # some piece of code that will change the slot corresponding to the text
        # maybe make the corresponding line-edit display something like "button ## was chosen"
        #########
        
        print("pressed")
 

App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
color_blue
  • 31
  • 6
  • Why not send the button obj to the function? – MR.code May 17 '22 at 21:21
  • 1
    Use [`self.sender()`](https://doc.qt.io/qt-5/qobject.html#sender) to know the object that emitted the signal. Then use a dictionary with buttons as keys and line edits as values. – musicamante May 17 '22 at 21:22
  • @MR.babady I think I found out how to pass the name of the button but wasn't sure how to return the results corresponding to it – color_blue May 17 '22 at 21:25
  • @musicamante Oh I think dictionaries would actually work. When I use self.sender(), would that be like my first line in my function? – color_blue May 17 '22 at 21:26
  • @color_blue it's the line where you need it. – musicamante May 17 '22 at 21:27
  • use `self` for your obj like `self.label = QLabel("text", self) self.label.move(200, 200)` So you can call wherever you need to – MR.code May 17 '22 at 21:28
  • @ musicamante does self.sender() if the text on the buttons are all the same? If it does, could you let me know what the syntax would be? – color_blue May 17 '22 at 21:58
  • 1
    @color_blue `sender` returns the object that emitted the signal, what object that doesn't matter, nor the button text would change its result: the keys of the dictionary must be *the buttons*, not their texts: `button = self.sender()` `lineedit = self.objects[button]` – musicamante May 18 '22 at 07:25
  • @ musicamante I see what you are saying. But I do have another question about that. So, self.sender() returns the name object in a form of ``. But then don't I need the name of the object rather than what I am getting? Is there a way to get the name of the object from what I get from the sender? I make the dictionary first and then use the function later. I have a predefined dictionary like `dict[button1] =lineedit1`. I thought I could get a name like "button" from the sender and use that for the dictionary. Am I missing something? – color_blue May 18 '22 at 08:35

0 Answers0