python - PyQt: Creating QT Widgets Programmatically -
there times when there need create many widgets (such qtgui.qlineedit() ) there values in list (we don't know how many values stored in list variable). create loop function run many times there values stored in list... such as:
for each in mylist: mylineedit = qtgui.qlineedit("mylineedit")
the problem approach every loop same variable name declared. there no way access mylineedit variable later. i've heard have success using eval()? or exec()? function. interesting see example. if there other way please post up. here example code start (if to):
from pyqt4 import qtcore, qtgui app = qtgui.qapplication(sys.argv) class mainwindow(qtgui.qmainwindow): def __init__(self): super(mainwindow, self).__init__() mainqwidget = qtgui.qwidget() mainlayout=qtgui.qvboxlayout() in range(5): exec( 'mygroupbox'+str(i)+'= qtgui.qgroupbox() ' ) exec( 'mylayout'+str(i)+' = qtgui.qhboxlayout()' ) exec( 'label'+str(i)+'=qtgui.qlabel("name '+str(i)+': ")' ) exec( 'self.mylineedit'+str(i)+'=qtgui.qlineedit()' ) exec( 'mylayout'+str(i)+'.addwidget(label'+str(i)+')' ) exec( 'mylayout'+str(i)+'.addwidget(self.mylineedit'+str(i)+', qtcore.qt.alignright)' ) exec( 'mygroupbox'+str(i)+'.setlayout(mylayout'+str(i)+')' ) exec( 'mainlayout.addwidget(mygroupbox'+str(i)+')' ) mainqwidget.setlayout(mainlayout) self.setcentralwidget(mainqwidget) window = mainwindow() window.show() window.resize(480,320) sys.exit(app.exec_())
another example:
from pyqt4 import qtcore, qtgui import random app = qtgui.qapplication(sys.argv) class mainwindow(qtgui.qmainwindow): def __init__(self, arglist): super(mainwindow, self).__init__() self.arglist=arglist mainqwidget = qtgui.qwidget() mainlayout=qtgui.qvboxlayout() in range(len(self.arglist)): exec( 'mygroupbox'+str(i)+'= qtgui.qgroupbox() ' ) exec( 'mylayout'+str(i)+' = qtgui.qhboxlayout()' ) exec( 'label'+str(i)+'=qtgui.qlabel("name '+str(self.arglist[i])+': ")' ) exec( 'label'+str(i)+'.setfixedwidth(100)' ) exec( 'self.mylineedit'+str(i)+'=qtgui.qlineedit()' ) exec( 'self.mylineedit'+str(i)+'.settext("'+str(random.random())+'")' ) exec( 'mylayout'+str(i)+'.addwidget(label'+str(i)+')' ) exec( 'mylayout'+str(i)+'.addwidget(self.mylineedit'+str(i)+', qtcore.qt.alignright)' ) exec( 'mygroupbox'+str(i)+'.setlayout(mylayout'+str(i)+')' ) exec( 'mainlayout.addwidget(mygroupbox'+str(i)+')' ) buttonbox = qtgui.qgroupbox() buttonslayout = qtgui.qhboxlayout() button_01 = qtgui.qpushbutton("close") button_01.clicked.connect(self.close) button_02 = qtgui.qpushbutton("print") button_02.clicked.connect(self.printout) buttonslayout.addwidget(button_01) buttonslayout.addwidget(button_02) buttonbox.setlayout(buttonslayout) mainlayout.addwidget(buttonbox) mainqwidget.setlayout(mainlayout) self.setcentralwidget(mainqwidget) def printout(self): in range(len(self.arglist)): exec( 'print self.mylineedit'+str(i)+'.text()' ) def close(self): sys.exit() mylist=['one','two','tree','four','five','six','seven'] window = mainwindow(mylist) window.show() window.resize(480,320) sys.exit(app.exec_())
a easy way handle situation use list:
lineedits = [] _ in range(5): mylineedit = qtgui.qlineedit("mylineedit") lineedits.append(mylineedit) mylayout = qtgui.qhboxlayout() lineedit in lineedits: mylayout.addwidget(lineedit)
alternatively use dictionary, if you'd later reference widget key:
lineedits = {} in range(5): mylineedit = qtgui.qlineedit("mylineedit") lineedits[i] = mylineedit lineedits[3].settext("my new text")
Comments
Post a Comment