我使用Qt Designer创建了一个自定义小部件。通过使用pyuic,我将.ui文件转换为.py文件。我的意图很简单:我需要找出选择了哪个单选按钮。
这是Designer转换的python脚本ui_persistence_size.py
:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'persistance_size2.ui'
#
# Created: Fri Dec 19 00:57:01 2014
# by: PyQt4 UI code generator 4.10.4
#
# 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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(449, 129)
self.gridLayout_2 = QtGui.QGridLayout(Dialog)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.gridLayout = QtGui.QGridLayout()
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.groupBox = QtGui.QGroupBox(Dialog)
self.groupBox.setObjectName(_fromUtf8("groupBox"))
self.size_256 = QtGui.QRadioButton(self.groupBox)
self.size_256.setGeometry(QtCore.QRect(0, 30, 81, 22))
self.size_256.setObjectName(_fromUtf8("size_256"))
self.size_512 = QtGui.QRadioButton(self.groupBox)
self.size_512.setGeometry(QtCore.QRect(90, 30, 81, 22))
self.size_512.setObjectName(_fromUtf8("size_512"))
self.size_768 = QtGui.QRadioButton(self.groupBox)
self.size_768.setGeometry(QtCore.QRect(179, 30, 81, 22))
self.size_768.setObjectName(_fromUtf8("size_768"))
self.size_1gb = QtGui.QRadioButton(self.groupBox)
self.size_1gb.setGeometry(QtCore.QRect(262, 30, 81, 22))
self.size_1gb.setObjectName(_fromUtf8("size_1gb"))
self.size_2gb = QtGui.QRadioButton(self.groupBox)
self.size_2gb.setGeometry(QtCore.QRect(352, 30, 81, 22))
self.size_2gb.setObjectName(_fromUtf8("size_2gb"))
self.size_3gb = QtGui.QRadioButton(self.groupBox)
self.size_3gb.setGeometry(QtCore.QRect(0, 70, 81, 22))
self.size_3gb.setObjectName(_fromUtf8("size_3gb"))
self.size_4gb = QtGui.QRadioButton(self.groupBox)
self.size_4gb.setGeometry(QtCore.QRect(90, 70, 81, 22))
self.size_4gb.setObjectName(_fromUtf8("size_4gb"))
self.choose = QtGui.QPushButton(self.groupBox)
self.choose.setGeometry(QtCore.QRect(310, 68, 96, 26))
self.choose.setObjectName(_fromUtf8("choose"))
self.cancel = QtGui.QPushButton(self.groupBox)
self.cancel.setGeometry(QtCore.QRect(200, 68, 96, 26))
self.cancel.setObjectName(_fromUtf8("cancel"))
self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Persistence Size Chooser...", None))
self.groupBox.setTitle(_translate("Dialog", "Choose Persistence Size..", None))
self.size_256.setText(_translate("Dialog", "256MB", None))
self.size_512.setText(_translate("Dialog", "512MB", None))
self.size_768.setText(_translate("Dialog", "768GB", None))
self.size_1gb.setText(_translate("Dialog", "1GB", None))
self.size_2gb.setText(_translate("Dialog", "2GB", None))
self.size_3gb.setText(_translate("Dialog", "3GB", None))
self.size_4gb.setText(_translate("Dialog", "4GB", None))
self.choose.setToolTip(_translate("Dialog", "<html><head/><body><p>Choose selected persistance size...</p></body></html>", None))
self.choose.setText(_translate("Dialog", "Choose", None))
self.cancel.setToolTip(_translate("Dialog", "<html><head/><body><p>Do not choose anything and close the window.</p></body></html>", None))
self.cancel.setText(_translate("Dialog", "Cancel", None))
以下是我正在处理的主要脚本persistence.py
:
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
from PyQt4 import QtGui
from ui_persistence_size import Ui_Dialog
import sys
import var
class PersistenceGui(QtGui.QDialog, Ui_Dialog):
#def __init__(self):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.cancel.clicked.connect(self.on_close_clicked)
self.ui.choose.clicked.connect(self.on_choose_clicked)
def on_close_clicked(self):
print "No persistence size choosen."
QtGui.qApp.closeAllWindows()
def on_choose_clicked(self):
if self.ui.size_256.isChecked():
var.persistence_size = "256"
elif self.ui.size_512.isChecked():
var.persistence_size = "512"
elif self.ui.size_768.isChecked():
var.persistence_size = "768"
elif self.ui.size_1gb.isChecked():
var.persistence_size = "1gb"
elif self.ui.size_2gb.isChecked():
var.persistence_size = "2gb"
elif self.ui.size_3gb.isChecked():
var.persistence_size = "3gb"
elif self.ui.size_4gb.isChecked():
var.persistence_size = "4gb"
print "Selected" + var.persistence_size
QtGui.qApp.closeAllWindows()
else:
print "Please choose persistence size..."
QtGui.QMessageBox.information(self, 'No size...', 'No persistence size selected.\n\nPlease choose persistence size and click Choose.')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = PersistenceGui()
myapp.show()
sys.exit(app.exec_())
当我执行主脚本时,我发现了两个问题。一个是除了一个单选按钮(256MB - 3GB),没有任何作用;当我选择最后一个单选按钮(4GB)时,代码重复三次。
$ ./persistence.py
Selected4gb
Selected4gb
Selected4gb
答案 0 :(得分:5)
输出打印三次的原因是因为您命名信号处理程序的方式。 connect slots by name feature将自动连接使用以下格式命名的信号处理程序:
on_[object name]_[signal name]
由于clicked
信号有两个重载,并且您自己添加了显式连接,因此总共建立了三个连接。
解决此问题的最简单方法是将处理程序的名称更改为:
self.ui.cancel.clicked.connect(self.handleCloseClicked)
self.ui.choose.clicked.connect(self.handleChooseClicked)
def handleCloseClicked(self):
...
def handleChooseClicked(self):
...
另一个问题是由if/elif
块的错误处理引起的。它应该看起来更像这样:
...
elif self.ui.size_4gb.isChecked():
var.persistence_size = "4gb"
else:
print "Please choose persistence size..."
QtGui.QMessageBox.information(self, 'No size...', 'No persistence size selected.\n\nPlease choose persistence size and click Choose.')
return
print "Selected", var.persistence_size
QtGui.qApp.closeAllWindows()