Question

    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. NameError: global name 'user_entry' is not defined This is a GUI if that matters. Also how would I change the Gui for currentscore everytime I click the sumbit.

submit = Gtk.Button("submit")
submit.connect("clicked",self.check_answer)
Was it helpful?

Solution

You access it on self, just like when you set it on the instance in __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.

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:

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("clicked", self.check_answer, total, current)

but I suspect that total and current and currentscore are also attributes on your class.

Perhaps another reading of the Python tutorial on classes would be helpful?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top