fork download
  1. from PyQt5.QtWidgets import QPushButton, QWidget, QApplication, QHBoxLayout
  2. from PyQt5.QtCore import QObject
  3.  
  4.  
  5. class Button(QPushButton):
  6.  
  7. def __init__(self, callback):
  8. super(Button, self).__init__()
  9.  
  10. self.setText("Hi")
  11.  
  12. #1
  13. self.clicked.connect(lambda: callback())
  14.  
  15. #2
  16. self.callback = callback
  17. self.clicked.connect(self.callback)
  18.  
  19.  
  20. class Example(QObject):
  21. def __init__(self):
  22. super(Example, self).__init__()
  23.  
  24. self.btn = Button(self.callback)
  25.  
  26. def callback(self):
  27. print("callback")
  28.  
  29.  
  30. class Widget(QWidget):
  31. def __init__(self):
  32. super(Widget, self).__init__()
  33.  
  34. ex_ = Example()
  35.  
  36. h_box = QHBoxLayout()
  37.  
  38. h_box.addWidget(ex_.btn)
  39.  
  40. self.setLayout(h_box)
  41.  
  42. self.show()
  43.  
  44.  
  45. if __name__ == "__main__":
  46. import sys
  47.  
  48.  
  49. app = QApplication(sys.argv)
  50. wd = Widget()
  51. sys.exit(app.exec_())
  52.  
  53.  
Runtime error #stdin #stdout #stderr 0.04s 63596KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 1, in <module>
    from PyQt5.QtWidgets import QPushButton, QWidget, QApplication, QHBoxLayout
ImportError: No module named PyQt5