Qt窗口继承
写一个自定义类继承QWidget
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
class MyWindow(QWidget):
def __init__(self, title):
super().__init__()
self.setWindowTitle(title)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow("窗口标题")
window.show()
sys.exit(app.exec_())
通过继承QWidget
来实现窗体
在构造中,必须调用super
函数,否则将出行错误
初始化ui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
class MyWindow(QWidget):
def __init__(self, title):
super().__init__()
self.setWindowTitle(title)
self.init_ui()
def init_ui(self):
layout = QHBoxLayout()
# ---------------------------------
# 在这里初始化界面内容
# ---------------------------------
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow("窗口标题")
window.show()
sys.exit(app.exec_())