[英]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. 我正在尝试通过对外部函数进行函数调用来发送Class属性来泛化脚本中的函数,但是由于我必须在类内部调用
self.attribute_name
属性, self.attribute_name
在其外部调用object_name.attribute.name
,我或者得到一个错误,即代码外部不存在self.attribute_name
或内部不存在object_name.attribute.name
。 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. 我脑海中有些东西告诉我,也许可以再次使用lambda来完成此操作,但是我仍然不确定如何正确使用它们,并且找不到适合此特定情况的任何有效问题。 Anyone have any ideas how to work this out?
任何人有任何想法如何解决这个问题?
Try it without the my_window
. 在没有
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. 顺便说一句,
entity = input_check(box)
不会导致solitaire.cards
保留input_check
返回的值,因为赋值不会像这样向上传播。 If you want to change solitaire.cards
, you'll need to do solitaire.cards = input_check(box)
instead. 如果要更改
solitaire.cards
,则需要改为solitaire.cards = input_check(box)
。 If solitaire
isn't visible inside retrieve_input
, then you'll need to make it an attribute of self
. 如果
solitaire
是不是里面可见retrieve_input
,那么你就需要使它的属性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 但是很难确定
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.