简体   繁体   English

如何从一种方法访问变量到另一种方法?

[英]How to access variables from one method into another?

    def check_answer(total,current):
        if user_entry == books:
            current += 1
            total += 1
            currentscore = current


   def __init__(self):
       ...
       self.user_entry = Gtk.Entry()

I was wondering how can I access user_entry from __init__ and check it in check_answer without getting this error. 我想知道如何从__init__访问user_entry并在check_answer检查它而不会出现此错误。 NameError: global name 'user_entry' is not defined This is a GUI if that matters. NameError: global name 'user_entry' is not defined如果重要,则为GUI。 Also how would I change the Gui for currentscore everytime I click the sumbit . 此外,我将如何改变GUI的currentscore每次我点击了sumbit

submit = Gtk.Button("submit")
submit.connect("clicked",self.check_answer)

You access it on self , just like when you set it on the instance in __init__ : 您可以在self上访问它,就像在__init__的实例上设置它一样:

if self.user_entry.get_text() == books:

where you get the text out of the Gtk.Entry() object by calling the get_text() method on it. 通过调用get_text()方法从Gtk.Entry()对象中获取文本。

Note that your check_answer method needs to take a self argument for this to work, and must accept the original object as an argument: 请注意, check_answer方法需要使用self参数才能check_answer ,并且必须接受原始对象作为参数:

def check_answer(self, button):
    if self.user_entry.get_text() == books:
        current += 1
        total += 1
        currentscore = current

If you need some extra arguments to be passed in, you need to pass these in to submit.connect() : 如果需要传入一些额外的参数,则需要将这些参数传递给submit.connect()

submit.connect("clicked", self.check_answer, total, current)

but I suspect that total and current and currentscore are also attributes on your class. 但是我怀疑你的班级的totalcurrentcurrentscore也是属性。

Perhaps another reading of the Python tutorial on classes would be helpful? 或许另外阅读关于类Python教程会有所帮助吗?

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

相关问题 我将如何从一个类访问变量到另一个类? - How would I access variables from one class to another? 如何在PyQt4中将变量从一个类访问到另一个类? - How to access variables from one class to another class in PyQt4? 在 Python 中从一种方法调用变量到另一种方法 - Calling variables from one method to another in Python 如何在同一个类中从一种方法访问列表到另一种方法 - How to access the list from one method to another in the same class 如何在同一个类中从一个方法访问我的变量? - how to access my variable from one method to another in same class? 如何在基于django类的视图中从同一个类中的另一个方法访问一个方法的变量 - how to access variable of one method from another method within the same class in django class based views 在 python 中使用 unittest 访问从一个测试到另一个测试的变量 - access of variables from one test to another using unittest in python 如何将变量从一个视图发送到另一个视图? - How to send variables from one views to another? 如何访问在另一类中在一类中声明的全局变量? - How to access global variables, declared in one class, during another class? 如何在 jupyter notebook 中将一个 ipynb 文件中定义的变量访问到另一个文件中? - How to access variables defined in one ipynb file into another in jupyter notebook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM