简体   繁体   中英

How to access class member functions from within different instances of that same class? Python

My issue is that with my tkinter application. I have a button used to collapse part of the application, however if I click on each of the buttons on the left I have to press the collapse button 7 times to get rid of all instances of the right side of the application which is first created in the createArea member function.

I have stored each instance of the class in a dictionary called dictionary and I need for the destroyRight function to destroy all of the right side of the application in every instance.

I am unsure how to access the functions of each class instance when it is stored in a dictionary and I am also unsure as to how I am able to get one instance of a class to communicate with all the others.

Help would be very much appreciated.

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()

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

There's nothing special about the fact that your instances are values in a dictionary. You can think of this just like having separate variables B1 , B2 ,... for each instance (in fact the global namespace is just a dictionary). An advantage of having them in a dict is you can easily loop over the instances and call the same method on each instance like:

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

or whatever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM