Previously, we added a status bar to our main window.
The status bar will show two buttons when the user clicks on a cell, an edit record button and a delete
record button.
When we press edit records, an empty dialog box shows up.
So in this video, we want to add the elements, the widgets, to this dialog window.
Let's do that.
So we have main window and then we have added dialog, which currently only has a pass statement.
So nothing is happening here.
So let's add some code such as define and in its methods as usual, this is where we construct those
widgets.
Now this added dialog will be very similar to the insert dialog which is constructed down here.
So there it is.
Therefore, what I'll do is I'll just copy this method so the init methods are folded.
So if I just double click here and I select the folder to line, you see the plus here.
So if I do control C now to copy that and I just go here and I just replace this.
So control V.
What I've just done here is I pasted the entire method so I can expand this.
And there it is.
So that's the init method, which for now it's identical with the insert method.
So we want to modify it.
For example, we want to call it update student data.
The width and the height are okay.
Then we have the vertical layout.
That's fine.
Then we have the name of the student.
And currently this textbox is empty.
You see, there's nothing inside Q line.
Edit This is just the placeholder.
Don't confuse this for the name of the text box.
So perhaps now with the added dialog, perhaps we want to show a default value in the box.
In the insert dialog, the box was empty, but with editing we want to play the game differently, right?
We want to show the existing value for the selected cell for the user.
If the user selected John Smith, then here we want to show John Smith that will be displayed as the
default value.
So how can we get the student name and place it inside these parentheses here?
Well, to get the student name, we should get access to the table first.
And the table is part of the main window variable.
Right.
Main window table.
Main window is the variable that we assigned at the very end here.
So main window is equal to the instance of the main window class.
The main window class has that.
Table attribute.
So we access the table and we get the current row from the table.
And this method will return an integer and we store that integer in the index variable.
The integer could be zero one, two, three, depending on what cell the user has selected in the table.
So if the user selected Michael Johnson, which is in the third row, then index will be equal to two
because the first row has an index of zero.
The second has an index of one and the third row has an index of two.
So index will be equal to two.
Now what do we do with this index?
Because this is just the index rate, but we want the student name to get the student name of the selected
row.
We want to refer to the table again.
So main window, that table dot item in parentheses, we say index one index is this.
So we specify the index of the row and we specify the index of the column.
So the first column is the ID column and it has an index of zero.
And the second column is the student name column.
It has an index of one.
That's why we place one here.
And you also want to apply the text method to extract the actual text.
So the actual student name string and that string now student name is placed inside Q line.
Edit pasted there the student name variable.
And yet we should be ready to go now with the next widget.
So let's check this one first.
Let's say get student name from selected row.
So there we go.
Let's run.
We should get an error.
First, I'll explain to you why.
When we press edit records, what happens is we get this error.
So in.
Line 125.
So that's here.
So this is.
We added dialogue.
You're right, the init method of the edit added dialog.
This is the the code we copy paste it from add student.
So this code still has some methods referring to the old code.
So this is add student.
We want to change this to update self that update which is the method.
We'll add in just a bit here.
So let's add that method.
Define update or maybe you can say update student update students.
This is the method which will access the database and actually alter update that particular row in the
database.
So let's just pass for now.
And let's run again.
So let's add the John Deere.
Yeah.
There we go.
So John Deere now is placed as the default value of the Q line edit box.
Now we go to the next combo box.
The combo box has biology as the default value.
As we did with the first widget for this one.
We also want to select a default value.
Right, let's do that.
So we go.
To the added dialogue class in the init method.
And before we create the combo box, we want to say here course name is equal to main window dot table.
Dot item.
Index two.
So this is the same code as this one here where we extract the pseudonym.
The only thing that changes is the index of the column.
So we want the third column, which is the course column, and it has an index of two and we extract
the text out of that.
And then.
This doesn't go here.
A boat.
It goes after we add the items the course is.
So we have a list here.
We add that list to the combo box.
So the combo box is represented by self, that course name.
Right?
And this is another variable.
This is just a local variable.
This is the attribute.
So these two are different entities.
Don't confuse that.
Right?
So then we say self that course name.
So the combo box and we access the set current text methods which expects a string and the string is
course name.
So there we go.
And that should.
Select physics for Andy Johnson, for example.
So physics was the course.
Andy Johnson's is a routine, and that is the selected default item in the combo box.
Lastly, let's do the same thing for the mobile phone.
So that's for the mobile phone.
So that's the same code.
Should be the same code with student name as well.
So again, before Q line added we do mobile is equal to main window dot table dot item index three dot
text with parentheses and mobile goes in here.
Right.
So now we have.
The structure completed, but what happens when the user presses register?
So when the user presses register, what we want to do is we want that the current values, let's say
the teacher wants to update the phone number of this student so they will write something like 12300
and they press on register.
These data.
Jane Crow astronomy and the new number should update the existing row in the database for this particular
student.
So let's implement that.
So there we go.
That's the register button.
Perhaps we change it to update.
It makes more sense.
And so this button will trigger the update student function.
So it will call this function this method.
And in this method.
Now we want to establish a connection to the database with skilled three point connect.
Database db db create a cursor object because we want to modify the database.
Connection that cursor and using that cursor we execute an SQL query.
Earlier in the code, we used a select query to search for existing records.
We used an insert query to add a new record to the table.
Now we want to update an existing record.
So we want to say update students the name of the table.
And we want to use the set command.
We want to set the name value.
So the name column to a question mark, comma, the course column to a question mark.
Comma.
Mobile to a question mark.
And here we don't use the comma, but we say, well, it is equal to a question mark.
That's the query.
So what this query will do is it will find the row where the ID is equal to a particular value, which
we will provide in this tuple.
So these are placeholders for now the question marks.
So we will match that row with that ID and we will change the name to a particular value, which we
also provides here in the tuple.
The course will be changed to a particular value and mobile as well.
So where do we get all these four values we have for question marks for values?
Well, we'll get all those full values from the user rights so when the user.
Is in this.
The dialogue.
Perhaps they change something like the phone number.
They.
Change the phone number to 100.
Maybe they change the name to something else.
So what we need to do as a programmer is we need to capture the current values in these boxes.
So the values at the time when the user presses the update button.
So, John, for example, a 100 year.
To do that we need to look at this self that student name Q line edit instance.
Right.
We don't need the student name variable, but the self dot student name instance variable, you should
be able to tell the difference because student name is only the existing value of the student, the
existing name of the student selected in the table.
So if the user selects Jane Doe, student name is equal to Jane Crow.
Right.
But then that student name variable will keep having the Jane Crow variable.
If the user changes something here, student name will still be Jane Crow.
So we shouldn't be working on that.
We should be working on this, so we should access that self, that student name, So we go here.
Let's put this tuple in here.
So we say self.
That's student name.
So that gives us the text box, the queue line added object, and we want to extract the text out of
that.
Right.
So now the text that the user enters in that added dialog box will be stored in this.
Expression here.
And that expression is going to be injected into this question mark.
So name is will be equal to that.
Value their rights and we should do the same for self dot course name.
Write that text and then is the next one mobile.
So you should respect the order of.
The column names that you set here.
So pseudo name, course name then more self.
That mobile.
Dot text.
Right.
So double check this self that course name.
Yes there's an actual.
Attributes for the combo box.
And then we have the mobile self that mobile, that's also fine.
Then we have the other placeholder, the fourth one that is the the idea.
So let's make another comma here, and I'd like to actually split this because it's getting too long.
So that's the fourth item of this tuple.
So the four item will be the idea.
How do we get the ID?
Well, the same way we did to get the student name.
The selected value of the cell.
It's student name.
So let's add another line.
Get ID from selected row.
So that would be student ID is main window dot table dot item index zero.
So the ID column has an index of zero.
And we get the text out of that right.
So that's the student ID and we need to access that student ID from a method.
Therefore, let's make it an attribute of the class.
So then we can see self dot student ID and that's it.
So the ID is something that doesn't change.
And this model we're working on.
The idea is to have a permanent ID for students.
And every new student that is added to the database, you want to assign them a new ID that has not
been used by others in the past.
So that's the query.
And before we execute, make sure to.
Commit the changes.
So when you do a select query, you don't have to commit because that's not a right operation.
But update and insert are right operations, right.
Database operations.
So you want to commit and you want to close the cursor and you want to close the connection.
And you also want to refer to the main window where the table is.
So the main window class and you want to.
Cool.
The.
Low data methods of main windows so that you can refresh the data over the table so that you can see
the changes that the user just made.
So let's go back to our class at the dialog.
We were inside of the student, so main window load data.
So just call that method maybe a comment, refresh the table.
And let's execute.
So let's change.
This student.
Go to edit.
Let's change the student's phone number.
This time it starts with 102345.
Press on Update.
And yeah, we've got an error.
Cucumber box object has no attribute text.
So the error is in this line 140.
So there you go.
Yeah.
Text is an attribute of a queue line edit widget, but not an attribute of a combo box for combo boxes.
So self that course name is a combo box object.
Therefore, we need to apply an item text method and inside that we want to provide self that course
name.
Again, that's current index method.
Let's split this as well.
And I'll split all these four items so each of them in one single line that's more readable.
And let's run again.
Right so that students we want to update with a new phone number 102345.
UPDATE and we can see that the.
Mobile phone is updated accordingly, so it seems to be working fine.
Let's also change the course of this student to math.
And math is updated as well.
So things seem to be working fine and that's how you added data in p y Q 36.
So that was quite fun, I suppose.
Thanks for following.
And let's add some more features to the program.
See you in the next video.