Python 3.x Python3 PyQT(使用设计器),启动时出现问题

Python 3.x Python3 PyQT(使用设计器),启动时出现问题,python-3.x,pyqt4,qt-designer,Python 3.x,Pyqt4,Qt Designer,我正在尝试开发一个带有GUI的程序,因为我非常喜欢KDE,所以我想我会尝试一下PyQT。这里有一个我到目前为止所拥有的示例(还没有逻辑,我想看看是否可以让UI显示出来) 这是名为at.UI的UI文件 <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="Mai

我正在尝试开发一个带有GUI的程序,因为我非常喜欢KDE,所以我想我会尝试一下PyQT。这里有一个我到目前为止所拥有的示例(还没有逻辑,我想看看是否可以让UI显示出来)

这是名为at.UI的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>410</width>
    <height>322</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QTabWidget" name="tabWidget">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>10</y>
      <width>261</width>
      <height>271</height>
     </rect>
    </property>
    <property name="tabPosition">
     <enum>QTabWidget::North</enum>
    </property>
    <property name="tabShape">
     <enum>QTabWidget::Rounded</enum>
    </property>
    <property name="currentIndex">
     <number>2</number>
    </property>
    <property name="tabsClosable">
     <bool>false</bool>
    </property>
    <widget class="QWidget" name="tab">
     <attribute name="title">
      <string>Information</string>
     </attribute>
     <widget class="QWidget" name="formLayoutWidget">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>251</width>
        <height>111</height>
       </rect>
      </property>
      <layout class="QFormLayout" name="formLayout">
       <item row="0" column="0">
        <widget class="QLabel" name="jobNameLabel">
         <property name="text">
          <string>Job Name</string>
         </property>
        </widget>
       </item>
       <item row="0" column="1">
        <widget class="QLineEdit" name="jobNameLineEdit"/>
       </item>
       <item row="1" column="0">
        <widget class="QLabel" name="inputDirectoryLabel">
         <property name="text">
          <string>Input Directory</string>
         </property>
        </widget>
       </item>
       <item row="1" column="1">
        <widget class="QLineEdit" name="inputDirectoryLineEdit"/>
       </item>
       <item row="2" column="0">
        <widget class="QLabel" name="albumNameLabel">
         <property name="text">
          <string>Album Name</string>
         </property>
        </widget>
       </item>
       <item row="2" column="1">
        <widget class="QLineEdit" name="albumNameLineEdit"/>
       </item>
       <item row="3" column="0">
        <widget class="QLabel" name="scalingAmountLabel">
         <property name="text">
          <string>Scaling Amount</string>
         </property>
        </widget>
       </item>
       <item row="3" column="1">
        <widget class="QComboBox" name="scalingAmountComboBox">
         <property name="sizePolicy">
          <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
           <horstretch>0</horstretch>
           <verstretch>0</verstretch>
          </sizepolicy>
         </property>
         <item>
          <property name="text">
           <string>10% of Original</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>20% of Original</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>30% of Original</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>40% of Original</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>50% of Original</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>60% of Original</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>70% of Original</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>80% of Original</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>90% of Original</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>100% of Original</string>
          </property>
         </item>
        </widget>
       </item>
      </layout>
     </widget>
     <widget class="QTextEdit" name="textEdit">
      <property name="geometry">
       <rect>
        <x>3</x>
        <y>130</y>
        <width>251</width>
        <height>111</height>
       </rect>
      </property>
     </widget>
     <widget class="QLabel" name="label_3">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>110</y>
        <width>53</width>
        <height>14</height>
       </rect>
      </property>
      <property name="text">
       <string>Notes</string>
      </property>
     </widget>
    </widget>
    <widget class="QWidget" name="tab_2">
     <attribute name="title">
      <string>Manual</string>
     </attribute>
     <widget class="QPushButton" name="pushButton_5">
      <property name="geometry">
       <rect>
        <x>30</x>
        <y>120</y>
        <width>181</width>
        <height>25</height>
       </rect>
      </property>
      <property name="text">
       <string>Step 1 (Copy Images)</string>
      </property>
     </widget>
     <widget class="QPushButton" name="pushButton_4">
      <property name="geometry">
       <rect>
        <x>30</x>
        <y>150</y>
        <width>181</width>
        <height>25</height>
       </rect>
      </property>
      <property name="text">
       <string>Step 2 (Resize Images)</string>
      </property>
     </widget>
     <widget class="QPushButton" name="pushButton_3">
      <property name="geometry">
       <rect>
        <x>30</x>
        <y>180</y>
        <width>181</width>
        <height>25</height>
       </rect>
      </property>
      <property name="text">
       <string>Step 3 (Upload Images)</string>
      </property>
     </widget>
     <widget class="QPushButton" name="pushButton_2">
      <property name="geometry">
       <rect>
        <x>30</x>
        <y>210</y>
        <width>181</width>
        <height>25</height>
       </rect>
      </property>
      <property name="text">
       <string>Step 4 (Manage Album)</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_2">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>10</y>
        <width>241</width>
        <height>61</height>
       </rect>
      </property>
      <property name="frameShape">
       <enum>QFrame::NoFrame</enum>
      </property>
      <property name="text">
       <string>This is the manual method of uploading your images.  Complete all four steps and you'll be done!</string>
      </property>
      <property name="wordWrap">
       <bool>true</bool>
      </property>
     </widget>
    </widget>
    <widget class="QWidget" name="tab_3">
     <attribute name="title">
      <string>Automatic</string>
     </attribute>
     <widget class="QPushButton" name="pushButton_6">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>170</y>
        <width>231</width>
        <height>61</height>
       </rect>
      </property>
      <property name="text">
       <string>Express Mode</string>
      </property>
     </widget>
     <widget class="QLabel" name="label">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>10</y>
        <width>241</width>
        <height>61</height>
       </rect>
      </property>
      <property name="frameShape">
       <enum>QFrame::NoFrame</enum>
      </property>
      <property name="text">
       <string>This is the automatic method of uploading your images.  Clicking the 'Express Mode' button will finish the entire process for you.</string>
      </property>
      <property name="wordWrap">
       <bool>true</bool>
      </property>
     </widget>
    </widget>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>270</x>
      <y>250</y>
      <width>131</width>
      <height>25</height>
     </rect>
    </property>
    <property name="text">
     <string>Exit</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_4">
    <property name="geometry">
     <rect>
      <x>270</x>
      <y>0</y>
      <width>131</width>
      <height>81</height>
     </rect>
    </property>
    <property name="text">
     <string>First, fill out the fields in the Information tab.  Next, choose either Manual or Automatic processing.</string>
    </property>
    <property name="wordWrap">
     <bool>true</bool>
    </property>
   </widget>
   <widget class="QLabel" name="label_5">
    <property name="geometry">
     <rect>
      <x>270</x>
      <y>80</y>
      <width>131</width>
      <height>101</height>
     </rect>
    </property>
    <property name="text">
     <string>There are additional tools and options available in the menubar.  If you need any assistance, check out the Help menu for more information!</string>
    </property>
    <property name="wordWrap">
     <bool>true</bool>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_7">
    <property name="geometry">
     <rect>
      <x>270</x>
      <y>220</y>
      <width>131</width>
      <height>25</height>
     </rect>
    </property>
    <property name="text">
     <string>Export</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_8">
    <property name="geometry">
     <rect>
      <x>270</x>
      <y>190</y>
      <width>131</width>
      <height>25</height>
     </rect>
    </property>
    <property name="text">
     <string>Import</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>410</width>
     <height>21</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuFileFile">
    <property name="title">
     <string>File</string>
    </property>
   </widget>
   <widget class="QMenu" name="menuTools">
    <property name="title">
     <string>Tools</string>
    </property>
   </widget>
   <widget class="QMenu" name="menuHelp">
    <property name="title">
     <string>Help</string>
    </property>
   </widget>
   <addaction name="menuFileFile"/>
   <addaction name="menuTools"/>
   <addaction name="menuHelp"/>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
启动它的Python脚本是:

#!/usr/bin/python3

from PyQt4 import QtGui, QtCore
import at_auto, os

class at(at_auto):
    def __init__(self):
        at_auto.__init__(self)

if __name__ == '__main__':
    import sys
    a = QtGui.QApplication(sys.argv)
    QtCore.QObject.connect(a, QtCore.SIGNAL('lastWindowClosed()'), a, QtCore.SLOT('quit()'))
    w = at(at_auto.__init__(self))
    a.setMainWindow(w)
    w.show()
    a.exec_loop()
尝试运行该程序时,出现以下错误:

Traceback (most recent call last):
  File "./at.py", line 6, in <module>
    class at(at_auto):
TypeError: module.__init__() takes at most 2 arguments (3 given)
回溯(最近一次呼叫最后一次):
文件“/at.py”,第6行,在
上课时间(自动):
TypeError:module.\uuuuu init\uuuuuu()最多接受2个参数(给定3个)
我从以下教程中修改了at.py中的代码:


非常感谢你

首先:你链接到的教程已经过时了(它与PyQt3而不是PyQt4相关),所以我建议你看看更新的教程

但为了解决手头的问题,如果您使用以下命令重新编译ui,则会在某种程度上简化事情:

pyuic4 -w at.ui > at_auto.py
-w
选项将生成一个比默认创建的GUI类更直观的附加GUI类。该类的名称将与在Qt Designer中创建的顶级对象具有相同的名称-在您的情况下,这将是
MainWindow

要使用它,只需将它导入到
main
脚本中,创建一个子类,然后
show
它的一个实例,如下所示:

from PyQt4 import QtGui, QtCore
from at_auto import MainWindow

class Window(MainWindow):
    def __init__(self):
        MainWindow.__init__(self)
        # do other initialization stuff here...

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

谢谢你的回答。这无疑消除了令人讨厌的错误信息。现在我只需要弄清楚如何正确初始化程序,因为它似乎刚刚开始和结束。朝正确的方向走!我添加了打印语句来查看到底发生了什么,现在它正确地打开了窗口。奇怪。
from PyQt4 import QtGui, QtCore
from at_auto import MainWindow

class Window(MainWindow):
    def __init__(self):
        MainWindow.__init__(self)
        # do other initialization stuff here...

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())