0
import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self, items=None):
        super(Example, self).__init__()
        self.items = items or {'India':['New Delhi', 'Bangalore']}

        self.initUI()

    def initUI(self):

        self.combo = QtGui.QComboBox(self)
        self.cbBox = QtGui.QComboBox(self)
        self.combo.addItems(self.items.keys())
        self.cbBox.addItems(self.items[str(self.combo.currentText())])

        self.combo.move(50, 50)
        self.cbBox.move(50,80)
        self.combo.currentIndexChanged.connect(self.onActivated)    
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Learning by practice')
        self.show() 

    def onActivated(self):
        self.cbBox.clear()
        self.cbBox.addItems(self.items[str(self.combo.currentText())])

def main():
    app = QtGui.QApplication(sys.argv)
    items = sys.argv[1:]


    ex = Example(items)
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()  

the above code gives error if i pass this dictionary {'India':['New Delhi', 'Bangalore'], 'Canada': ['Toronto','Vancouver']} from the command line.

3
Contributors
6
Replies
81
Views
3 Years
Discussion Span
Last Post by krystosan
Featured Replies
  • I would pass json format command '{"India":["New Delhi", "Bangalore"], "Canada": ["Toronto","Vancouver"]}' and in python code import json self.items = json.loads(items) This avoids any eval issue, and eases cross programs calls. Read More

  • 1

    Also note that json (JavaScript Object Notation) expects your strings to be in double quotes. Read More

  • > i guess i can use eval(str) No! Use json.loads() Read More

1

I would pass json format

command '{"India":["New Delhi", "Bangalore"], "Canada": ["Toronto","Vancouver"]}'

and in python code

import json
self.items = json.loads(items)

This avoids any eval issue, and eases cross programs calls.

Edited by Gribouillis

1

Also note that json (JavaScript Object Notation) expects your strings to be in double quotes.

0

the json format returns '{"India":["New Delhi", "Bangalore"], "Canada": ["Toronto","Vancouver"]}' while i want a dictionary...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.