pyqt5 'QWidget' object has no attribute 'setCentralWidget'

2019年02月18日 16:48:28 wardenjohn 阅读数:78
 
版权声明:本博客内容为原创,若要转载,请注明出处!否则禁止转载! https://blog.csdn.net/wardenjohn/article/details/87628891

在我刚使用PyQt5的时候,我运行的时候出现以下报错:

pyqt5 'QWidget' object has no attribute 'setCentralWidget'

这种错误我用了一种方法来解决:

在运行的主函数里面,原本是这样的

  1.  
    if __name__ == '__main__':
  2.  
    app = QtWidgets.QApplication(sys.argv)
  3.  
    widgets = QtWidgets.QWidget()
  4.  
    ui = MainWin()
  5.  
    ui.main_ui.setupUi(widgets)
  6.  
    widgets.show()
  7.  
    ui.run_function()
  8.  
    sys.exit(app.exec_())

然后修改成这样:

  1.  
    if __name__ == '__main__':
  2.  
    app = QtWidgets.QApplication(sys.argv)
  3.  
    widgets = QtWidgets.QMainWindow()
  4.  
    ui = MainWin()
  5.  
    ui.main_ui.setupUi(widgets)
  6.  
    widgets.show()
  7.  
    ui.run_function()
  8.  
    sys.exit(app.exec_())

除了这种方法,Stack Overflow上还有几种不知道可不可行:

1:

  1.  
    class MainWindow(QtWidgets.QMainWindow):
  2.  
    def __init__(self, parent=None):
  3.  
    super(MainWindow, self).__init__(parent=parent)
  4.  
    ui = Ui_MainWindow()
  5.  
    ui.setupUi(self)
  6.  
     
  7.  
     
  8.  
    import sys
  9.  
     
  10.  
    if __name__ == "__main__":
  11.  
    app = QtWidgets.QApplication(sys.argv)
  12.  
    w = MainWindow()
  13.  
    w.show()
  14.  
    sys.exit(app.exec_())

2:

  1.  
    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
  2.  
    def __init__(self, parent=None):
  3.  
    super(MainWindow, self).__init__(parent=parent)
  4.  
    self.setupUi(self)
  5.  
     
  6.  
     
  7.  
     
  8.  
    import sys
  9.  
     
  10.  
    if __name__ == "__main__":
  11.  
    app = QtWidgets.QApplication(sys.argv)
  12.  
    w = MainWindow()
  13.  
    w.show()
  14.  

pyqt5 'QWidget' object has no attribute 'setCentralWidget'(转)的更多相关文章

  1. PyQt程序执行时报错:AttributeError: 'winTest' object has no attribute 'setCentralWidget'的解决方法

    用QtDesigner设计了一个UI界面,保存在文件Ui_wintest.ui中,界面中使用了MainWindow窗口,窗口名字也叫MainWindow,用PyUIC将其转换成了 Ui_wintest ...

  2. pyqt(二) 创建第一个程序(helloworld)解决object has no attribute 'setCentralWidget'

    1.运行Qt Creator QtCreator主界面分为了6个模式:欢迎模式.编辑模式.设计模式.Debug调试模式.项目模式和帮助模式,分别由左侧的6个图标进行切换,对应的快捷键是Ctrl + 数 ...

  3. PyQt学习随笔:自定义信号连接时报AttributeError: 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect'

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 如果使用自定义信号,一定要记得信号是类变量,必须在类中定义,不能在实例 ...

  4. PyQt: “AttributeError: 'Form' object has no attribute 'exec_'” when opening second window

    # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import QApplication , QMainWindow from PyQt5 ...

  5. PyQt(Python+Qt)学习随笔:自定义信号在emit发射信号时报错:AttributeError: object has no attribute

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 如果使用自定义信号,一定要记得信号是类变量,必须在类中定义,不能在实例 ...

  6. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  7. AttributeError: 'list' object has no attribute 'write_pdf'

    我在可视化决策树,运行以下代码时报错:AttributeError: 'list' object has no attribute 'write_pdf' 我使用的是python3.4 from sk ...

  8. AttributeError: '_csv.reader' object has no attribute 'next'

    我在使用pyhon3.4运行以下代码时报错:AttributeError: '_csv.reader' object has no attribute 'next' import csv import ...

  9. attributeError:'module' object has no attribute ** 解决办法

    写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ...

随机推荐

  1. kafka.common.KafkaException: Socket server failed to bind to hdp1:9092: Cannot assign requested address.

    ERROR [KafkaServer id=1] Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.K ...

  2. 一个极好的JavaScript学习网址

    JavaScript学习网址:https://javascript.info/ 网址界面如下,内容和排版都非常不错,内容也比较新,不像一些教程已经是好几年前的了.把这个教程浏览一遍,能够解答很多看代码 ...

  3. 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法

    在计算loss的时候,最常见的一句话就是 tf.nn.softmax_cross_entropy_with_logits ,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化 ...

  4. centOS6.4 extundelete工具恢复rm -rf 删除的目录[转]

    原文:http://www.cnblogs.com/patf/p/3368765.html PS:补充下,我在fedora 19上运行的时候遇到的一个问题: 1 [root@localhost ext ...

  5. WinEdt 和 Sumatra 双向关联设置

    (1)配置PDF Viewer,在菜单栏选Options -> Execution Modes ->PDF Viewer ->点击右侧的"Browse"按钮,在弹 ...

  6. 实现开发板与ubuntu的共享--根文件系统NFS--Samba共享【sky原创】

    虚拟机要选择桥接,并且禁用有线和无线网卡,开启本地连接,本地连接属性要写如下:     ip地址是在连上板子后,windows   cmd  下  ipconfig得出的 板子的网线最好连接交换机或者 ...

  7. 董事局主席董事长总裁首席执行官CEO总裁董事监事区别

    董事长是公司的最大股东:董事长是董事会的主席,一般是企业的所有者,掌握企业的股权并且决定企业的发展策略. 董事局主席通常是在大财团中才会出现,董事局主席管数个董事长,一个大财团涉及很多方面的业务,因此 ...

  8. dubbo系列一、dubbo背景介绍、微服务拆分

    一.背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 二.传统应用到分布式应用的演进过程 ...

  9. java中集合的组成及特点

    1:集合 Collection(单列集合) List(有序,可重复) ArrayList 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector 底层数据结构是数组,查询快,增删慢 线程 ...

  10. java 基础(转自索宁)

    一.方法函数 函数也称为方法,就是定义在类中的具有特定功能的一段独立代码.用于定义功能,提高代码的复用性. 函数的特点1> 定义函数可以将功能代码进行封装,便于对该功能进行复用:2> 函数 ...