Tracubik wrote: > if you like, off course :) > > I'm making a port in python of a program made of bash commands + zenity > for the GUI. > so, i've to re-create a GUI in pyGTK and associate the right bash commands > to the buttons. > Instead of executing the bash script i simply print they in the console. > > so, here's my code > > import gtk > > class myProgram: > > def __init__(self): > ... > self.btnUname = gtk.Button("uname") > self.btnLs = gtk.Button("ls") > > self.btnUname.connect("clicked", self.print_associated_command, > "uname") self.btnLs.connect("clicked", self.print_associated_command, > "ls") ... > > def print_associated_command(self, widget, data=None): > UNAME_CODE = ['uname'] > LS_CODE = ['cd /home/myUserId/Images/SashaGray', > 'ls *.jpg'] > > command_list = { > "uname" : UNAME_CODE, > "ls" : LS_CODE > } > for item in command_list[data]: > print 'COMMAND: ' + item > print '-----------------------------------------------------' > > > do you like it? > considering i'll have about 40+ buttons, do you suggest me to move some > part of code outside in a different module?
If all buttons work the same you can treat them uniformly: import gtk command_defs = { "uname" : ['uname'], "ls" : ['cd /home/myUserId/Images/EarlGray', 'ls *.jpg'] } class MyProgram: def __init__(self): ... for name, command in command_defs.iteritems(): button = gtk.Button(name) button.connect("clicked", self.print_associated_command, command) button.show() ... ... def print_associated_command(self, widget, data=None): for item in data: print 'COMMAND:', item print '-----------------------------------------------------' You can then move the command_defs dictionary into another module or alternatively turn it into a JSON file and load it with the json module from the standard library: import json with open("command_defs.json") as f: command_defs = json.load(f) The corresponding JSON file would then look like { "uname" : ["uname"], "ls" : ["cd /home/myUserId/Images/EarlGray", "ls *.jpg"] } Note that with this approach all strings become unicode objects. -- http://mail.python.org/mailman/listinfo/python-list