【问题标题】:How to access class member functions from within different instances of that same class? Python如何从同一类的不同实例中访问类成员函数? Python
【发布时间】:2017-11-02 22:18:20
【问题描述】:

我的问题是我的tkinter 应用程序。我有一个用于折叠部分应用程序的按钮,但是如果我单击左侧的每个按钮,我必须按折叠按钮 7 次才能删除第一次创建的应用程序右侧的所有实例在createArea 成员函数中。

我已将类的每个实例存储在名为dictionary 的字典中,我需要destroyRight 函数在每个实例中销毁应用程序的所有右侧。

我不确定当每个类实例存储在字典中时如何访问它的功能,我也不确定如何让一个类实例与所有其他实例进行通信。

非常感谢您的帮助。

Image of my application

from tkinter import*

root = Tk()
class Buttons:
    def __init__(self,master,imperialText,metricText,metricVal):
        self.imperialText,self.metricText,self.metricVal,self.master = imperialText,metricText,metricVal,master

        self.displayedText  = (self.imperialText +'-'+ self.metricText)
        self.button = Button(self.master,text= self.displayedText,command = self.createArea)
        self.button.config(height= 3,width=30)
        self.button.grid(column = 0)

        self.rightCreated = False
        self.rightButtons = []
    def createArea(self):
        self.rightCreated = True
        self.entryBox = Entry(self.master)
        self.entryBox.bind('<Return>',self.calc)
        self.entryBox.grid(column = 1,row = 1)

        self.label = Label(self.master,text = 'Enter '+self.imperialText)
        self.label.grid(column = 1,row = 0)

        self.backButton = Button(self.master,text = '<<Collapse', command = self.destroyRight)
        self.backButton.grid(column = 1, row = 6)

        print('happen') 
        self.rightButtons.extend([self.entryBox,self.label,self.backButton])

    def destroyRight(self):
        collapseAll()
        print(self.rightButtons)
        for i in self.rightButtons:
            i.destroy()


    def calc(self):
        print('hi')

def collapseAll():
    for i in dictionary:
        dictionary[i].destroyRight()




dictionary = {'B1':None,'B2':None,'B3':None,'B4':None,'B5':None,'B6':None,'B7':None}
ImperialText = ['inches','miles','foot','yards','gallons','pounds','ounces']
MetricText = ['centimetres','kilometres','metres','metres','litres','kilograms','grams']
metricVal = [2.54,1.6093,0.3048,0.9144,4.546,0.454,0.454]

num = 0
for i in dictionary:
dictionary[i] = 
Buttons(root,ImperialText[num],MetricText[num],metricVal[num])
    num += 1
    if num == 6:
        print(i)

root.mainloop()

【问题讨论】:

    标签: python class dictionary tkinter


    【解决方案1】:

    您所要做的就是像dictionary['B1'].calc() 这样访问字典中的第一个按钮并调用calc 方法。

    【讨论】:

    • 我可以把它放在类中并从里面调用其他类实例的函数吗?感谢您的回答顺便说一句
    • 不,如果你想调用特定实例的方法,你将不得不这样做,显式调用它们的方法..即使它在循环中for i in dictionary: dictionary[i].calc()
    • 好的,谢谢您回答我的问题。我明天早上试试这个。
    • 我使用您的示例编辑了代码以包含一个函数,但是现在我只是在 collapseAll 字典中获取文件“C:\Python32\Projects\GUI\Tkinter 8.py”,第 42 行 [i ].destroyRight() 文件“C:\Python32\Projects\GUI\Tkinter 8.py”,第 31 行,在 destroyRight collapseAll() 文件“C:\Python32\Projects\GUI\Tkinter 8.py”,第 42 行,在 collapseAll dictionary[i].destroyRight() 一遍又一遍
    【解决方案2】:

    您的实例是字典中的值这一事实并没有什么特别之处。你可以把它想象成每个实例都有单独的变量B1, B2,...(实际上全局命名空间只是一个字典)。将它们放在 dict 中的一个优点是您可以轻松地遍历实例并在每个实例上调用相同的方法,例如:

    for b in dictionary.values():
        b.collapse()
    

    或其他。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 2012-01-06
      相关资源
      最近更新 更多