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. luogu P2511 [HAOI2008]木棍分割

    传送门 第一问是一道经典的二分,二分答案\(ans\),然后从前往后扫,判断要分成几段救星了 第二问设\(f_{i,j}\)表示前\(i\)个数分成\(j\)段,每段之和不超过第一问答案的方案,转移就 ...

  2. E - 着色方案 HYSBZ - 1079 (计数DP)

    题目链接:https://cn.vjudge.net/contest/281963#problem/E 题目大意:中文题目 具体思路:这个题脑洞有点大,因为ci的数据量非常小,所以我们可以根据这个来进 ...

  3. Android 中查看内存的使用情况集常用adb命令

    http://blog.csdn.net/bigconvience/article/details/35553983 http://blog.csdn.net/duantihi/article/det ...

  4. shiroWeb项目-授权(十一)

    使用PermissionsAuthorizationFilter 在applicationContext-shiro.xml中配置url所对应的权限. 测试流程: 1.在applicationCont ...

  5. Levmar 配置

    Levmar配置 原文有些错误,在我的博客里已经改好了:http://blog.sina.com.cn/s/blog_45b747f70101he1t.html 如果6或者7自由度机器人的运动学逆解无 ...

  6. C# ASP.NET MVC 配置允许跨域访问

    在web.config文件中的 system.webServer 节点下 增加如下配置 <httpProtocol> <customHeaders> <add name= ...

  7. Check Box、Radio Button、Combo Box控件使用

    Check Box.Radio Button.Combo Box控件使用 使用控件的方法 1.拖动控件到对话框 2. 定义控件对应的变量(值变量或者控件变量) 3.响应控件各种消息 Check Box ...

  8. Linux下锁定账号,禁止登录系统的设置总结【转】

    在我们运维工作中,会经常要求一些用户不允许登陆系统,以加固系统安全.今天这里介绍下锁定账号登陆的几种方法: (推荐使用)这种方式会更加人性化一点,因为不仅可以禁止用户登录,还可以在禁用登陆时给提示告诉 ...

  9. windows使用python原生组件包获取系统日志信息

    #coding=utf8 import sys import traceback import win32con import win32evtlog import win32evtlogutil i ...

  10. Python创建、删除桌面、启动组快捷方式的例子分享

    一.Python创桌面建快捷方式的2个例子 例子一: 代码如下: import osimport pythoncomfrom win32com.shell import shell    from w ...