Previously we implemented the ED record feature.
In this video we will implement the Delete record feature so that when the user selects a record and
they press on delete, then here we want to show a confirmation message such as Are you sure you want
to delete?
Then we have two buttons here, yes or no, so that the user can press yes if they want to confirm the
deletion or know if they want to continue without deleting that record.
So let's do that.
Currently we do have this empty dialog box.
That's because.
We did create, like, an empty glass here.
Note that I folded the other classes.
The code is still there, but I'm just hiding it.
So that's the delete dialog class.
It only has a pass statement for now.
So please write this class as you see on my screen if you don't have this already.
And moreover, we also have in main window, we also have the call for that class.
So the delete method is part of the main window class and this delete method is instantiating that delete
dialog which is empty for now.
So delete dialog is this class.
This is empty.
So let's run.
Write some code in the delete dialog.
So the init method of this class will be similar to the init methods of the added dialog class.
So let's copy some code here.
I'll get the first lines up to fix tied up to this line and go down to.
Delete.
I look.
Paste those lines in here.
So this will be delete student data.
But then the edit Let me fold this also.
The ad did in its method is creating a QVC box layout because we had those widgets stacked vertically
in this case.
This would be more appropriate to have.
A cute grid layout because our widgets are stacked horizontally and vertically.
So the message will be on top and then the buttons on the second row, one on the left and the other
button on the right.
So yes or no.
And then let's start creating the widgets.
Let's create a confirmation message using a cute label class.
Are you sure you want to delete?
That's a standard confirmation message you show to this kind of confirmation dialog boxes.
And then we want to have a yes button which will be equal to a queue push button widget having a yes
label and same goes for no.
Cute.
Push button.
No.
And then we add those to the layout using an add to widget method.
So that would be the confirmation message first on top.
So that would be row zero column zero, expanding one, row expanding, two columns.
You'll see how that will display in just a bit.
Layout and widget that will be yes button.
Second row So index one first column layout.
Add widget.
No.
One.
First row.
Second column.
And we do self set layout and we set this layout object as the layout of the Tillie dialog window.
Right.
So that's that's the good.
Now we still need to implement the functionality so that when the user presses, yes, something will
happen.
But let's see if we've got this right.
So for.
Are you sure you want to delete?
Yes.
No.
The sizing is a bit off.
So to fix that.
You simply want to remove these two sizing setting lines, and that will use a minimalistic dialog window,
which is the case for these kinds of confirmation windows.
So are you sure you want to delete yes or no if you press?
Yes.
For now, nothing happens.
But we can handle that using.
Yes, that clicked.
That connect.
When the button is clicked.
We want to execute a delete.
Student methods, which will be a method we will add inside delete dialogue.
So define delete.
Student Now, in delete.
Student Here is where we write that SQL query which will delete the student, the selected student record.
So similarly as in added dialogue.
We're going to get the student ID, So here we are in in it.
So the student ID can be accessed using main window, dot table, dot item, giving the index which
can be accessed through this line.
So we are trying to get the index and the ID of the currently selected row in the table.
Right.
So if the user I cannot run the code now to show you the guy, but when the user selects a cell, then
we want to get the idea of that selected cell.
So let's copy these two lines.
That one and this one.
First we get the index, go to delete dialogue.
Don't confuse it with this method, so be careful where you paste that.
So paste it inside.
Delete student.
So we get the index.
No, you could also paste it inside the init method.
So either way it's fine, right.
But let's just keep it in delete student this time.
And then you get the self Student ID.
Let me copy it together with the comment.
And we go inside.
Delete.
Student Let's put this comment up here.
Get index and Student ID get selected the row index and student ID, Yeah.
So now this student ID since this is part of delete students, we don't need this self.
If you placed it inside in it and you access it from delete, then you need to do it self that student
ID inside in it and then you access it with self.
Student ID from within delete student.
But now we can keep it as a local variable and then we're ready to establish a connection to the database
with skylight three that's connect.
That is the database DB file.
Create a cursor using connection dot cursor.
And using that cursor, we execute a SQL query.
This will be a delete SQL commands from students where ID is a question mark and in this place is going
to go.
So make to parentheses.
This should come as a tuple.
So here you go.
Student underscore id also make a comma here so that this is actually read as a tuple by python.
Otherwise, if you don't have the comma, this is just a value inside parentheses.
So this is not a tuple.
If it doesn't have at least a comma.
Right.
Then connection that commits because this is a database write operation.
So you should apply the changes cursor that close and connection close.
And then you may want to access main window.
So that's a load data.
Main window is the instance representing the main window of the application and we want to load the
data of the table.
We want to refresh the table.
Let's run this.
Let's delete John Deere.
We go to delete records.
We press on.
Yes.
And we've got an error.
Oh, I've got the typo here.
The table name is student.
Sorry.
Let's try again.
John Deere.
Delete.
Yes.
And John Deere disappears, but this window doesn't close.
That's another good practice We want to show like a prompt that tells the user that the record was successfully
deleted.
Right.
Let's do that.
So we can simply do that first by using self that close.
We want to close the current window, the yes or no window.
And then.
We make a confirmation widget which will be equal to Q message box.
This is a class which you need to import.
Up here from cue to widgets cue message box.
This is a simple window which creates.
So we're inside the elite students.
So this creates a simple dialog box, actually.
Q Message Box is a child of a cute dialog box.
So in other words, this is like a simpler dialog box that allows you to do simple things like, you
know, just showing some simple success confirmation or things like that.
For example, we can say confirmation widget title.
We said the success title to that window we are creating and then you access that variable again and
you can access the set text methods and you can say the record was deleted successfully.
And then you want to execute that.
Let's see what we've got.
Let's delete Jane Crow.
Press on.
Yes.
We've got an error that says Q message object has no attribute title of that actually is set window
title with Capital TX.
Let's try again.
John Smith.
This time delete.
John Smith is deleted and we've got we've got this window.
The record was deleted successfully.
You press ock and.
Yep.
That's how you do that.
We have one more lecture to go and that will be to implement the about page to show some information
about the application and we can mark this app as completed.
So thanks a lot for following.
I'll see you in the next video.
See you.