Qt Python GUI崩溃按钮单击

时间:2017-01-29 20:32:10

标签: python qt couchdb

代码是我放在一起的较小版本,用于演示我要做的事情。我只需要从QButtonGroup的单选按钮获取信息。具体是单击其中一个的文本。但是,当我运行时,单击GUI python中的按钮崩溃。没有错误消息,所以我无法确定原因。下面是示例代码,下面是单击单选按钮时显示的图像: 使用效率更低的方法更早地工作我必须向服务器发送命令,但是当我尝试使用db.save(doc)命令将更改保存到couchDB文档时发生了同样的错误。

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

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)
        self.verticalLayoutWidget = QtWidgets.QWidget(Dialog)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(130, 80, 101, 80))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.rad2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
        self.rad2.setObjectName("rad2")
        self.group1 = QtWidgets.QButtonGroup(Dialog)
        self.group1.setObjectName("group1")
        self.group1.addButton(self.rad2)
        self.verticalLayout.addWidget(self.rad2)
        self.rad3 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
        self.rad3.setObjectName("rad3")
        self.group1.addButton(self.rad3)
        self.verticalLayout.addWidget(self.rad3)
        self.rad1 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
        self.rad1.setObjectName("rad1")
        self.group1.addButton(self.rad1)
        self.verticalLayout.addWidget(self.rad1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def printText(button):
        print(button.text())

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.rad2.setText(_translate("Dialog", "RadioButton"))
        self.rad3.setText(_translate("Dialog", "RadioButton"))
        self.rad1.setText(_translate("Dialog", "RadioButton"))

        ui.group1.buttonClicked.connect(self.printText) 

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要使用self参数定义插槽:

    def printText(self, button):
        print(button.text())