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.

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.

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

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

i guess i can use eval(str)

i guess i can use eval(str)

No! Use json.loads()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.