2012-06-14 26 views
2

說我有一本字典滿參數:通用值編輯器GUI的字典

{speed = 1, intelligence = 3, dexterity = 2} 

我要調用創建一個Label和程序上在此列表中的每個項目SpinBox一個循環,萬一我想稍後添加更多屬性。我可以創建窗口並返回更新值就好了。我唯一的問題是,我希望根據需要創建所有小部件,無論我有7或20個屬性要編輯。

所以標籤對象可以被稱爲speed_Label和智能標籤對象intelligence_Label,以及包含速度值紡紗器會speed_SpinBox等等,然後我會回傳輕鬆。然而,這

一)好像命名差實踐

B)似乎很難看到,因爲我無法找出如何在程序上給對象的名稱,說

for KEY in dict.keys(): # say the KEY is "Speed" 
    # this would produce a Label object called Speed_Label 
    # which displays the text "Speed" 
    "KEY" + "_Label" = QLabel("KEY") 
+0

你_need_的對象有名字,如果你用手命名它們呢?您可以簡單地擁有這些小部件的數組或字典,並通過編號或字符串將它們編入索引。 – sarnold

回答

2

爲什麼不直接使用列表或字典?

像這樣的東西應該工作:

widgets = {} 
form = QFormLayout() 
for key, value in your_dict.iteritems(): 
    widgets[key] = widget = {} 
    widget['spinbox'] = spinbox = QSpinBox() 
    spinbox.setValue(value) 
    form.addRow(key, spinbox) 
+0

這工作完美。現在來逆向工程吧!謝謝。 – RodericDay