【问题标题】:AttributeError: 'QSlider' object has no attribute 'subtractStep'AttributeError:“QSlider”对象没有属性“subtractStep”
【发布时间】:2015-05-15 01:23:52
【问题描述】:

当我尝试运行此代码时出现错误:

# -*- 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

但是,我仍然遇到同样的错误...有什么想法吗?

【问题讨论】:

    标签: python-2.7 pyqt4


    【解决方案1】:

    addStepsubtractStep 插槽是 deprecated members of QSlider。它们只是为了提供与 Qt3 的向后兼容性。

    看来您必须尝试使用​​不包含此类已弃用 API 的 Qt4/PyQt4 构建来运行示例脚本。

    文档建议改用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))
    

    【讨论】:

    • 真的很奇怪!我正在使用 Qt 4 Designer 连接我的滑块/按钮,但它仍然使用旧语法...
    • @Vitto。你的 Qt4/PyQt4 包是从哪里来的?我原以为默认情况下通常会包含已弃用的 API(它们适用于我在 Arch Linux 上)。
    • 好问题。 IIRC,我只是按照通常的方式安装了它。 sudo apt-get install qt4-dev-tools qt4-designer 不过已经有一段时间了,所以我真的不能确定。
    • ekhumoro,我还没有尝试过您的解决方案,我会在实际尝试后告诉您效果如何。
    猜你喜欢
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多