試圖瞭解如何將字典傳遞給QDialog函數並找回編輯過的字典,我搜索了但找不到任何可以遵循的東西。 我正在使用模態調用,因此我需要在繼續之前關閉對話框。通過QDialog編輯字典不返回正確的數據
到目前爲止的問題是,應用程序獲取儘可能遠的returnAttributes()函數,此時它會打印出意外的字符串並且不會關閉。
dialog_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()
從素文字輸出:
[email protected]:~/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>}
[email protected]:~/Python_scripts/dialog$
感謝您的詳細解釋,我發現很難拿起Python語言,這有助於。我會做出你所建議的改變。 – salatwork 2013-04-29 23:31:50
感謝您的幫助,我做出了改變,併發揮了作用。是否有可能將字典中的編輯對象傳遞迴調用程序?我想在未來會讓這個功能更加靈活,假設我知道如何動態地創建標籤和編輯對象。 – salatwork 2013-04-30 00:51:44
>我發現很難拿起Python語言<這不是關於Python,而是關於Qt框架。 >是否有可能將編輯的對象在字典中傳回給調用程序<製作封裝函數,它將打開傳遞初始數據的模式窗體,將分析返回代碼並將數據提取出來並返回帶有數據的字典。同樣,這不是關於Python,而是關於所謂的編程模式。 – warvariuc 2013-04-30 07:16:06