Pyside2 PushButton에 마우스 커서를 올리면 반응하게 할려면 어떻에 해야 되나요?

조회수 566회

제목 그래로 Pyside2에 PushButton위젯 위에 마우스 커서를 올리면 버튼의 택스트나 이미지가 바뀌는 등에 기능을 넣고 싶은데 어떻에 해야 할 지 모르겠습니다.

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • 참고하세요.

    import sys
    from PySide2 import QtCore
    from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QMessageBox
    
    class MyForm(QWidget):
        def __init__(self,parent=None):
            QWidget.__init__(self,parent)
            self.setWindowTitle('Button Demo')
    
            self.button = QPushButton('Ok',self)
            self.button.clicked.connect(self.do_clicked)
            self.button.installEventFilter(self)
            mainlayout = QVBoxLayout()
            mainlayout.addWidget(self.button)
            self.setLayout(mainlayout)
    
        def eventFilter(self, obj, event):
            if obj == self.button and event.type() == QtCore.QEvent.HoverEnter:
                self.button.setText("Ko")
            elif obj == self.button and event.type() == QtCore.QEvent.HoverLeave:
                self.button.setText("Ok")
            return super(MyForm, self).eventFilter(obj, event)
    
        def do_clicked(self):
            buttonReply = QMessageBox.question(self, 'title', "message", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if buttonReply == QMessageBox.Yes:
                print('Yes clicked.')
            else:
                print('No clicked.')
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        form = MyForm()
        form.show()
        app.exec_()
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)