MyApp_window.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Only needed for access to command line arguments
  2. import sys
  3. from PyQt5.QtCore import *
  4. from PyQt5.QtWidgets import *
  5. # 自定义窗口,继承 QMainWindow
  6. class MainWindow(QMainWindow):
  7. def __init__(self, *args, **kwargs):
  8. super(MainWindow, self).__init__(*args, **kwargs)
  9. self.windowTitleChanged().connect(self.onWindowTitleChanged)
  10. self.windowTitleChanged().connect(lambda x: self.my_custom_fn())
  11. self.windowTitleChanged().connect(lambda x: self.my_custom_fn(x))
  12. self.windowTitleChanged().connect(lambda x: self.my_custom_fn(x, 25))
  13. self.setWindowTitle("My Awesome App") # 标题
  14. label = QLabel("THIS IS AWESOME!!!")
  15. label.setAlignment(Qt.AlignCenter)
  16. self.setCentralWidget(label)
  17. def contextMenuEvent(self, event):
  18. print("Context menu event!")
  19. super(MainWindow, self).contextMenuEvent(event)
  20. def onWindowTitleChanged(self, s):
  21. print(s)
  22. def my_custom_fn(self, a="HELLO!", b=5):
  23. print(a, b)
  24. if __name__ == '__main__':
  25. app = QApplication(sys.argv)
  26. window = MainWindow()
  27. window.show() # 默认下 windows 是隐藏的
  28. # 开启事件循环
  29. app.exec_()