如何创建接受用户输入文本的单选按钮?

How do I create a radio button that accept user input text?

我对此很陌生,但我想知道如何让“其他”单选按钮接受用户输入作为文本字段?我想为用户提供自定义分隔符的选项。例如,他们可能希望分隔符是“><”或“//”。这可能使用 PyQt 吗?

这是我的属于 Delimeter 选项的单选按钮组的代码。

    #Label for Delimeter options
    self.delimeter_option_label = QtWidgets.QLabel(self)
    self.delimeter_option_label.setText("Delimeter Options")
    self.delimeter_option_label.setFont(MYFONT)
    self.delimeter_option_label.move(250,100)
    self.delimeter_option_label.adjustSize()

    #Delimeter option for "|" character
    self.delimeter_option1 = QRadioButton("|", self)
    self.delimeter_option1.move(250,125)
    self.delimeter_option1.resize(300,20)

    #Delimeter option for "," character
    self.delimeter_option2 = QRadioButton(",", self)
    self.delimeter_option2.move(250,150)
    self.delimeter_option2.resize(300,20)

    #Delimeter option for Tabs
    self.delimeter_option3 = QRadioButton("Tab", self)
    self.delimeter_option3.move(250,175)
    self.delimeter_option3.resize(300,20)

    #Delimeter option for custom delimeter
    self.delimeter_option4 = QRadioButton("Other", self)
    self.delimeter_option4.move(250,200)
    self.delimeter_option4.resize(300,20)

    #Groups Delimeter option buttons together
    box_group2 = QButtonGroup(self)
    box_group2.addButton(self.delimeter_option1)
    box_group2.addButton(self.delimeter_option2)
    box_group2.addButton(self.delimeter_option3)
    box_group2.addButton(self.delimeter_option4)

向您的布局添加一个条目。然后将事件处理程序绑定到“其他”单选按钮的 toggled 信号。处理函数将接收一个参数 checked,它是一个布尔值,当单选按钮被选中时为 True,否则为 False。基于此,您可以通过分别调用 showhide 方法来显示或隐藏条目。这是一个示例代码:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowTitle("Radio button demo")

        self.mainWid = QWidget(self)
        self.mainLay = QVBoxLayout(self)
        self.mainWid.setLayout(self.mainLay)
        self.setCentralWidget(self.mainWid)
        
        self.radioSemicolon = QRadioButton(",", self.mainWid)
        self.mainLay.addWidget(self.radioSemicolon)
        
        self.radioOther = QRadioButton("Other", self.mainWid)
        self.radioOther.toggled.connect(self.showHideOtherEntry)
        self.mainLay.addWidget(self.radioOther)
        
        self.radiosGroup = QButtonGroup(self)
        self.radiosGroup.addButton(self.radioSemicolon)
        self.radiosGroup.addButton(self.radioOther)
        
        self.otherEntry = QLineEdit(self.mainWid)
        self.otherEntry.setPlaceholderText("Enter other delimiter")
        self.mainLay.addWidget(self.otherEntry)
        self.otherEntry.hide()
        
    def showHideOtherEntry(self, clicked):
        if clicked:
            self.otherEntry.show()
        else:
            self.otherEntry.hide()

if __name__ == "__main__":
    app = QApplication([])
    mainWin = MainWindow()
    mainWin.show()
    app.exec_()