python - Dynamic function>form generation with PySide -
i'm exploring ways in automatically generate forms based on simple class methods of file. idea "items.py" package grows, "gui.py" automatically keeps tabs , bit of validation. got working bit:
function file:
def printhellosomeone(str_someone): print "hello %s" % str_someone def printn(int_n): print "%i" % int_n def printrange(int_min, int_max): print range(int_min, int_max) gui file:
import inspect pyside import qtgui class autoform(qtgui.qformlayout): arg2gui = { 'int' : (qtgui.qspinbox, 'value'), 'str' : (qtgui.qlineedit, 'text'), } def __init__(self, (name, function)): super(autoform, self).__init__() self.function = function self.addrow(qtgui.qlabel(name)) self.call_list = [] arg in inspect.getargspec(function).args: self.call_list.append(self.arg2widget(arg)) button = qtgui.qpushbutton("run") button.clicked.connect(self.call_function) self.addrow(none, button) def arg2widget(self, arg): tp, name = arg.split('_') widget_obj, call = self.arg2gui[tp] widget = widget_obj() widget_call = getattr(widget, call) self.addrow(name, widget) return widget_call def call_function(self): args = [call() call in self.call_list] self.function(*args) if __name__ == '__main__': import sys import stuff app = qtgui.qapplication(sys.argv) w = qtgui.qwidget() w.show() w.setlayout(qtgui.qhboxlayout()) function in inspect.getmembers(stuff, inspect.isfunction): w.layout().addlayout(autoform(function)) sys.exit(app.exec_()) the code seems work, wondering if there better ways trying do.
Comments
Post a Comment