Python 将小部件添加到从.ui文件创建的布局中

Python 将小部件添加到从.ui文件创建的布局中,python,pyside2,Python,Pyside2,我正在使用PySide2在Python中创建GUI,这是我的.ui文件: <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>main_window</class> <widget class="QWidget" name="main_window">

我正在使用PySide2在Python中创建GUI,这是我的.ui文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>main_window</class>
 <widget class="QWidget" name="main_window">
  <property name="windowModality">
   <enum>Qt::NonModal</enum>
  </property>
  <property name="enabled">
   <bool>true</bool>
  </property>
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1000</width>
    <height>640</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Testing App</string>
  </property>
  <layout class="QVBoxLayout" name="layout">
   <item>
    <layout class="QVBoxLayout" name="other_layout">
     <item>
      <widget class="QWidget" name="panel" native="true">
       <layout class="QHBoxLayout" name="layout3">
        <item>
         <layout class="QVBoxLayout" name="layout4">
          <item>
           <layout class="QHBoxLayout" name="layout5">
            <item>
             <widget class="QLabel" name="lo">
              <property name="text">
               <string>X</string>
              </property>
              <property name="alignment">
               <set>Qt::AlignCenter</set>
              </property>
             </widget>
            </item>
            <item>
             <widget class="QLineEdit" name="resolution_x">
              <property name="sizePolicy">
               <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
                <horstretch>0</horstretch>
                <verstretch>0</verstretch>
               </sizepolicy>
              </property>
              <property name="inputMethodHints">
               <set>Qt::ImhDigitsOnly</set>
              </property>
              <property name="text">
               <string>175</string>
              </property>
             </widget>
            </item>
           </layout>
          </item>
          <item>
           <layout class="QHBoxLayout" name="layout6">
            <item>
             <widget class="QLabel" name="lo2">
              <property name="text">
               <string>Y</string>
              </property>
              <property name="alignment">
               <set>Qt::AlignCenter</set>
              </property>
             </widget>
            </item>
            <item>
             <widget class="QLineEdit" name="res2">
              <property name="sizePolicy">
               <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
                <horstretch>0</horstretch>
                <verstretch>0</verstretch>
               </sizepolicy>
              </property>
              <property name="inputMethodHints">
               <set>Qt::ImhDigitsOnly</set>
              </property>
              <property name="text">
               <string>150</string>
              </property>
             </widget>
            </item>
           </layout>
          </item>
         </layout>
        </item>
        <item>
         <layout class="QVBoxLayout" name="layout7">
          <item>
           <layout class="QHBoxLayout" name="iterations_layout">
            <item>
             <widget class="QLabel" name="iterLab">
              <property name="text">
               <string>Num Passes</string>
              </property>
              <property name="alignment">
               <set>Qt::AlignCenter</set>
              </property>
             </widget>
            </item>
            <item>
             <widget class="QLineEdit" name="itr">
              <property name="sizePolicy">
               <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
                <horstretch>0</horstretch>
                <verstretch>0</verstretch>
               </sizepolicy>
              </property>
              <property name="inputMethodHints">
               <set>Qt::ImhDigitsOnly</set>
              </property>
              <property name="text">
               <string>20</string>
              </property>
             </widget>
            </item>
           </layout>
          </item>
          <item>
           <layout class="QHBoxLayout" name="layout8">
            <item>
             <widget class="QPushButton" name="reset_button">
              <property name="sizePolicy">
               <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
                <horstretch>0</horstretch>
                <verstretch>0</verstretch>
               </sizepolicy>
              </property>
              <property name="minimumSize">
               <size>
                <width>50</width>
                <height>0</height>
               </size>
              </property>
              <property name="layoutDirection">
               <enum>Qt::LeftToRight</enum>
              </property>
              <property name="text">
               <string>Zoom</string>
              </property>
             </widget>
            </item>
           </layout>
          </item>
         </layout>
        </item>
       </layout>
      </widget>
     </item>
     <item>
      <layout class="QHBoxLayout" name="process_layout">
       <item alignment="Qt::AlignLeft">
        <widget class="QLabel" name="status">
         <property name="sizePolicy">
          <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
           <horstretch>0</horstretch>
           <verstretch>0</verstretch>
          </sizepolicy>
         </property>
         <property name="minimumSize">
          <size>
           <width>120</width>
           <height>20</height>
          </size>
         </property>
         <property name="maximumSize">
          <size>
           <width>120</width>
           <height>20</height>
          </size>
         </property>
         <property name="text">
          <string/>
         </property>
         <property name="alignment">
          <set>Qt::AlignCenter</set>
         </property>
        </widget>
       </item>
       <item alignment="Qt::AlignRight">
        <widget class="QLabel" name="procLab">
         <property name="minimumSize">
          <size>
           <width>77</width>
           <height>20</height>
          </size>
         </property>
         <property name="maximumSize">
          <size>
           <width>77</width>
           <height>20</height>
          </size>
         </property>
         <property name="text">
          <string>#</string>
         </property>
        </widget>
       </item>
       <item alignment="Qt::AlignRight">
        <widget class="QLineEdit" name="processes">
         <property name="sizePolicy">
          <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
           <horstretch>0</horstretch>
           <verstretch>0</verstretch>
          </sizepolicy>
         </property>
         <property name="minimumSize">
          <size>
           <width>50</width>
           <height>0</height>
          </size>
         </property>
         <property name="maximumSize">
          <size>
           <width>50</width>
           <height>2000</height>
          </size>
         </property>
         <property name="inputMethodHints">
          <set>Qt::ImhDigitsOnly</set>
         </property>
         <property name="text">
          <string>8</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>
但是,我得到了以下错误

    self.layout.addWidget(QLabel('Stuff'))
AttributeError: 'builtin_function_or_method' object has no attribute 'addWidget'

我不明白为什么self.layout没有addWidget属性。我以为self.layout会引用QVBoxLayout对象?

您有几个错误:

  • 从.ui加载的小部件不是MyWindow,另一方面,在PySide2中,不可能将.ui加载到由python实现的小部件类中(在PyQt5中,如果可能,可以通过
    uic.loadUi()
    ),因此该类应该处理小部件

  • 另一方面,不要使用可能与方法名称冲突的变量名称,例如QWidget类有一个布局方法,因此不建议您使用该名称的属性,一个可能的解决方案是使用findChild来获取布局:

导入系统 从PySide2.QtCore导入QObject,QFile 从PySide2.qtwidts导入QApplication、QLabel、QVBoxLayout、QWidget 从PySide2.QtUiTools导入QUiLoader 班级经理(QObject): 定义初始化(自): 超级(经理,自我)。\uuuuu初始化\uuuuuuu(无) ui\u file=QFile(“./myui.ui”) ui_file.open(QFile.ReadOnly) 加载器=QUiLoader() self.window=loader.load(ui\U文件) ui_file.close() lay=self.window.findChild(QVBoxLayout,“布局”) lay.addWidget(QLabel(“Stuff”)) self.window.show() 如果名称=“\uuuuu main\uuuuuuuu”: app=QApplication(sys.argv) f=经理() sys.exit(app.exec_())
  • 另一种可能的解决方案是将布局的名称更改为例如vlayout:
导入系统 从PySide2.QtCore导入QObject,QFile 从PySide2.qtwidts导入QApplication、QLabel、QWidget 从PySide2.QtUiTools导入QUiLoader 班级经理(QObject): 定义初始化(自): 超级(经理,自我)。\uuuuu初始化\uuuuuuu(无) ui\u file=QFile(“./myui.ui”) ui_file.open(QFile.ReadOnly) 加载器=QUiLoader() self.window=loader.load(ui\U文件) ui_file.close() self.window.vlayout.addWidget(QLabel(“Stuff”)) self.window.show() 如果名称=“\uuuuu main\uuuuuuuu”: app=QApplication(sys.argv) f=经理() sys.exit(app.exec_())
添加了更多的.uifile@eyllanesc加上
    self.layout.addWidget(QLabel('Stuff'))
AttributeError: 'builtin_function_or_method' object has no attribute 'addWidget'