pyqt上的setWidget属性错误

问题描述 投票:0回答:1

我从python3 + pyqt5开始,方法是做一个非常愚蠢的.ui,名为invest.ui,它具有单个按钮“ ok_button”,单击该按钮即可关闭界面。但是,当我运行以下代码时,我无法使接口运行:

import sys
import time
import datetime
import random
from PyQt5.QtWidgets import *
from PyQt5 import uic

Ui_MainWindow, QtBaseClass = uic.loadUiType("invest.ui")

class MyApp(QMainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.ok_button.clicked.connect(self.close)

    def close(self):
        sys.exit(app.exec_())

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

我收到错误:

Traceback (most recent call last):
  File "invest.py", line 24, in <module>
    window = MyApp()
  File "invest.py", line 16, in __init__
    self.ui.setupUi(self)
  File "<string>", line 37, in setupUi
AttributeError: 'MyApp' object has no attribute 'setWidget'

我该如何解决,必须声明SetWidget属性?

python-3.x pyqt
1个回答
0
投票

在您引用的示例中,没有attribute 'setWidget',因此,我没有这样的错误。

但是有一个错误:QCoreApplication::exec: The event loop is already running

QWidget :: close()

关闭此小部件。如果小部件已关闭,则返回true;否则返回false。

尝试:

main.py

import sys
#import time
#import datetime
#import random
from PyQt5.QtWidgets import *
from PyQt5 import uic

Ui_MainWindow, QtBaseClass = uic.loadUiType("invest.ui")

class MyApp(QMainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # try it:        
        # self.ui.ok_button.clicked.connect(self.close)
        # or 
        self.ui.ok_button.clicked.connect(self.myClose)

#    def close(self):
#        sys.exit(app.exec_())

    def myClose(self):
        self.close()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

invest.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>320</width>
    <height>240</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="1">
     <spacer name="verticalSpacer_2">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>20</width>
        <height>31</height>
       </size>
      </property>
     </spacer>
    </item>
    <item row="1" column="0">
     <spacer name="horizontalSpacer">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>92</width>
        <height>20</height>
       </size>
      </property>
     </spacer>
    </item>
    <item row="1" column="1">
     <widget class="QPushButton" name="ok_button">
      <property name="minimumSize">
       <size>
        <width>100</width>
        <height>100</height>
       </size>
      </property>
      <property name="text">
       <string>Click me</string>
      </property>
     </widget>
    </item>
    <item row="1" column="2">
     <spacer name="horizontalSpacer_2">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>92</width>
        <height>20</height>
       </size>
      </property>
     </spacer>
    </item>
    <item row="2" column="1">
     <spacer name="verticalSpacer">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>20</width>
        <height>32</height>
       </size>
      </property>
     </spacer>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>320</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
© www.soinside.com 2019 - 2024. All rights reserved.