Guest User

Untitled

a guest
Nov 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. btn.clicked.connect(self.lb_pressed)
  2. def lb_pressed(self):
  3. sender = self.sender()
  4. if sender.isEnabled():
  5. sender.change_color('red')
  6.  
  7. def mousePressEvent(self, event):
  8. if event.button() == Qt.LeftButton:
  9. self.lb_pressed()
  10. elif event.button() == Qt.RightButton:
  11. self.rb_pressed()
  12.  
  13. def rb_pressed(self):
  14. sender = self.sender()
  15. if sender.isEnabled():
  16. sender.change_color('green')
  17.  
  18. AttributeError: 'NoneType' object has no attribute 'isEnabled'
  19.  
  20. from PyQt5 import Qt
  21.  
  22.  
  23. class MyButton(Qt.QPushButton):
  24.  
  25. def __init__(self, *args, **kwargs):
  26. super().__init__(*args, **kwargs)
  27.  
  28. def mousePressEvent(self, event):
  29. button = event.button()
  30. if button == Qt.Qt.RightButton:
  31. print("Right button click!")
  32. self.setStyleSheet("""QPushButton{
  33. background-color: #aaaaff;
  34. border: 1px solid black;
  35. border-radius: 5px;
  36. }
  37. """)
  38. elif button == Qt.Qt.LeftButton:
  39. print("Left button click!")
  40. self.setStyleSheet("""QPushButton{
  41. background-color: #ffaaaa;
  42. border: 1px solid black;
  43. border-radius: 10px;
  44. }
  45. """)
  46. return Qt.QPushButton.mousePressEvent(self, event)
  47.  
  48.  
  49. class Widget(Qt.QWidget):
  50.  
  51. def __init__(self, *args, **kwargs):
  52. super().__init__()
  53. layout = Qt.QGridLayout(self)
  54.  
  55. for i in range(8):
  56. for j in range(8):
  57. layout.addWidget(MyButton("Button[{}, {}]".format(i, j)), i, j)
  58.  
  59.  
  60. if __name__ == '__main__':
  61. app = Qt.QApplication([])
  62. w = Widget()
  63. w.show()
  64. app.exec()
Add Comment
Please, Sign In to add comment