动力学温度

2024-09-23 14:35:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写代码,创建一个表示组形式(ArticoloForm)的类,从init方法中传递的字典中动态获取属性。 当按下“组窗体”按钮时,将创建一个项目并将其添加到此类的属性项目中,并增加IntVar articleId

另一个类(articlesdisplayed)列出了在其init方法中传递的ArticoloForm实例的项目。 当传递的某个ArticoloForm实例中的变量articleId发生更改时,ArticlesDisplayed会在其draw方法中执行某些操作,这要归功于其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的draw方法打印为“we”,问题是当我添加一个组窗体的单个article点击按钮时,“we”被打印了两次。 但是ArticlesDisplayed的articlesid只在单击按钮时更改一次,因此应该调用ArticlesDisplayed的draw方法一次,并且只打印一个“we+”str(文章ID.get()). 除了我的问题,我第一次点击一个按钮添加一篇文章,是打印“我们0”,但articleId应该增加到1。你知道吗

你能帮我吗?如果不太清楚,我可以问,我会尽量用更好的方式来表达我的问题。你知道吗

谢谢


Tags: selfcontainerdeftypewidgetattributesrowattr
1条回答
网友
1楼 · 发布于 2024-09-23 14:35:32

找到了!!我觉得自己很愚蠢。我将传递给这些变量的trace方法的第一个参数从“r”更改为“w”。我想追踪写操作,而不是阅读。。。你知道吗

完成

相关问题 更多 >