1
00:00:00,390 --> 00:00:01,470
Hey, we'll come back.

2
00:00:01,650 --> 00:00:04,560
In the previous video, we populated the table with data.

3
00:00:04,590 --> 00:00:09,200
In this video we will implement the ADD student functionality.

4
00:00:09,210 --> 00:00:18,330
So currently we have the file and the help menu items and let's implement the add student sub menu item.

5
00:00:18,510 --> 00:00:20,730
So let's close that.

6
00:00:21,510 --> 00:00:25,920
Let's locate at students, which is a queue action, Right?

7
00:00:25,950 --> 00:00:27,270
That's a queue action.

8
00:00:27,270 --> 00:00:30,510
That's how it's called in pq26.

9
00:00:31,320 --> 00:00:37,950
Now, once you have created that variable, then in the next line you refer to that variable.

10
00:00:42,340 --> 00:00:52,090
A vet referred to triggers, triggers, property of that cue action instance, and then that's connect.

11
00:00:52,090 --> 00:00:53,380
This is a method.

12
00:00:53,380 --> 00:01:00,850
So in parentheses you should specify a function or a method.

13
00:01:00,880 --> 00:01:06,070
In our case, it's going to be a method, just the method without calling it.

14
00:01:06,100 --> 00:01:09,010
So self insert what is self insert?

15
00:01:09,010 --> 00:01:11,500
Self insert is a method we need to add here.

16
00:01:11,500 --> 00:01:25,720
Now define insert and this method is going to instantiate an insert dialog class.

17
00:01:27,700 --> 00:01:30,460
And then execute that window.

18
00:01:30,480 --> 00:01:34,350
So this will be another class we will create.

19
00:01:34,440 --> 00:01:45,150
So let's do that class insert dialog which inherits from a queue dialog class.

20
00:01:45,810 --> 00:01:46,310
Right?

21
00:01:46,320 --> 00:01:50,340
So our main window inherits from queue, main window.

22
00:01:50,460 --> 00:01:54,890
Now this inherits from queue dialog.

23
00:01:54,900 --> 00:02:03,840
So this is a specific class in py queue to which serves the purpose of creating dialog windows like.

24
00:02:05,500 --> 00:02:07,690
The one we have here in the sketch.

25
00:02:08,810 --> 00:02:14,960
So the user will be able to enter the name, the course, select the course and the phone number and

26
00:02:14,960 --> 00:02:15,980
press submit.

27
00:02:15,980 --> 00:02:20,620
When they press submit, that data will be added to the table.

28
00:02:20,630 --> 00:02:22,420
So this is how it works.

29
00:02:22,430 --> 00:02:28,250
One more time we have the ADD student sub menu item, right?

30
00:02:28,340 --> 00:02:36,080
So when the user presses that add student item, then the insert method will be called, right?

31
00:02:37,660 --> 00:02:46,060
The insert method itself is going to create to generate an insert dialog, and that dialog will be displayed

32
00:02:46,060 --> 00:02:48,650
on the screen as a pop up window.

33
00:02:48,670 --> 00:02:51,640
Basically, yeah, that's the idea.

34
00:02:51,670 --> 00:02:56,410
So now all we have to do is implement the insert dialog class.

35
00:02:58,500 --> 00:03:04,850
Of course, the very first thing we do is define an init method with double underscores.

36
00:03:04,860 --> 00:03:08,460
And then the second thing is we call the parent class.

37
00:03:11,480 --> 00:03:17,240
So this is basically the same thing we did with the main window class.

38
00:03:17,270 --> 00:03:17,810
Right.

39
00:03:18,170 --> 00:03:20,690
And then you want to set a window title.

40
00:03:20,730 --> 00:03:24,530
So I'll just copy this and paste it in here.

41
00:03:24,530 --> 00:03:26,150
Self set window title.

42
00:03:26,150 --> 00:03:32,510
So self refers to the insert dialog instance, the particular instance, the particular window instance

43
00:03:32,510 --> 00:03:35,690
that is generated when the user is using the program.

44
00:03:37,070 --> 00:03:43,370
So this Q Dialogue class also have such a set window title method just like.

45
00:03:44,900 --> 00:03:47,450
The Q main window class has one.

46
00:03:48,020 --> 00:03:55,010
By the way, you can use the structure tab now to navigate through your classes, right?

47
00:03:55,400 --> 00:03:57,860
Because the code is getting bigger and bigger.

48
00:03:57,860 --> 00:04:04,550
So you want to use the structure and you'll see all your classes there and you can expand those.

49
00:04:04,550 --> 00:04:11,540
You can jump into a particular method like in its load data for the main window class inserts, and

50
00:04:11,540 --> 00:04:18,560
then you can collapse that, expand the other class and you can easily navigate through your code.

51
00:04:19,709 --> 00:04:19,959
Right.

52
00:04:20,040 --> 00:04:23,310
Let me make some space here between the two classes.

53
00:04:23,310 --> 00:04:29,040
And yet we want to change this to insert student data.

54
00:04:32,550 --> 00:04:40,410
It's good to set a fixed width of, for example, 300 and.

55
00:04:41,270 --> 00:04:44,620
A fixed height of 300 as well.

56
00:04:44,630 --> 00:04:47,360
This is a good practice for dialogue windows.

57
00:04:49,500 --> 00:04:52,800
This time we need to use a layout or a grid.

58
00:04:52,830 --> 00:04:56,880
You can also use a grid like we did with the example here.

59
00:04:57,900 --> 00:04:59,220
Agreed layout.

60
00:04:59,850 --> 00:05:08,550
But since this window is structured vertically, like we have a text box and under that we have another

61
00:05:08,550 --> 00:05:11,690
dropdown list and then a text box and then the button.

62
00:05:11,700 --> 00:05:17,820
Then it's more appropriate to just use a q v box layout.

63
00:05:18,270 --> 00:05:20,310
So make sure to import that.

64
00:05:22,320 --> 00:05:29,880
Make a coma here, perhaps because it's getting too long and in the next line, make some space.

65
00:05:29,880 --> 00:05:31,560
So that should be fine.

66
00:05:31,980 --> 00:05:36,270
Q Box layout is imported, and it's used in here.

67
00:05:38,420 --> 00:05:40,190
And then let's create the widgets.

68
00:05:41,960 --> 00:05:47,780
The first one is the name, let's say student name as the variable name is queue line.

69
00:05:47,780 --> 00:05:55,460
Edit So we use the same widgets we used before with example that py queue line.

70
00:05:55,460 --> 00:05:58,310
Edit right, queue line.

71
00:05:58,310 --> 00:06:09,380
Edit You can set a placeholder student name set placeholder text name.

72
00:06:09,710 --> 00:06:13,310
You'll see how this will be displayed when we run the code.

73
00:06:14,600 --> 00:06:23,810
And lastly, we want to access the layout variable and the add widget methods and add student name to

74
00:06:23,810 --> 00:06:24,620
that layout.

75
00:06:25,340 --> 00:06:28,670
Let's see how things are working so far.

76
00:06:28,700 --> 00:06:38,540
So to do that, you want to refer to self set layouts and set the layout variable as the layout.

77
00:06:39,690 --> 00:06:41,320
All right, then run the code.

78
00:06:42,550 --> 00:06:46,000
So that's the first window.

79
00:06:46,000 --> 00:06:46,990
The main window.

80
00:06:47,590 --> 00:06:50,320
So you want to go to file ad student.

81
00:06:51,140 --> 00:06:52,750
Now that's our dialog.

82
00:06:53,710 --> 00:06:57,040
So all we have to for is the name TextBox.

83
00:06:57,040 --> 00:07:03,490
You see the placeholder there name, That's the text, the string we placed in here.

84
00:07:03,760 --> 00:07:08,920
Now, any other widgets that we add here will be stacked under this widget.

85
00:07:09,430 --> 00:07:19,680
So let's go ahead and add the other widget, which is this dropdown list known as the combo box in Pie

86
00:07:19,690 --> 00:07:20,290
Q two.

87
00:07:20,830 --> 00:07:23,500
So that would be the course name, right?

88
00:07:23,800 --> 00:07:29,260
The teacher, the user is selecting a course to register that students.

89
00:07:30,370 --> 00:07:31,750
So that is cute.

90
00:07:31,750 --> 00:07:33,760
Combo box.

91
00:07:34,750 --> 00:07:41,440
That's something we need to import from the Q widgets again combo box.

92
00:07:44,380 --> 00:07:47,560
Then we want to make a list of courses.

93
00:07:49,840 --> 00:07:52,600
Available for students such as biology.

94
00:07:56,080 --> 00:07:56,860
Math.

95
00:08:00,560 --> 00:08:02,570
Astronomy and physics.

96
00:08:05,260 --> 00:08:14,860
Then we refer to the combo box variable course name that add items, and then you provide the list courses

97
00:08:14,860 --> 00:08:15,700
to that.

98
00:08:16,060 --> 00:08:19,380
And then we want to add the combo box to the layout.

99
00:08:19,390 --> 00:08:24,370
So layout add widget course name.

100
00:08:25,270 --> 00:08:26,380
So that's it.

101
00:08:26,800 --> 00:08:32,679
Let's add some comments such as add student name widget.

102
00:08:34,990 --> 00:08:38,350
Had a combo box of courses.

103
00:08:41,840 --> 00:08:43,159
Let's try this.

104
00:08:44,090 --> 00:08:46,520
So go to file, add student.

105
00:08:47,240 --> 00:08:47,650
Yeah.

106
00:08:47,660 --> 00:08:51,710
So now we have a combo box here with all those items.

107
00:08:53,470 --> 00:08:54,070
Right.

108
00:08:54,460 --> 00:08:57,880
Next ad.

109
00:09:01,000 --> 00:09:04,990
The phone number at Mobile Widget.

110
00:09:05,960 --> 00:09:11,330
So I'm going to copy these three lines and just change whatever we need to change.

111
00:09:11,330 --> 00:09:13,190
So mobile in here.

112
00:09:13,730 --> 00:09:21,050
Mobile Mobile set placeholder mobile layout of add widget mobile.

113
00:09:23,350 --> 00:09:25,720
Lastly, we need to add.

114
00:09:27,990 --> 00:09:34,920
I submit button, which is going to send all these user entries to the database.

115
00:09:35,700 --> 00:09:41,760
Let's call this the button because it's just one button so we don't have to write it, submit button

116
00:09:41,760 --> 00:09:43,020
or something else.

117
00:09:43,020 --> 00:09:49,140
So the button variable is equal to Q push button.

118
00:09:50,030 --> 00:09:50,930
This is a class.

119
00:09:50,930 --> 00:09:54,740
You need to import this as well if you haven't done so already, that's the button.

120
00:09:55,130 --> 00:09:57,350
So queue, push button.

121
00:10:00,360 --> 00:10:05,490
Give it a name such as register or submit.

122
00:10:06,690 --> 00:10:15,930
And then when the button is clicked, you want to connect it to a method such as add student.

123
00:10:15,930 --> 00:10:21,160
So this will be a method which is going to be part of which class.

124
00:10:21,180 --> 00:10:22,800
So where are we now?

125
00:10:24,000 --> 00:10:25,120
Insert dialogue.

126
00:10:25,140 --> 00:10:33,840
So inside this class, make sure to indent one space to make sure you are in the same indentation level.

127
00:10:33,870 --> 00:10:45,540
You can use this minus icon here to fold this method so that will not delete the code of the init method.

128
00:10:45,540 --> 00:10:50,580
It just folds it so you can fold it and you can unfold it.

129
00:10:51,510 --> 00:10:58,790
So now I'm writing an ADD student methods in here, right?

130
00:10:58,800 --> 00:11:02,400
So I make sure that this is in the same indentation level.

131
00:11:02,400 --> 00:11:06,060
So this is very useful when you have large classes.

132
00:11:07,020 --> 00:11:11,550
Now I can fold that out and add some code in here.

133
00:11:11,550 --> 00:11:12,600
Add students.

134
00:11:13,960 --> 00:11:17,710
For this, we need to establish a connection to the database again.

135
00:11:17,710 --> 00:11:20,500
So with SQLite three dot connect

136
00:11:23,080 --> 00:11:24,910
database dot to be.

137
00:11:29,060 --> 00:11:33,210
This time we need a cursor object connection dot cursor.

138
00:11:33,230 --> 00:11:34,970
Since we are inserting.

139
00:11:36,710 --> 00:11:43,880
Data and cursor needs to execute the SQL query.

140
00:11:46,580 --> 00:11:53,570
So this time it's going to be insert into students the name of the table.

141
00:11:54,140 --> 00:11:57,380
We want to insert name.

142
00:11:57,380 --> 00:12:01,340
I'll explain what these are name course mobile.

143
00:12:02,510 --> 00:12:05,080
So that's like a tuple.

144
00:12:05,090 --> 00:12:12,350
And then after this, you write values and then after values, you write parentheses.

145
00:12:12,350 --> 00:12:14,390
And three question marks here.

146
00:12:17,860 --> 00:12:22,420
So that's the first argument of the execute methods.

147
00:12:22,450 --> 00:12:25,390
Note that this is one string, right?

148
00:12:25,420 --> 00:12:26,470
Quotes here.

149
00:12:26,470 --> 00:12:27,500
Quotes here.

150
00:12:27,520 --> 00:12:29,940
Then we need to add another arguments.

151
00:12:29,950 --> 00:12:35,200
I'll write a comma and then enter to write the second argument in the second line.

152
00:12:35,970 --> 00:12:39,880
And here we need to write name course Mobile.

153
00:12:39,900 --> 00:12:45,210
Now, these are three variables that we have to create in here.

154
00:12:45,210 --> 00:12:47,640
Name, of course.

155
00:12:50,110 --> 00:12:58,930
And mobile name is going to be equal to the value that the user enters in the student name.

156
00:12:59,620 --> 00:13:04,150
So we need to access the student name from this method.

157
00:13:04,370 --> 00:13:07,990
A student name is currently a local variable.

158
00:13:09,150 --> 00:13:10,790
Of this methods.

159
00:13:11,210 --> 00:13:15,320
So we want to convert it into an instance variable.

160
00:13:15,950 --> 00:13:19,610
So add self to all these three variable occurrences.

161
00:13:19,910 --> 00:13:28,250
Then in here you want to refer to self that student name and then extract the text using the text methods.

162
00:13:29,160 --> 00:13:37,640
So now this name variable, the value of this variable will be injected into the first question mark,

163
00:13:37,650 --> 00:13:41,000
so which corresponds to the name column.

164
00:13:41,010 --> 00:13:47,520
And then we do the same for this course goes to the second question mark, mobile to the third question

165
00:13:47,520 --> 00:13:47,760
mark.

166
00:13:47,760 --> 00:13:53,730
So of course it's going to be self that course name, which also has to be.

167
00:13:57,740 --> 00:14:03,590
An instance variable so self that course name that item text.

168
00:14:03,590 --> 00:14:04,940
This is a combo box.

169
00:14:04,940 --> 00:14:07,250
So you need to use item text here.

170
00:14:08,530 --> 00:14:12,250
Self dot course name dot.

171
00:14:13,540 --> 00:14:16,390
Current index in parentheses.

172
00:14:16,570 --> 00:14:22,360
So that will give you the choice that the user made in the combo box.

173
00:14:23,350 --> 00:14:25,960
So we're talking about this combo box here.

174
00:14:27,160 --> 00:14:28,090
Mobile.

175
00:14:28,240 --> 00:14:31,390
This is self dot mobile.

176
00:14:32,340 --> 00:14:33,420
The text.

177
00:14:33,660 --> 00:14:36,860
So let's convert this into an instance variable as well.

178
00:14:36,870 --> 00:14:38,070
Self that mobile.

179
00:14:38,430 --> 00:14:39,840
Self that mobile.

180
00:14:40,750 --> 00:14:44,160
All right, so that's the add student method.

181
00:14:45,580 --> 00:14:46,670
Oh, I have a typo here.

182
00:14:46,710 --> 00:14:47,080
Course.

183
00:14:47,080 --> 00:14:47,410
Name?

184
00:14:47,410 --> 00:14:47,920
Sorry.

185
00:14:48,340 --> 00:14:52,240
And also here we have an error highlighted from pie chart.

186
00:14:52,360 --> 00:14:55,240
This has to be inside a tuple.

187
00:14:55,240 --> 00:14:58,810
So it's one arguments, not three arguments.

188
00:14:58,960 --> 00:15:01,090
So that's the first argument.

189
00:15:01,540 --> 00:15:02,620
The string.

190
00:15:02,950 --> 00:15:05,680
And that's the second argument.

191
00:15:05,830 --> 00:15:06,970
The tuple.

192
00:15:07,270 --> 00:15:11,700
Then you need to refer to connection that commit.

193
00:15:11,710 --> 00:15:18,280
So you call the commit method to actually apply the SQL query to the database.

194
00:15:18,640 --> 00:15:27,430
Once you commit, you want to point to the cursor object that's close to close the cursor and you want

195
00:15:27,430 --> 00:15:29,500
to also close the connection.

196
00:15:29,500 --> 00:15:31,750
So connection, that's close.

197
00:15:33,190 --> 00:15:36,040
Now we're ready to run the code and see.

198
00:15:38,180 --> 00:15:39,890
What we've done so far.

199
00:15:40,010 --> 00:15:42,650
So let's go to file at student.

200
00:15:44,890 --> 00:15:46,390
Let's add, Jane.

201
00:15:47,850 --> 00:15:48,510
Crowe.

202
00:15:49,430 --> 00:15:58,760
Select a course in the course name combo box, such as astronomy and typing a phone number, press and

203
00:15:58,760 --> 00:16:00,950
register now.

204
00:16:02,030 --> 00:16:03,230
I press and register.

205
00:16:03,280 --> 00:16:11,780
Nothing apparently happened, but we don't have any refreshing functionality yet.

206
00:16:11,780 --> 00:16:16,610
So the table is going to display the old data currently.

207
00:16:16,610 --> 00:16:18,820
Only if you close the window.

208
00:16:18,830 --> 00:16:21,790
So close the program and then run it again.

209
00:16:21,800 --> 00:16:28,070
Then you should be able to see the new added students or Jane Crow there.

210
00:16:29,780 --> 00:16:34,490
However, we can improve this by adding the refreshing capability.

211
00:16:34,880 --> 00:16:43,400
One thing here is to note that the ID is added automatically because that is configured when the database

212
00:16:43,400 --> 00:16:44,720
file was created.

213
00:16:44,720 --> 00:16:52,310
So you can create database files using this program DB browser for SQLite, you can create a database

214
00:16:52,310 --> 00:16:58,550
file, you can create a table in the database and you can specify, for example, that the ID column

215
00:16:58,550 --> 00:17:01,400
will be auto incremented.

216
00:17:01,400 --> 00:17:04,670
So the numbers, the idea is.

217
00:17:05,450 --> 00:17:09,800
Will be unique and they will be added automatically.

218
00:17:09,800 --> 00:17:17,420
For example, I've got this ten here because I'd added some more students before, which occupied number

219
00:17:17,420 --> 00:17:21,680
seven, eight and nine, and then I deleted those students.

220
00:17:21,680 --> 00:17:26,150
So now the next available number was ten.

221
00:17:26,569 --> 00:17:30,110
So that's a unique ID that is added automatically.

222
00:17:30,470 --> 00:17:37,100
Now if you want to load the data, you want to go to the add student method in here and you want to

223
00:17:37,100 --> 00:17:37,610
do.

224
00:17:39,030 --> 00:17:39,510
H.

225
00:17:42,070 --> 00:17:44,740
Calculator or whatever this is called.

226
00:17:45,510 --> 00:17:47,700
Well, we need to rename that to something better.

227
00:17:47,700 --> 00:17:51,320
But anyway, and then load data.

228
00:17:51,330 --> 00:17:56,880
So we referring to the main window, basically to this class, to the instance of that class.

229
00:17:56,900 --> 00:18:00,420
And then we refer to the load data method.

230
00:18:00,420 --> 00:18:02,700
So we're calling that method.

231
00:18:03,960 --> 00:18:11,700
So run the program again, expand the window file, add student.

232
00:18:13,440 --> 00:18:15,500
Joan, dear.

233
00:18:16,800 --> 00:18:17,880
Physics.

234
00:18:17,880 --> 00:18:19,470
One, two, three, four.

235
00:18:19,650 --> 00:18:26,850
Press on register, and you'll see that John Deere was added instantly to the database table.

236
00:18:26,850 --> 00:18:31,710
So that load method is working fine, so we can close this.

237
00:18:32,760 --> 00:18:35,580
There is room for improvement, of course.

238
00:18:35,700 --> 00:18:40,290
But we can call this functionality as completed for now.

239
00:18:41,480 --> 00:18:45,620
So I would like to refactor this name the age calculator here.

240
00:18:45,980 --> 00:18:48,830
So I'll go to a refactor rename.

241
00:18:49,310 --> 00:18:55,580
And I'll just call this main window refactor and that will refactor all the occurrences of that variable

242
00:18:55,580 --> 00:18:59,360
here, here, there and there as well.

243
00:18:59,690 --> 00:19:02,060
So now we have a more meaningful name.

244
00:19:02,210 --> 00:19:04,970
And I'll delete one line here.

245
00:19:05,240 --> 00:19:10,730
So just do some housekeeping, one more line in here.

246
00:19:10,970 --> 00:19:13,730
So yeah, things look better now.

247
00:19:14,510 --> 00:19:15,820
Now that's about this video.

248
00:19:15,830 --> 00:19:18,080
So we have some more things to do.

249
00:19:18,080 --> 00:19:19,280
So I'll see you in the next one.

