我正在寫碼創建表示的基團的形式(ArticoloForm)一類,以動態地從在INIT方法傳遞的字典屬性。 當按下組形式按鈕,製品被創建並添加到屬性該類的文章和IntVar條款ArticleID增加Tkinter的變種跟蹤
了在其的init方法傳遞ArticoloForm實例的另一類(ArticlesDisplyed)列表的文章。在通過ArticoloForm實例之一 當變量改變條款ArticleID,ArticlesDisplayed做其畫方法在其updateList方法的東西,感謝痕跡。
這裏的類:
class ArticoloForm:
articleList = {}
def __init__(self, container, gridRow, gridColumn, className, attributes):
self.container = container
self.gridRow = gridRow
self.gridColumn = gridColumn
self.className = className
if type(attributes) == dict:
self.attributes = attributes
def _getValues_(self, event):
# metodo che recupera i valori dei widget di una singola form e li inserisce nel dizionario widgetsValues. Descrive un articolo.
widgetsValues = {}
#per ogni attributo nel dizionario degli attributi
for attr in self.attributes:
#se il dizionario ha la chiave widget e quindi c'è un widget
if self.attributes[attr].has_key('widget'):
#se il widget è diverso da 'text' prende il valore in un modo, altrimenti in un altro, siccome il widget text ha bisogno di indici di inizio e fine
if self.attributes[attr]['type'] != "Text":
widgetsValues[attr] = self.attributes[attr]['widget'].get()
else:
widgetsValues[attr] = self.attributes[attr]['widget'].get("1.0", "end-1c")
widgetsValues['articleType'] = str(self.className).split(".")[1]
#~ inserisce nel dizionario che contiene la lista degli articoli creati da questa form il nuovo articolo. La chiave è un id IntVar di cui monitoro le modifiche
self.articleList[self.articleId.get() + 1] = widgetsValues
temp = self.articleId.get()
self.articleId.set(temp + 1)
#~ print "ID: " + str(self.articleId.get()) + "\n\n"
def _bindAddArticle_(self):
for attr in self.attributes:
if self.attributes[attr].has_key('button'):
self.attributes[attr]['button'].bind("<Button-1>", self._getValues_)
def draw(self):
# etichetta che specifica il tipo di articolo
self.articleId = IntVar()
self.articleId.set(0)
Label(self.container, text="Aggiunta " + str(self.className.__name__), font="bold").grid(row=self.gridRow, column=self.gridColumn, columnspan=2)
# disegno gli attributi prendendo i dati dal dizionario e aggiungendo il widget da disegnare al dizionario per poterlo richiamare negli events
for attr in self.attributes.iterkeys():
if self.attributes[attr]['type'] == 'Combobox':
Label(self.container, text=attr).grid(row=self.gridRow + self.attributes[attr]['row'], column=self.gridColumn)
#~ default = StringVar(self.container)
#~ default.set("Seleziona...")
self.attributes[attr]['widget'] = Combobox(self.container)
self.attributes[attr]['widget']['values'] = self.attributes[attr]['values']
elif self.attributes[attr]['type'] == 'Text':
Label(self.container, text=attr).grid(row=self.gridRow + self.attributes[attr]['row'], column=self.gridColumn)
self.attributes[attr]['widget'] = Text(self.container, width=self.attributes[attr]['width'], height=self.attributes[attr]['height'])
elif self.attributes[attr]['type'] == 'Entry':
Label(self.container, text=attr).grid(row=self.gridRow + self.attributes[attr]['row'], column=self.gridColumn)
self.attributes[attr]['widget'] = Entry(self.container)
if self.attributes[attr].has_key('widget'):
self.attributes[attr]['widget'].grid(row=self.gridRow + self.attributes[attr]['row'], column=self.gridColumn+1)
if self.attributes[attr]['type'] == 'Button':
self.attributes[attr]['button'] = Button(self.container, text="Aggiungi "+ str(self.className.__name__))
self.attributes[attr]['button'].grid(row=self.gridRow + self.attributes[attr]['row'], column=self.gridColumn+1)
#Separator(self.container, orient=VERTICAL).grid(row=self.gridRow-1, column=self.gridColumn, rowspan=len(self.attributes.keys()), sticky='sn')
self._bindAddArticle_()
#~ def removeArticle(self, event, articleId): TODO
#~ self.articleList.pop(articleId, None)
#~ temp = self.articleId.get()
#~ self.articleId.set(temp - 1)
######################################################################################
class ArticlesDisplayed:
#~ labels: CONTIENE TUTTI I GRUPPI DI ETICHETTE E BOTTONI. OGNI ETICHETTA E' UN ARTICOLO AGGIUNTO ALL'ORDINE. LA CHIAVE DI OGNI ARTICOLO CORRISPONDE ALL'ARTICLEID DELLA CLASSE ARTICOLOFORM
#~ articleForm: L'ISTANZA DI UNA FORM CREATA
labelsGroup = {}
def __init__(self, container, gridRow, gridColumn, articleForms=[]):
self.container = container
self.gridRow = gridRow
self.gridColumn = gridColumn
self.articleForms = articleForms
def updateList(self):
traceIdList = {}
i = 0
for articleForm in self.articleForms:
traceIdList[i] = articleForm.articleId
traceIdList[i].trace("r", self.__draw__)
i += 1
def __draw__(self, event, d, s):
print "we " + str(self.articleForms[0].articleId.get())
所以我的主要代碼:
# draw five group forms
scarpaAttrs = {'colore' : {'row' : 1, 'type' : 'Combobox', 'values' : Scarpe.possibiliColori},
'modello' : {'row' : 2, 'type' : 'Combobox', 'values' : Scarpe.possibiliModelli},
'marca' : {'row' : 3, 'type' : 'Combobox', 'values' : Scarpe.possibiliMarche},
'descrizione' : {'row' : 4, 'type' : 'Text', 'width' : 25, 'height' : 3},
'Aggiungi' : {'row' : 5, 'type' : 'Button'}}
scarpaForm = ArticoloForm(mainframe, 5, 0, Scarpe, scarpaAttrs)
scarpaForm.draw()
borsaAttrs = {'colore' : {'row' : 1, 'type' : 'Combobox', 'values' : Borsa.possibiliColori},
'modello' : {'row' : 2, 'type' : 'Combobox', 'values' : Borsa.possibiliModelli},
'marca' : {'row' : 3, 'type' : 'Combobox', 'values' : Borsa.possibiliMarche},
'descrizione' : {'row' : 4, 'type' : 'Text', 'width' : 25, 'height' : 3},
'Aggiungi' : {'row' : 5, 'type' : 'Button'}}
borsaForm = ArticoloForm(mainframe, 5, 3, Borsa, borsaAttrs)
borsaForm.draw()
cinturaAttrs = {'colore' : {'row' : 1, 'type' : 'Combobox', 'values' : Cintura.possibiliColori},
'modello' : {'row' : 2, 'type' : 'Combobox', 'values' : Cintura.possibiliModelli},
'marca' : {'row' : 3, 'type' : 'Combobox', 'values' : Cintura.possibiliMarche},
'descrizione' : {'row' : 4, 'type' : 'Text', 'width' : 25, 'height' : 3},
'Aggiungi' : {'row' : 5, 'type' : 'Button'}}
cinturaForm = ArticoloForm(mainframe, 5, 6, Cintura, cinturaAttrs)
cinturaForm.draw()
giaccaAttrs = {'colore' : {'row' : 1, 'type' : 'Combobox', 'values' : Giacca.possibiliColori},
'modello' : {'row' : 2, 'type' : 'Combobox', 'values' : Giacca.possibiliModelli},
'marca' : {'row' : 3, 'type' : 'Combobox', 'values' : Giacca.possibiliMarche},
'descrizione' : {'row' : 4, 'type' : 'Text', 'width' : 25, 'height' : 3},
'Aggiungi' : {'row' : 5, 'type' : 'Button'}}
giaccaForm = ArticoloForm(mainframe, 5, 9, Giacca, giaccaAttrs)
giaccaForm.draw()
articoloAttrs = {'colore' : {'row' : 1, 'type' : 'Entry', 'values' : Articolo.possibiliColori},
'modello' : {'row' : 2, 'type' : 'Entry'},
'marca' : {'row' : 3, 'type' : 'Entry'},
'descrizione' : {'row' : 4, 'type' : 'Text', 'width' : 25, 'height' : 3},
'Aggiungi' : {'row' : 5, 'type' : 'Button'}}
articoloForm = ArticoloForm(mainframe, 5, 13, Articolo, articoloAttrs)
articoloForm.draw()
然後我打電話ArticlesDisplayed顯示添加物品。現在
articles = ArticlesDisplayed(mainframe, 16, 0, articleForms=[scarpaForm, borsaForm, giaccaForm, cinturaForm])
articles.updateList()
,以便爲我的問題做調試,我讓畫ArticlesDisplayed打印的方法,「我們」,問題是,「我們」是印了一次,當我添加一個單篇文章點擊一組形式的按鈕。 但條款ArticleID變化僅是一次單擊按鈕時,所以畫ArticlesDisplayed的方法應被調用一次,只有一個打印「我們+」 STR(articleId.get())。除了我的問題,我第一次點擊一個按鈕來添加文章,是印有「我們0」,但條款ArticleID應提高到1
能幫我嗎?如果不是很清楚,可以問一下,我會試着用更好的方式來表達我的問題。
謝謝
請仔細閱讀這一點,並進行相應的修改:https://stackoverflow.com/help/mcve –
這只是太多的代碼涉水通過。請嘗試儘可能地濃縮它。 –