Now that we've sorted out the user interface,
the next step is to populate this with some questions that we got from our API
call.
So everything in our program is linked and the place where we get hold of the
next question is inside our quiz_brain. At the moment,
the quiz_brain, when this method next_question is called,
will find the current question from the list of questions that it gets from
data.py, and then it will serve it up in a input.
This is what used to happen,
at least, when we were still making our text-based quiz app.
But in this case,
we actually want to be able to output that question text and then get hold of it
inside our ui.py.
So let's update our quiz_brain so that this question is returned as an output.
I'm gonna comment out this part of the code where we check the user answer
because we no longer get a user answer from the input. Instead,
we're going to take this question_text and the question_number
and we're going to serve it up as the output.
So let's go ahead and return this entire string.
And we can comment out this line of code.
Now inside our ui.py,
we can call that method and put it onto this piece of text in the canvas.
Let's create a method in here, which we'll call get_
_next_question.
And this method is going to tap into the quiz_brain and call this next_question.
So how do we get hold of the same quiz brain that we create in our main.py?
Well,
we could pass it in when we create our quiz UI as an input.
So this quiz,
which is creative from the quiz brain, can be passed into the quiz interface. And
to catch it, all we have to do is just to
add another parameter in our init
which we'll call the quiz brain.
And then we can create a property called the quiz
and we can set that equal to the quiz brain that we receive when we initialize
this new quiz interface.
So now when we have our get next question being called,
we're going to tap into the self.quiz,
and then we're going to call the method quiz_
_next_question. Now notice how as I'm typing,
you don't actually get anything showing up.
And the reason for this is because even though we're passing in that quiz brain
into here through the init, this file
doesn't actually know what is the datatype of this particular object that's
being passed in.
So one of the things that you can do is actually to add the datatype when you
create it as a parameter.
So we could say that this quiz brain thing that's going to be passed in must be
of data type quiz brain. And in order for that to work,
we need to import it. So from quiz
brain import the quiz brain class.
And now we can declare that when we initialize a new quiz interface,
we must pass in a quiz brain object
which is of the data type quiz brain. And once we've done that
and we've ensured this,
then when we try to initialize this and we try to pass on something else,
let's say I try to pass in the question bank
then this is going to give me a error and you can see it says expected type quiz
brain, but instead got a list.
So this also ensures that we don't make any mistakes
when we initialize the quiz interface.
Coming back to here and scrolling down, now, at this point
once we know the data type of quiz,
then we can start typing next question. And you can see,
it shows up all of the methods that are available to the quiz brain
object. We know that this is going to give us the output,
so this is the question text. And once we get that,
then we want to update our canvas.
So what's tap into our self.canvas and then call the item config method to
change the item, which is our self.question_text.
And the thing we want to change about it is the text
and we wanna set it to this q_text that we got from here.
Now,
all that's left to do is to actually call this method and we have to do it
in our init so that when we first initialize our user interface,
we don't end up seeing this Some Question Text. Instead,
we actually fetch the first question from our list of questions.
But remember that everything has to go before the main loop
because all the code you write after this line is not going to be executed until
that window gets destroyed.
So let's put it here and we can call self.get_next_question.
And now if we run our code,
you can see that instead of some dummy text,
we're actually getting a real question here.
The only problem is that it's not fully visible.
One of the tricks that we can use is to get the text in the canvas
to wrap by setting the text width property. Back up here
where we created our question_text, we can add
another argument which is called width.
And if we set that width to some value that is maybe a little bit less than the
width of the canvas,
then this question_text is automatically going to go on two separate lines.
I'm going to set it to 280 so that it's a little bit less than 300.
So that way there's a little bit of padding, either side. And now if I hit run,
you can see the question now fits into the actual piece of card. There you have
it. We've now created our get next question method
which is able to fetch the question text from the quiz brain
by returning the question text that it got. Now,
we still have our principles of Object Oriented Programming maintained where
each module is responsible for its own thing.
So the quiz brain is responsible for managing the quiz, getting questions,
keeping track of score, and our user interface
or our quiz interface class is responsible for putting those things onto the
screen to be displayed. In the next lesson,
we're going to take a closer look at Python type hints.
And then afterwards, we'll figure out how to get our buttons working.
So for all of that and more, I'll see you on the next lesson.