python qt 按钮,Qt Python GUI在单击按钮时崩溃

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

WDA9U.png

解决方案

You need to define the slot with a self argument:

def printText(self, button):

print(button.text())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值