프로그래밍/python
[pyqt5] 두개의 버튼이 하나의 함수를 실행할 때 어떤 버튼이 클릭되었는지 구분하는 법
이휘재123
2023. 4. 10. 14:53
반응형
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton
from PyQt5.QtCore import QObject, pyqtSignal
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
# 버튼 생성
self.button1 = QPushButton("Button 1", self)
self.button2 = QPushButton("Button 2", self)
# 버튼 위치 조정
self.button1.move(50, 50)
self.button2.move(150, 50)
# 버튼 클릭 시그널에 함수 연결
self.button1.clicked.connect(self.onButtonClicked)
self.button2.clicked.connect(self.onButtonClicked)
def onButtonClicked(self):
# sender()를 이용하여 어떤 버튼이 호출했는지 확인
button = self.sender()
if button == self.button1:
print("Button 1 clicked")
elif button == self.button2:
print("Button 2 clicked")
if __name__ == '__main__':
app = QApplication([])
window = MyWindow()
window.show()
app.exec_()
sender()를 이용해서 어떤 버튼이 클릭되었는지 구분할 수 있다.
반응형