[英]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. 但是我怀疑你的班级的
total
, current
和currentscore
也是属性。
Perhaps another reading of the Python tutorial on classes would be helpful? 或许另外阅读关于类的Python教程会有所帮助吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.