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. Broadcast的类型

    两种发送方法 1.无序广播 对于多个接收者来说是完全异步的,通常每个接收者都无需等待即可以接收到广播,接收者相互之间不会有影响.对于这种广播,接收者无法终止广播,即无法阻止其他接收者的 接收动作. 消 ...

  2. oracle里的tns是什么意思

    参考http://www.jb51.net/article/44668.htm TNS简要介绍与应用 Oracle中TNS的完整定义:transparence Network Substrate透明网 ...

  3. 蒙特卡罗定位(Particle Filter Localization)笔记

    善始善终,这篇文章是Coursera课程Robotics: Estimation and Learning最后一周的课程总结.里面的小哥讲得不是很清晰,留下的作业很花功夫(第二周课程也是酱紫). 这周 ...

  4. Linux命令学习总结:date命令【转】

    本文转自:http://www.cnblogs.com/kerrycode/p/3427617.html 命令简介: date 根据给定格式显示日期或设置系统日期时间.print or set the ...

  5. 【转】C++ map的基本操作和使用

    1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响较小,除了那个操作节点,对其它的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...

  6. unbuntu 16.04.2 安装 Eclipse C++开发环境

    1.安装JAVA (1)首先添加源: sudo gedit /etc/apt/sources.list 在打开的文件中添加如下内容并保存: deb http://ppa.launchpad.net/w ...

  7. win10 安装硕正

    提示权限不够,解决方法:根据提示路径手动在路径下建立文件夹

  8. 反向代理负载均衡之APACHE

    反向代理负载均衡之APACHE 一.反向代理1.1 介绍反响代理 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将 ...

  9. 使用apache和nginx代理实现tomcat负载均衡及集群配置详解

    实验环境: 1.nginx的代理功能 nginx proxy: eth0: 192.168.8.48 vmnet2 eth1: 192.168.10.10 tomcat server1: vmnet2 ...

  10. chart学习

    效果图: 目录信息 graphic.jsp <%@ page language="java" contentType="text/html; charset=UTF ...