简体   繁体   中英

Sending a Class' attribute to an outside function in the function call

I'm trying to generalise a function in my script by sending in the Class' attribute through the function call to an outside function, but since I have to call the attribute self.attribute_name inside the class and object_name.attribute.name outside it, I either get an error that no self.attribute_name exists outside the code or that no object_name.attribute.name exists inside. The part of my code concerned is as follows(this is just a fragment of the full code with many parts omitted):

class My_Window:
    self.get_info_box = Entry(root)
    self.entry_button = Button(root, text="Choose", command =lambda: self.retrieve_input(self.get_info_box, solitaire.cards))

    def retrieve_input(self, box, entity):
        self.user_input = box.get()
        entity = input_check(box)

def input_check(which_box):   # Function outside class
    my_window.which_box.delete(0, "end")    # This is want I would like to do if it worked 
    return 0   

my_window = My_Window()

Something in the back of my head tells me it might be possible to use lambda again to accomplish this but I'm still not sure how to use them properly and I couldn't find any active questions covering this specific case. Anyone have any ideas how to work this out?

Try it without the my_window .

def input_check(which_box):
    which_box.delete(0, "end")
    return 0

Incidentally, entity = input_check(box) won't cause solitaire.cards to retain the value returned by input_check , because assignment doesn't propagate upwards like that. If you want to change solitaire.cards , you'll need to do solitaire.cards = input_check(box) instead. If solitaire isn't visible inside retrieve_input , then you'll need to make it an attribute of self .

I think what you want is

def input_check(which_box):
    getattr(my_window,which_box).delete(0, "end")
    return 0

input_check("get_info_box")

but its hard to tell for sure

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