Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否可以将QInputDialog与嵌套列表或类似的内容一起使用?_Python_Python 3.x_Pyqt_Pyqt5_Qinputdialog - Fatal编程技术网

Python 是否可以将QInputDialog与嵌套列表或类似的内容一起使用?

Python 是否可以将QInputDialog与嵌套列表或类似的内容一起使用?,python,python-3.x,pyqt,pyqt5,qinputdialog,Python,Python 3.x,Pyqt,Pyqt5,Qinputdialog,我找到的PyQt5对话框上的所有示例或文档都使用了简单的经典列表,每行仅限一项,就像我的代码示例中的红色、蓝色或绿色一样 我正在寻找一种很好的方法来构建一个更详细的多维列表,比如一个表,在这个表中,用户可以看到并选择整行,并将多个值作为输入对话框中的一个项目,而不是单个值 例如,这样的嵌套列表: [Ryan',24',m',[Lisa',22',f',[Joe',30',m']] ->假设列表中的三个列表中的每一个都应该是对话框中可以选择的一行条目。就像在一个表中,每行都有一个复选框 这样可能吗

我找到的PyQt5对话框上的所有示例或文档都使用了简单的经典列表,每行仅限一项,就像我的代码示例中的红色、蓝色或绿色一样

我正在寻找一种很好的方法来构建一个更详细的多维列表,比如一个表,在这个表中,用户可以看到并选择整行,并将多个值作为输入对话框中的一个项目,而不是单个值

例如,这样的嵌套列表: [Ryan',24',m',[Lisa',22',f',[Joe',30',m']]

->假设列表中的三个列表中的每一个都应该是对话框中可以选择的一行条目。就像在一个表中,每行都有一个复选框

这样可能吗?有人知道吗

#The normal (limited) version with a simple list I am referring to looks like that:
def getChoice(self):
    itemlist = ("Red","Blue","Green")
    item, okPressed = QInputDialog.getItem(self, "Get item","Color:", itemlist, 0, False)
    if okPressed and item:
        print(item)
join方法获取一个iterable中的所有项,并将它们连接到一个字符串中

语法:string.joinetrable


好主意,谢谢!虽然不是真的将数据保存在多个字段中,比如在表/嵌套列表中,但使用普通字符串方式解决它是一个优雅的技巧。这似乎是QInputDialog的答案,虽然我希望使用真正的列,但可以使用它。可能对于真实的表格之类的东西,需要其他小部件/其他方法的真实列表…我应该补充一点,一个真正完美的解决方案还允许从结果中直接访问所选行的所有单个部分,例如,进一步处理所选姓名为Joe field 0的信息,Joe field 0 30岁,field 1。但是,如果这是不可能的,一个字符串中的组合显示版本也可以作为后备;姓名,年份,性别='Ryan 24 m'。是的,当然,但这是对真正独立字段的攻击;Sara Jean一加入我们,我们就有麻烦了。。。虽然我们可以通过引入特定的分隔符从技术上解决这个问题,但让图形对话框的用户看到这些分隔符并不是一个非常优雅的解决方案。。。不过还是要谢谢你_lst=[x代表《莎拉·琼24米》中的x。拆分]2。姓名,年份,性别=[[U lst if len_lst==3 else[[U lst[0]+'++[1],[U lst[-2],[U lst[-1]]]0]姓名,年份,性别=[[U lst if len_lst==3 else['.[0:-2],[U lst[-2],[U lst[-1]]
import sys
from PyQt5.QtCore import QTimer
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QLineEdit, QInputDialog, QLabel, QVBoxLayout


class PopupDialog(QtWidgets.QDialog):
    def __init__(self):
        super(PopupDialog, self).__init__()
        self.selected_item = None
        layout = QtWidgets.QFormLayout()
        self.setLayout(layout)
        self.setWindowTitle("New Popup")
        self.setMinimumWidth(400)

#        items = (['Ryan', 24, 'm'], ['Lisa', 22, 'f'], ['Joe', 30, 'm'])
        items = (['Ryan', '24', 'm'], ['Lisa', '22', 'f'], ['Joe', '30', 'm'])
        items = [ " ".join(item) for item in items ]                              # <<<-----<
        item, okPressed = QInputDialog.getItem(self, "Get item",
                          "Color:", items, 0, False)
        if okPressed and item:
            self.selected_item = item


class MainWindow(QtWidgets.QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setMinimumWidth(600)
        self.setWindowTitle("Main Window")
        self.le = QtWidgets.QLineEdit()
        button = QtWidgets.QPushButton("Select...")
        button.clicked.connect(self.get_input)
        layout = QtWidgets.QHBoxLayout()
        layout.addWidget(self.le)
        layout.addWidget(button)
        self.setLayout(layout)

    def get_input(self):
        popup = PopupDialog()
        print("got selection data: ", popup.selected_item)
        self.le.setText(popup.selected_item)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())