作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
为什么我无法收到 self.sender() 输出?我的意思是识别点击了哪个按钮(启动下载功能),然后匹配相应的进度条作为下载指示器。
错误输出为:
回溯(最近调用最后):
文件“final.py”,第 308 行,在 buttonClicked 中
buttonHandle = self.sender()
AttributeError: 'Ui_MainWindow' 对象没有属性 'sender'
非常感谢。
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.setGeometry(600,300,727,455)
MainWindow.setMinimumSize(QtCore.QSize(727, 455))
#MainWindow.setMaximumSize(QtCore.QSize(727, 455))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 727, 23))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuFile = QtGui.QMenu(self.menubar)
self.menuFile.setObjectName(_fromUtf8("menuFile"))
self.menuAbout = QtGui.QMenu(self.menubar)
self.menuAbout.setObjectName(_fromUtf8("menuAbout"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.actionExit = QtGui.QAction(MainWindow)
self.actionExit.setObjectName(_fromUtf8("actionExit"))
self.actionPythAri = QtGui.QAction(MainWindow)
self.actionPythAri.setObjectName(_fromUtf8("actionPythAri"))
self.menuFile.addAction(self.actionExit)
self.menuAbout.addAction(self.actionPythAri)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuAbout.menuAction())
# scrollArea
self.mainLayout = QtGui.QVBoxLayout()
self.scrollArea = QtGui.QScrollArea()
self.scrollArea.setWidgetResizable(True)
self.scrollAreaLayout = QtGui.QVBoxLayout()
#groupBoxes
self.groupBoxes = QtGui.QWidget()
self.groupBoxesLayout = QtGui.QVBoxLayout()
self.groupBoxes.setLayout(self.groupBoxesLayout)
self.groupBoxesLayout.setContentsMargins(5,5,5,5)
for i in range(3):
groupBox = QtGui.QGroupBox(_fromUtf8('Issue #%d' %i))
groupLayout = QtGui.QVBoxLayout()
#groupEllements
self.groupBoxUpWidget = QtGui.QWidget()
self.groupBoxUpLayout = QtGui.QHBoxLayout()
self.groupBoxDownWidget = QtGui.QWidget()
self.groupBoxDownLayout = QtGui.QHBoxLayout()
self.groupBoxUpWidget.setLayout(self.groupBoxUpLayout)
self.groupBoxDownWidget.setLayout(self.groupBoxDownLayout)
#mainwidget-temp
self.groupBoxMainWidget = QtGui.QWidget()
self.groupBoxMainLayout = QtGui.QVBoxLayout()
self.groupBoxMainWidget.setLayout(self.groupBoxMainLayout)
# ISSUE THUMB
self.issueThumb(self.groupBoxUpLayout)
#SETTING MAIN LAYOUT FOR EVERY GROUPBOX
groupBox.setLayout(groupLayout)
# text box
#self.txtLayout = QtGui.QHBoxLayout()
txt = QtGui.QLabel()
txt.setMaximumWidth(500)
txt.setText(_fromUtf8('Issue Description'))
#self.txtLayout.addWidget(self.txt)
#self.txt.setLayout(self.txtLayout)
#progressBars
progressBar = QtGui.QProgressBar()
progressBar.setMaximumWidth(500)
# controls
readBtn = QtGui.QPushButton(_fromUtf8('Read it!'),groupBox)
readBtn.setMaximumWidth(100)
#readBtn.setText()
readBtn.clicked.connect(self.buttonClicked)
# ADD EVERY WIDGET TO THE APPROPRIATE LAYOUT
self.groupBoxUpLayout.addWidget(txt)
self.groupBoxDownLayout.addWidget(readBtn)
self.groupBoxDownLayout.addWidget(progressBar)
# ADD PARENT WIDGETS TO MAIN GROUPBOX LAYOUT
groupLayout.addWidget(self.groupBoxUpWidget)
groupLayout.addWidget(self.groupBoxDownWidget)
groupLayout.addWidget(self.groupBoxMainWidget)
#ADD EVERY GROUPBOX TO A MAIN LAYOUT
self.groupBoxesLayout.addWidget(groupBox)
#self.scrollArea.setLayout(self.scrollAreaLayout)
self.scrollArea.setWidget(self.groupBoxes)
self.mainLayout.addWidget(self.scrollArea)
self.centralwidget.setLayout(self.mainLayout)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
authenticatedUser = False
authenticatedUser = self.authenticateDialog()
print authenticatedUser
if authenticatedUser == False:
sys.exit(0)
def buttonClicked(self):
buttonHandle = self.sender()
print buttonHandle
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui2 = Ui_MainWindow()
ui2.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
最佳答案
def buttonClicked(self):
buttonHandle = self.sender()
print buttonHandle
此处的 self 是 Ui_MainWindow
,它不是 QT 对象,您可能需要检查 MainWindow
的 .sender。因此将 UI_MainWindow 的类变量设置为 MainWindow 对象并检查该对象的发送者。
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
....
def buttonClicked(self):
buttonHandle = self.MainWindow.sender()
print buttonHandle
关于python - PyQt4 - 无法接收 sender() 信号/如何识别单击了哪个按钮并将其与适当的进度条匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20752753/
这段代码在 Java 中的等价物是什么?我放了一部分,我对 I/O 部分感兴趣: int fd = open(FILE_NAME, O_WRONLY); int ret = 0; if (fd =
我正在尝试将维度为 d1,d2,d3 的张量 M[a1,a2,a3] reshape 为维度为 d2, d1*d3 的矩阵 M[a2,a1*a3]。我试过 M.reshape(d2,d1*d3) 但是
我是一名优秀的程序员,十分优秀!