PyQT-AttributeError:“ MyApp”对象没有属性“ TabWidget”

时间:2018-08-20 13:34:44

标签: python pyqt pyqt4 qt-designer

我将PyQT4与QT Designer一起使用。我在设计器中创建了QTabWidget,我想将选项卡选择与需要执行的某些操作联系起来。 我的QTabWidget的对象名称是默认的-TabWidget。 这是我剪切的python代码:

import sys
from PyQt4 import QtCore, QtGui, uic

class MyApp(QtGui.QTabWidget):
    def __init__(self):
        super(MyApp,self).__init__()
        uic.loadUi('k2400.ui',self)
        self.show()


    ################### SELECT TAB ##########
        self.TabWidget.currentChanged.connect(self.setup_settings)

    def setup_settings(self):
        if self.TabWidget.currentIndex() == 1:
            do something
        elif self.TabWidget.currentIndex() == 2:
            do something else

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()

sys.exit(app.exec_())
end_command = raw_input("Press any key to exit")

但是我收到错误消息

AttributeError: 'MyApp' object has no attribute 'TabWidget'

我不知道出了什么问题,因为当我连接其他内容(例如纯文本编辑)时,它就很好了。

这是我的k2400.ui文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>TabWidget</class>
 <widget class="QTabWidget" name="TabWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>781</width>
    <height>576</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Keithley 2400</string>
  </property>
  <property name="currentIndex">
   <number>1</number>
  </property>
  <property name="tabsClosable">
   <bool>false</bool>
  </property>
  <widget class="QWidget" name="General">
   <attribute name="title">
    <string>General</string>
   </attribute>
  </widget>
  <widget class="QWidget" name="Charge">
   <attribute name="title">
    <string>Charge</string>
   </attribute>
   <widget class="QLabel" name="label_FirstStageCharge">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>40</y>
      <width>131</width>
      <height>51</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>12</pointsize>
     </font>
    </property>
    <property name="text">
     <string>First Stage Charge</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_StartVoltage">
    <property name="geometry">
     <rect>
      <x>270</x>
      <y>80</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Start Voltage</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_FinishVoltage">
    <property name="geometry">
     <rect>
      <x>270</x>
      <y>120</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Finish Voltage</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_ChargeCurrent">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>90</y>
      <width>81</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Charge current</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_ChargeCurrent_2">
    <property name="geometry">
     <rect>
      <x>270</x>
      <y>160</y>
      <width>161</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Charge current </string>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="plainTextEdit_StartVoltage">
    <property name="geometry">
     <rect>
      <x>360</x>
      <y>80</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="plainTextEdit_FinishVoltage">
    <property name="geometry">
     <rect>
      <x>360</x>
      <y>120</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="plainTextEdit_ChargeCurrent">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>90</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
    <property name="sizePolicy">
     <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
      <horstretch>0</horstretch>
      <verstretch>0</verstretch>
     </sizepolicy>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="plainTextEdit_ChargeCurrent2">
    <property name="geometry">
     <rect>
      <x>360</x>
      <y>160</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_SecondStageCharge">
    <property name="geometry">
     <rect>
      <x>290</x>
      <y>40</y>
      <width>161</width>
      <height>51</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>12</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Second Stage Charge</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_START">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>360</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>START</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_STOP">
    <property name="geometry">
     <rect>
      <x>180</x>
      <y>360</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>STOP</string>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox_Saturation">
    <property name="geometry">
     <rect>
      <x>310</x>
      <y>230</y>
      <width>70</width>
      <height>17</height>
     </rect>
    </property>
    <property name="text">
     <string>Saturation</string>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="plainTextEdit_filename_charge">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>290</y>
      <width>171</width>
      <height>31</height>
     </rect>
    </property>
    <property name="plainText">
     <string>filename</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_FinishVoltage_2">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>130</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Finish Voltage</string>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="plainTextEdit_FinishVoltage_2">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>130</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit_ChargeCurrent">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>180</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QWidget" name="Discharge">
   <attribute name="title">
    <string>Discharge</string>
   </attribute>
   <widget class="QPlainTextEdit" name="plainTextEdit_discharge_VoltageCompliance">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>40</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_8">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>50</y>
      <width>91</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>Voltage compliance</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_9">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>140</y>
      <width>91</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>Discharge current</string>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="plainTextEdit_discharge_current">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>130</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_5">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>230</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>START</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_3">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>230</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>STOP</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_10">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>190</y>
      <width>47</width>
      <height>13</height>
     </rect>
    </property>
    <property name="text">
     <string>Filename</string>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="plainTextEdit_discharge_filename">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>180</y>
      <width>171</width>
      <height>31</height>
     </rect>
    </property>
    <property name="plainText">
     <string>filename</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_11">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>90</y>
      <width>91</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>Finish voltage</string>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="plainTextEdit_discharge_finishVoltage">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>80</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QWidget" name="Sweep">
   <attribute name="title">
    <string>Sweep</string>
   </attribute>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:0)

正如您指出的那样,您的课程是QTabWidget,因此要访问主要元素,您必须使用self,而不应该使用self.TabWidget

import sys
from PyQt4 import QtCore, QtGui, uic

class MyApp(QtGui.QTabWidget):
    def __init__(self):
        super(MyApp,self).__init__()
        uic.loadUi('k2400.ui',self)
        self.show()

    ################### SELECT TAB ##########
        self.currentChanged.connect(self.setup_settings)

    @QtCore.pyqtSlot(int)
    def setup_settings(self, index):
        if index == 0:
            print("0")
        elif index == 1:
            print("1")
        else:
            print(index)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()

    sys.exit(app.exec_())