DELMAR_ 2022-11-03 06:37 采纳率: 53.3%
浏览 61
已结题

pyqt调用继承类无法显示

问题遇到的现象和发生背景

我写出这样的代码,用Window类去调用MyWidget,MyWidget不显示,求解

img

运行结果及报错内容

MyWidget无法显示

img

  • 写回答

6条回答 默认 最新

  • 游一游走一走 2022-11-03 11:39
    关注
    1. main你应该初始化Window
    2. 你的MyWidget需要设置一个属性'Qt.WA_StyledBackground'
    import sys
    from PyQt5.QtCore import Qt, QRect
    from PyQt5.QtGui import QPainter
    from PyQt5.QtWidgets import QApplication, QWidget
    
    
    class MyWidget(QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.resize(300, 300)
            self.move(100, 100)
            self.setAttribute(Qt.WA_StyledBackground, True)
            self.setStyleSheet("background-color:yellow;")
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.resize(500, 500)
            self.move(200, 200)
            self.setStyleSheet("background-color:blue;")
            q_widget = MyWidget(self)
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = Window()
        w.show()
        sys.exit(app.exec())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 11月11日
  • 已采纳回答 11月3日
  • 创建了问题 11月3日