2017-08-17 61 views
0

我正在写码创建表示的基团的形式(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

能帮我吗?如果不是很清楚,可以问一下,我会试着用更好的方式来表达我的问题。

谢谢

+0

请仔细阅读这一点,并进行相应的修改:https://stackoverflow.com/help/mcve –

+1

这只是太多的代码涉水通过。请尝试尽可能地浓缩它。 –

回答

0

发现它!我觉得很愚蠢。我将这些变量从'r'传递给trace方法的第一个参数改为'w'。我想跟踪写操作,而不是阅读...

完成