繁体   English   中英

在函数调用中将“类”属性发送到外部函数

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

我正在尝试通过对外部函数进行函数调用来发送Class属性来泛化脚本中的函数,但是由于我必须在类内部调用self.attribute_name属性, self.attribute_name在其外部调用object_name.attribute.name ,我或者得到一个错误,即代码外部不存在self.attribute_name或内部不存在object_name.attribute.name 我所关心的代码部分如下(这只是完整代码的一部分,省略了许多部分):

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

我脑海中有些东西告诉我,也许可以再次使用lambda来完成此操作,但是我仍然不确定如何正确使用它们,并且找不到适合此特定情况的任何有效问题。 任何人有任何想法如何解决这个问题?

在没有my_window情况下尝试。

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

顺便说一句, entity = input_check(box)不会导致solitaire.cards保留input_check返回的值,因为赋值不会像这样向上传播。 如果要更改solitaire.cards ,则需要改为solitaire.cards = input_check(box) 如果solitaire是不是里面可见retrieve_input ,那么你就需要使它的属性self

我想你想要的是

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

input_check("get_info_box")

但是很难确定

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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