Qt窗口继承

写一个自定义类继承QWidget

  1. from PyQt5.QtWidgets import *
  2. from PyQt5.QtCore import *
  3. from PyQt5.QtGui import *
  4. import sys
  5. class MyWindow(QWidget):
  6. def __init__(self, title):
  7. super().__init__()
  8. self.setWindowTitle(title)
  9. if __name__ == '__main__':
  10. app = QApplication(sys.argv)
  11. window = MyWindow("窗口标题")
  12. window.show()
  13. sys.exit(app.exec_())

通过继承QWidget来实现窗体
在构造中,必须调用super函数,否则将出行错误

初始化ui

  1. from PyQt5.QtWidgets import *
  2. from PyQt5.QtCore import *
  3. from PyQt5.QtGui import *
  4. import sys
  5. class MyWindow(QWidget):
  6. def __init__(self, title):
  7. super().__init__()
  8. self.setWindowTitle(title)
  9. self.init_ui()
  10. def init_ui(self):
  11. layout = QHBoxLayout()
  12. # ---------------------------------
  13. # 在这里初始化界面内容
  14. # ---------------------------------
  15. self.setLayout(layout)
  16. if __name__ == '__main__':
  17. app = QApplication(sys.argv)
  18. window = MyWindow("窗口标题")
  19. window.show()
  20. sys.exit(app.exec_())