グループフォーム(ArticoloForm)を表すクラスを作成し、initメソッドで渡された辞書から動的に属性を取得するコードを記述しています。グループフォームボタンが押されると は、記事が作成され、このクラスの記事を属性に追加し、IntVar情報でarticleIDはTkinter varトレース
そののinit方法で渡されArticoloFormインスタンスの別のクラス(ArticlesDisplyed)リストの記事を増加させています。 渡されたArticoloFormインスタンスの1つで変数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プリントの方法を引く作る「私たちは」との問題は、私は、単一の記事のクリックを追加するとき、「私たちは」2時間を印刷していることですボタンを1つのグループフォームの。 ボタンをクリックするとarticleIdが一度だけ変わるので、ののArticlesDisplayedのメソッドを一度呼び出すと、 "we +" str(articleId.get())が1つだけ表示されます。私の問題に加えて、私は記事を追加するためにボタンをクリックする初めて、 "私は0"が印刷されますが、記事IDは1に増加する必要があります。
私を助けることができますか?それほど明確でない場合は尋ねることができ、より良い方法で質問を策定しようとします。
ありがとうございました
を完了
これを確認し、それに応じて修正してください:https://stackoverflow.com/help/mcve –
これはちょうどを通して苦労するあまりにも多くのコードです。できるだけ凝縮させてください。 –