当我尝试运行此代码时出现错误:
# -*- 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_())
似乎类似的错误发生在其他人身上,因为一些与班级有关的问题&#34;我不知道。我不明白为什么当我&#34;运行&#34;从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
然而,我仍然得到同样的错误......有什么想法吗?
答案 0 :(得分:1)
addStep
和subtractStep
广告位是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))