我试图动态地在运行时创建按钮与PyQT4.7的Python:动态属性名称不生成的exec()或eval()函数
但是,这是我的第一个Python程序,我不知道如何获得我想要的功能。
我想是能够替代文本字符串的属性名称:
即
for each in xrange(4):
myname = "tab1_button%s" % each #tab1_button0, tab1_button1, tab1_button2
#self.ui.tab1_button0 = QtGui.QPushButton(self.ui.tab) <--normal code to create a named button
setattr(self.ui,myname,QtGui.QPushButton(self.ui.tab)) #rewrite of line above to dynamicly generate a button
#here's where I get stuck. this code isn't valid, but it shows what i want to do
self.ui.gridLayout.addWidget(self.ui.%s) % myname
#I need to have %s be tab1_button1, tab1_button2, etc. I know the % is for string substituion but how can I substitute the dynamically generated attribute name into that statement?
我假设有一个BASICA语言结构,我缺少允许这样做。因为这是我的第一个程序,请把我很容易;)
的按钮越来越数字排序的名字,但在我正在编写的程序中,他们的全名将从字典中提取,因此我不确定如何使用您的示例不用智能名称跟踪它们。 – PyNewbie27 2010-03-23 02:37:23
将按钮存储在字典中? – FogleBird 2010-03-23 02:54:03