Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- btn.clicked.connect(self.lb_pressed)
- def lb_pressed(self):
- sender = self.sender()
- if sender.isEnabled():
- sender.change_color('red')
- def mousePressEvent(self, event):
- if event.button() == Qt.LeftButton:
- self.lb_pressed()
- elif event.button() == Qt.RightButton:
- self.rb_pressed()
- def rb_pressed(self):
- sender = self.sender()
- if sender.isEnabled():
- sender.change_color('green')
- AttributeError: 'NoneType' object has no attribute 'isEnabled'
- from PyQt5 import Qt
- class MyButton(Qt.QPushButton):
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- def mousePressEvent(self, event):
- button = event.button()
- if button == Qt.Qt.RightButton:
- print("Right button click!")
- self.setStyleSheet("""QPushButton{
- background-color: #aaaaff;
- border: 1px solid black;
- border-radius: 5px;
- }
- """)
- elif button == Qt.Qt.LeftButton:
- print("Left button click!")
- self.setStyleSheet("""QPushButton{
- background-color: #ffaaaa;
- border: 1px solid black;
- border-radius: 10px;
- }
- """)
- return Qt.QPushButton.mousePressEvent(self, event)
- class Widget(Qt.QWidget):
- def __init__(self, *args, **kwargs):
- super().__init__()
- layout = Qt.QGridLayout(self)
- for i in range(8):
- for j in range(8):
- layout.addWidget(MyButton("Button[{}, {}]".format(i, j)), i, j)
- if __name__ == '__main__':
- app = Qt.QApplication([])
- w = Widget()
- w.show()
- app.exec()
Add Comment
Please, Sign In to add comment