2015-05-14 18 views
1

我得到一个错误,当我尝试运行这段代码:AttributeError的:“QSlider”对象有没有属性“subtractStep”

# -*- coding: utf-8 -*- 

# Form implementation generated from reading ui file 'scaler_gui_3.ui' 
# 
# Created: Thu May 14 13:05:28 2015 
#  by: PyQt4 UI code generator 4.11.3 
# 
# WARNING! All changes made in this file will be lost! 

from PyQt4 import QtCore, QtGui 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    def _fromUtf8(s): 
     return s 

try: 
    _encoding = QtGui.QApplication.UnicodeUTF8 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig, _encoding) 
except AttributeError: 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig) 

class Ui_Form(object): 
    def setupUi(self, Form): 
     Form.setObjectName(_fromUtf8("Form")) 
     Form.resize(270, 219) 
     self.gridLayout_2 = QtGui.QGridLayout(Form) 
     self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2")) 
     self.horizontalSliderFrames = QtGui.QSlider(Form) 
     self.horizontalSliderFrames.setOrientation(QtCore.Qt.Horizontal) 
     self.horizontalSliderFrames.setObjectName(_fromUtf8("horizontalSliderFrames")) 
     self.gridLayout_2.addWidget(self.horizontalSliderFrames, 1, 0, 1, 1) 
     self.gridLayout = QtGui.QGridLayout() 
     self.gridLayout.setObjectName(_fromUtf8("gridLayout")) 
     self.verticalScrollBarIW = QtGui.QScrollBar(Form) 
     self.verticalScrollBarIW.setOrientation(QtCore.Qt.Vertical) 
     self.verticalScrollBarIW.setObjectName(_fromUtf8("verticalScrollBarIW")) 
     self.gridLayout.addWidget(self.verticalScrollBarIW, 0, 2, 1, 1) 
     self.verticalLayout_2 = QtGui.QVBoxLayout() 
     self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2")) 
     self.labelMain = QtGui.QLabel(Form) 
     self.labelMain.setObjectName(_fromUtf8("labelMain")) 
     self.verticalLayout_2.addWidget(self.labelMain) 
     self.horizontalScrollBarIW = QtGui.QScrollBar(Form) 
     self.horizontalScrollBarIW.setOrientation(QtCore.Qt.Horizontal) 
     self.horizontalScrollBarIW.setObjectName(_fromUtf8("horizontalScrollBarIW")) 
     self.verticalLayout_2.addWidget(self.horizontalScrollBarIW) 
     self.gridLayout.addLayout(self.verticalLayout_2, 0, 0, 1, 1) 
     spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) 
     self.gridLayout.addItem(spacerItem, 0, 1, 1, 1) 
     self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1) 
     self.horizontalLayout_2 = QtGui.QHBoxLayout() 
     self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2")) 
     self.gridLayout_2.addLayout(self.horizontalLayout_2, 6, 1, 1, 1) 
     self.verticalLayout = QtGui.QVBoxLayout() 
     self.verticalLayout.setObjectName(_fromUtf8("verticalLayout")) 
     self.horizontalLayout_3 = QtGui.QHBoxLayout() 
     self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3")) 
     self.labelSmallIW = QtGui.QLabel(Form) 
     self.labelSmallIW.setObjectName(_fromUtf8("labelSmallIW")) 
     self.horizontalLayout_3.addWidget(self.labelSmallIW) 
     self.labelBigIW = QtGui.QLabel(Form) 
     self.labelBigIW.setObjectName(_fromUtf8("labelBigIW")) 
     self.horizontalLayout_3.addWidget(self.labelBigIW) 
     self.pushButton = QtGui.QPushButton(Form) 
     self.pushButton.setObjectName(_fromUtf8("pushButton")) 
     self.horizontalLayout_3.addWidget(self.pushButton) 
     self.verticalLayout.addLayout(self.horizontalLayout_3) 
     self.gridLayout_2.addLayout(self.verticalLayout, 4, 0, 1, 1) 
     self.horizontalLayout = QtGui.QHBoxLayout() 
     self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) 
     self.toolButtonLoad = QtGui.QToolButton(Form) 
     self.toolButtonLoad.setObjectName(_fromUtf8("toolButtonLoad")) 
     self.horizontalLayout.addWidget(self.toolButtonLoad) 
     self.prevButton = QtGui.QPushButton(Form) 
     self.prevButton.setObjectName(_fromUtf8("prevButton")) 
     self.horizontalLayout.addWidget(self.prevButton) 
     self.nextButton = QtGui.QPushButton(Form) 
     self.nextButton.setObjectName(_fromUtf8("nextButton")) 
     self.horizontalLayout.addWidget(self.nextButton) 
     self.gridLayout_2.addLayout(self.horizontalLayout, 2, 0, 1, 1) 
     self.checkBox = QtGui.QCheckBox(Form) 
     self.checkBox.setObjectName(_fromUtf8("checkBox")) 
     self.gridLayout_2.addWidget(self.checkBox, 5, 0, 1, 1) 

     self.retranslateUi(Form) 
     QtCore.QObject.connect(self.prevButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.horizontalSliderFrames.subtractStep) 
     QtCore.QObject.connect(self.nextButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.horizontalSliderFrames.addStep) 
     QtCore.QMetaObject.connectSlotsByName(Form) 

    def retranslateUi(self, Form): 
     Form.setWindowTitle(_translate("Form", "Form", None)) 
     self.labelMain.setText(_translate("Form", "TextLabel", None)) 
     self.labelSmallIW.setText(_translate("Form", "TextLabel", None)) 
     self.labelBigIW.setText(_translate("Form", "TextLabel", None)) 
     self.pushButton.setText(_translate("Form", "PushButton", None)) 
     self.toolButtonLoad.setText(_translate("Form", "...", None)) 
     self.prevButton.setText(_translate("Form", "<", None)) 
     self.nextButton.setText(_translate("Form", ">", None)) 
     self.checkBox.setText(_translate("Form", "CheckBox", None)) 

if __name__ == "__main__": 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    app.setStyle('cleanlooks') 
    Form = QtGui.QWidget() 
    ui = Ui_Form() 
    ui.setupUi(Form) 
    Form.show() 
    sys.exit(app.exec_()) 

看起来类似的错误发生在别人身上,由于一些“类相关问题“,我不知道。我不明白为什么它似乎当我从Geany文本编辑器中的“运行”,而不是当我从终端使用python my_gui.py我得到这个错误运行它做工精细:

Traceback (most recent call last): 
    File "gui_template.ui.py", line 136, in <module> 
    ui = Ui_Form() 
    File "gui_template.ui.py", line 39, in __init__ 
    self.setupUi(self) 
    File "gui_template.ui.py", line 89, in setupUi 
    QtCore.QObject.connect(self.prevButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.horizontalSliderFrames.subtractStep) 
AttributeError: 'QSlider' object has no attribute 'subtractStep' 

我也尝试创建以类似的方式一个单独的.py文件到这个人是怎么做的:

https://youtu.be/FcX2FsPlVeI?t=10m25s

不过,我还是得到同样的错误...任何想法?

回答

1

addStepsubtractStep插槽是deprecated members of QSlider。他们只是为了向后兼容Qt3。

看来你必须尝试运行使用Qt4/PyQt4构建的示例脚本,这些构建不包括这些不赞成使用的API。

本文档建议使用setValue插槽。但是这需要传递适当的递增/递减值,所以它不是真正的替代品(尤其是如果您通过Qt Designer连接信号的话)。

如果你想解决的例子,你有东西来取代像两个信号/槽连接:

def setSlider(delta): 
     self.horizontalSliderFrames.setValue(
      self.horizontalSliderFrames.value() + 
      delta * self.horizontalSliderFrames.singleStep()) 

    self.prevButton.clicked.connect(lambda: setSlider(-1)) 
    self.nextButton.clicked.connect(lambda: setSlider(+1)) 
+0

真奇怪!我正在使用Qt 4 Designer来连接我的滑块/按钮,但它仍然使用旧的语法... –

+0

@Vitto。你的Qt4/PyQt4软件包来自哪里?我原以为过时的API通常会被默认包含在内(他们是Arch Linux上的我)。 – ekhumoro

+0

好问题。 IIRC,我刚刚安装了通常的方式。 sudo apt-get install qt4-dev-tools qt4-designer 但是它已经有一段时间了,所以我实在无法肯定地说。 –

相关问题