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
- recognize which button is chosen
- 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())