Dictionary 通过QDialog编辑字典未返回正确数据

Dictionary 通过QDialog编辑字典未返回正确数据,dictionary,pyqt,Dictionary,Pyqt,为了理解如何将字典传递给QDialog函数并取回编辑过的字典,我已经搜索过了,但找不到任何可以理解的内容。 我正在使用模式调用,因此在继续之前需要关闭对话框 到目前为止的问题是,应用程序一直到returnAttributes()函数为止,此时它会打印出意外的字符串,并且不会关闭 对话框\u grid.py #!/usr/bin/env python import sys from PyQt4 import QtGui, QtCore class grid_dialog(QtGui.QWidg

为了理解如何将字典传递给QDialog函数并取回编辑过的字典,我已经搜索过了,但找不到任何可以理解的内容。 我正在使用模式调用,因此在继续之前需要关闭对话框

到目前为止的问题是,应用程序一直到returnAttributes()函数为止,此时它会打印出意外的字符串,并且不会关闭

对话框\u grid.py

#!/usr/bin/env python

import sys
from PyQt4 import QtGui, QtCore

class grid_dialog(QtGui.QWidget):

    def __init__(self, **orig_attr):
        super(grid_dialog, self).__init__()
        self.initUI(**orig_attr)

    def initUI(self, **orig_attr):
        self.new_attr = orig_attr.copy()

        nameLabel = QtGui.QLabel("Name:")
        self.new_attr["name"] = QtGui.QLineEdit(orig_attr["name"])
        nameBox = QtGui.QHBoxLayout()
        nameBox.addStretch(1)
        nameBox.addWidget(nameLabel)
        nameBox.addWidget(self.new_attr["name"])

        shapeLabel = QtGui.QLabel("Shape:")
        self.new_attr["shape"] = QtGui.QLineEdit(orig_attr["shape"])
        shapeBox = QtGui.QHBoxLayout()
        shapeBox.addStretch(1)
        shapeBox.addWidget(shapeLabel)
        shapeBox.addWidget(self.new_attr["shape"])

        unitLabel = QtGui.QLabel("Unit:")
        self.new_attr["unit"] = QtGui.QLineEdit(orig_attr["unit"])
        unitBox = QtGui.QHBoxLayout()
        unitBox.addStretch(1)
        unitBox.addWidget(unitLabel)
        unitBox.addWidget(self.new_attr["unit"])

        scaleLabel = QtGui.QLabel("Scale:")
        self.new_attr["scale"] = QtGui.QLineEdit(orig_attr["scale"])
        scaleBox = QtGui.QHBoxLayout()
        scaleBox.addStretch(1)
        scaleBox.addWidget(scaleLabel)
        scaleBox.addWidget(self.new_attr["scale"])

        depthLabel = QtGui.QLabel("Depth:")
        self.new_attr["depth"] = QtGui.QLineEdit(orig_attr["depth"])
        depthBox = QtGui.QHBoxLayout()
        depthBox.addStretch(1)
        depthBox.addWidget(depthLabel)
        depthBox.addWidget(self.new_attr["depth"])

        planeLabel = QtGui.QLabel("Plane:")
        self.new_attr["plane"] = QtGui.QLineEdit(orig_attr["plane"])
        planeBox = QtGui.QHBoxLayout()
        planeBox.addStretch(1)
        planeBox.addWidget(planeLabel)
        planeBox.addWidget(self.new_attr["plane"])

        originLabel = QtGui.QLabel("Origin:")
        self.new_attr["origin"] = QtGui.QLineEdit(orig_attr["origin"])
        originBox = QtGui.QHBoxLayout()
        originBox.addStretch(1)
        originBox.addWidget(originLabel)
        originBox.addWidget(self.new_attr["origin"])

        acceptButton = QtGui.QPushButton('Accept')
        acceptButton.clicked.connect(self.returnAttributes)

        cancelButton = QtGui.QPushButton('Cancel')
        cancelButton.clicked.connect(self.discardAttributes)

        actionBox = QtGui.QHBoxLayout()
        actionBox.addStretch(1)
        actionBox.addWidget(acceptButton)
        actionBox.addWidget(cancelButton)

        attribBox = QtGui.QVBoxLayout()
        attribBox.addStretch(1)
        attribBox.addLayout(nameBox)
        attribBox.addLayout(shapeBox)
        attribBox.addLayout(unitBox)
        attribBox.addLayout(scaleBox)
        attribBox.addLayout(depthBox)
        attribBox.addLayout(planeBox)
        attribBox.addLayout(originBox)
        attribBox.addLayout(actionBox)

        self.setLayout(attribBox)    
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Attributes')
        self.show()

    # this is not working
    def returnAttributes(self):
        print self.new_attr
        return self.new_attr
        QtGui.QDialog.close(self)

    def discardAttributes(self):
        QtGui.QDialog.close(self)




def main():
    # Test case
    app = QtGui.QApplication(sys.argv)

    part_attr = {}
    part_attr["name"] = "name1"
    part_attr["shape"] = "shape1"
    part_attr["unit"] = "unit1"
    part_attr["scale"] = "scale1"
    part_attr["depth"] = "depth1"
    part_attr["plane"] = "plane1"
    part_attr["origin"] = "origin1"
    part_attr["action"] = "action1"

    # Print original data    
    print part_attr

    # Edit data 
    part_attr = grid_dialog(**part_attr)

    # Print edited data
    print part_attr

    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
来自scipt的输出:

emos@bob:~/Python_scripts/dialog$ ./dialog_grid.py 
{'origin': 'origin1', 'scale': 'scale1', 'name': 'name1', 'shape': 'shape1', 'depth': 'depth1', 'plane': 'plane1', 'action': 'action1', 'unit': 'unit1'}
<__main__.grid_dialog object at 0xb70a64ac>
{'origin': <PyQt4.QtGui.QLineEdit object at 0xb70a6a4c>, 'scale': <PyQt4.QtGui.QLineEdit object at 0xb70a67c4>, 'name': <PyQt4.QtGui.QLineEdit object at 0xb70a653c>, 'depth': <PyQt4.QtGui.QLineEdit object at 0xb70a689c>, 'shape': <PyQt4.QtGui.QLineEdit object at 0xb70a6614>, 'plane': <PyQt4.QtGui.QLineEdit object at 0xb70a6974>, 'action': 'action1', 'unit': <PyQt4.QtGui.QLineEdit object at 0xb70a66ec>}
emos@bob:~/Python_scripts/dialog$ 
emos@bob:~/Python\u脚本/dialog$。/dialog\u grid.py
{'origin':'origin1','scale1','name':'name1','shape1','depth1','plane':'plane1','action':'action1','unit':'unit1'}
{'origin':,'scale':,'name':,'depth':,'shape':,'plane':,'action':'action1','unit':}
emos@bob:~/Python\u脚本/对话框$
  • 类应该有CamelCase名称:
    GridDialog
    而不是
    grid\u dialog

  • 调用类
    grid\u对话框
    ,但从
    QtGui.QWidget
    继承它?通常用于显示带有设置某些数据的字段的表单
    QDialog

  • 这是错误的:

    def discardAttributes(self):
        QtGui.QDialog.close(self)
    
    首先,因为第二点。其次,您可以只使用
    self.close()

  • 如果使用以下内容,您可以使代码更具可读性:

    def __init__(self, **orig_attr):
        super(grid_dialog, self).__init__()
        self.initUI(**orig_attr)
    
    def initUI(self, name, shape, unit, ...):
        ...
    
  • 要将一些数据传递给对话框并从中获得一些结果,通常使用

  • 对话框窗口是主要用于短期任务的顶级窗口 以及与用户的简短通信

    QDialog
    的另一个特殊性是它可以被
    接受
    拒绝
    。当模式对话框被接受/拒绝时,您会得到相应的返回代码,但是窗口不是关闭的,而是隐藏的,因此您可以从对话框中读取所有值

    因此,您需要从
    QDialog
    继承类并进行更改

        acceptButton.clicked.connect(self.returnAttributes)
        cancelButton.clicked.connect(self.discardAttributes)
    

    也改变了:

        nameLabel = QtGui.QLabel("Name:")
        self.new_attr["name"] = QtGui.QLineEdit(orig_attr["name"])
        nameBox = QtGui.QHBoxLayout()
        nameBox.addStretch(1)
        nameBox.addWidget(nameLabel)
        nameBox.addWidget(self.new_attr["name"])
    

    然后使用类似于:

    dialog = GridDialog(...)
    return_code = dialog.exec()
    if return_code == QtGui.QDialog.Accepted:
        # get the data
        dialog.nameEdit.text()
    

    感谢您的详细解释,我发现学习Python语言很困难,这很有帮助。我会做你建议的更改。谢谢你的帮助,我做了更改,效果很好。是否可以将字典中编辑的对象传递回调用程序?我想这将使函数在将来更加灵活,假设我知道如何动态创建标签和编辑对象。>我发现学习Python语言很困难。<这不是关于Python,而是关于Qt框架。>是否有可能将字典中的编辑对象传递回调用程序<生成一个包装函数,该函数将打开传递初始数据的模态表单,分析返回代码,并将数据提取回并返回一个包含数据的字典。同样,这不是关于Python,而是关于所谓的编程模式。
        nameLabel = QtGui.QLabel("Name:")
        self.nameEdit = QtGui.QLineEdit(name)
        nameBox = QtGui.QHBoxLayout()
        nameBox.addStretch(1)
        nameBox.addWidget(nameLabel)
        nameBox.addWidget(self.nameEdit)
    
    dialog = GridDialog(...)
    return_code = dialog.exec()
    if return_code == QtGui.QDialog.Accepted:
        # get the data
        dialog.nameEdit.text()