Qt Python GUI Crashes on Button Click

孤街浪徒 提交于 2019-12-12 04:32:10

问题


The code is smaller version I put together to demonstrate what I am trying to do. I just need to get information from a QButtonGroup of radio buttons. Specifically the text of which one has been clicked. However, when I run click the button in the GUI python crashes. There is no error message so I can't pinpoint the cause. Below is the example code, below that is the image show upon clicking a radio button: Working earlier with a more inefficient method I got to sending commands to the server however the same error occurred when I tried to save the changes to the couchDB document specifically using db.save(doc) command.

# -*- 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_())


回答1:


You need to define the slot with a self argument:

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


来源:https://stackoverflow.com/questions/41925622/qt-python-gui-crashes-on-button-click

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!