WEBVTT 1 00:00:00.110 --> 00:00:01.800 Hi guys and welcome back. 2 00:00:01.800 --> 00:00:03.320 In this video we're going to be 3 00:00:03.320 --> 00:00:05.070 asking the user to confirm 4 00:00:05.070 --> 00:00:06.158 that the really want to exit 5 00:00:06.158 --> 00:00:09.300 if they have unsaved changes. 6 00:00:09.300 --> 00:00:11.040 So let's jump right in it. 7 00:00:11.040 --> 00:00:13.230 We're gonna go and create a new function. 8 00:00:13.230 --> 00:00:15.080 I'm just gonna call it, 9 00:00:15.080 --> 00:00:15.913 create it down here, 10 00:00:15.913 --> 00:00:19.100 and call it confirm quit. 11 00:00:19.100 --> 00:00:19.933 And what this is gonna do 12 00:00:19.933 --> 00:00:21.480 is it's going to start off 13 00:00:21.480 --> 00:00:23.690 as unsaved equal false. 14 00:00:23.690 --> 00:00:26.040 And then it's gonna cycle through the tabs 15 00:00:26.040 --> 00:00:28.660 and if anyone of them is unsaved 16 00:00:28.660 --> 00:00:30.070 it's gonna turn this to true. 17 00:00:30.070 --> 00:00:33.920 So we'll do for tab in notebook dot tabs. 18 00:00:33.920 --> 00:00:35.660 We will get the current widget, 19 00:00:35.660 --> 00:00:38.250 which is tab widget is root dot name 20 00:00:38.250 --> 00:00:40.880 to widget of the tab. 21 00:00:40.880 --> 00:00:43.780 And again we are using the tab name here 22 00:00:43.780 --> 00:00:45.010 and then we're getting the actual 23 00:00:45.010 --> 00:00:48.360 widget using the name to widget method. 24 00:00:48.360 --> 00:00:51.700 Then we are going to do that the content 25 00:00:53.130 --> 00:00:58.130 is equal to the tab widget dot get 1.0 26 00:00:59.850 --> 00:01:02.053 to end minus one C. 27 00:01:03.540 --> 00:01:05.440 And then we're gonna check the contents. 28 00:01:05.440 --> 00:01:08.260 So if the hash of the content 29 00:01:08.260 --> 00:01:11.470 is not equal to the text contents, 30 00:01:11.470 --> 00:01:14.110 SDR of tab widget sorry. 31 00:01:14.110 --> 00:01:15.090 I'm actually gonna rename this 32 00:01:15.090 --> 00:01:17.720 to text widget just because 33 00:01:17.720 --> 00:01:18.770 it makes it much clearer 34 00:01:18.770 --> 00:01:19.920 of what it actually is. 35 00:01:20.810 --> 00:01:23.800 Then unsaved is true 36 00:01:23.800 --> 00:01:25.360 because they are different. 37 00:01:25.360 --> 00:01:28.280 So we can now break. 38 00:01:28.280 --> 00:01:29.420 If they are the same, 39 00:01:29.420 --> 00:01:32.900 that means that this file is not unsaved 40 00:01:32.900 --> 00:01:34.630 and it should have the little asterisk 41 00:01:34.630 --> 00:01:36.000 beside its name. 42 00:01:36.000 --> 00:01:37.833 So we won't do this. 43 00:01:39.760 --> 00:01:41.580 And next up if unsaved 44 00:01:42.600 --> 00:01:44.810 then we're going to ask the user 45 00:01:44.810 --> 00:01:47.060 to confirm whether they really want to exit. 46 00:01:47.060 --> 00:01:48.580 So we'll say confirm 47 00:01:48.580 --> 00:01:52.580 is message box dot ask yes no. 48 00:01:52.580 --> 00:01:54.730 And we will pass in a message. 49 00:01:54.730 --> 00:01:56.030 You have unsaved changes, 50 00:01:56.030 --> 00:01:57.450 are you sure you want to quit? 51 00:01:57.450 --> 00:01:59.290 Then we will place an icon 52 00:01:59.290 --> 00:02:02.433 which is the string question. 53 00:02:03.500 --> 00:02:06.860 And finally we will have a title for the window, 54 00:02:06.860 --> 00:02:09.763 which is going to be confirm quit. 55 00:02:11.070 --> 00:02:13.920 So if not confirm, 56 00:02:13.920 --> 00:02:15.470 we're going to return. 57 00:02:15.470 --> 00:02:17.970 Otherwise we're gonna do root dot destroy. 58 00:02:17.970 --> 00:02:19.570 And that's gonna close our application. 59 00:02:19.570 --> 00:02:21.780 Now we do have to import message box. 60 00:02:21.780 --> 00:02:23.670 So go ahead and do that 61 00:02:23.670 --> 00:02:25.513 just from TK inter there. 62 00:02:27.240 --> 00:02:28.260 So what this is doing, 63 00:02:28.260 --> 00:02:29.230 just to review, 64 00:02:29.230 --> 00:02:31.500 is it's starting off as unsaved false. 65 00:02:31.500 --> 00:02:32.919 Which means that all the files 66 00:02:32.919 --> 00:02:34.940 currently open are saved. 67 00:02:34.940 --> 00:02:36.040 Then it's gonna cycle through 68 00:02:36.040 --> 00:02:38.280 all the tabs getting the text widget, 69 00:02:38.280 --> 00:02:39.113 getting the content, 70 00:02:39.113 --> 00:02:40.240 and checking it against 71 00:02:40.240 --> 00:02:42.470 our text contents dictionary. 72 00:02:42.470 --> 00:02:43.990 If any of them don't match, 73 00:02:43.990 --> 00:02:45.000 that means there is at least 74 00:02:45.000 --> 00:02:46.870 one file that is unsaved. 75 00:02:46.870 --> 00:02:48.160 So we set unsaved to true, 76 00:02:48.160 --> 00:02:49.320 we break, 77 00:02:49.320 --> 00:02:51.860 and then if unsaved is true, 78 00:02:51.860 --> 00:02:53.890 then we ask the user to confirm. 79 00:02:53.890 --> 00:02:55.481 If the confirm saying 80 00:02:55.481 --> 00:02:58.140 that they do not want to quit, 81 00:02:58.140 --> 00:02:59.630 that means they click no. 82 00:02:59.630 --> 00:03:01.650 Then confirm will be false. 83 00:03:01.650 --> 00:03:03.160 And then we will just return 84 00:03:03.160 --> 00:03:05.510 and not run root destroy. 85 00:03:05.510 --> 00:03:07.963 However if they do not say no, 86 00:03:07.963 --> 00:03:11.230 that means that not confirm will be false 87 00:03:11.230 --> 00:03:13.393 so we will then root destroy. 88 00:03:14.350 --> 00:03:15.880 Next up we do have to add 89 00:03:15.880 --> 00:03:17.240 a way of calling that function. 90 00:03:17.240 --> 00:03:18.073 And we're gonna do that 91 00:03:18.073 --> 00:03:20.340 with another menu item. 92 00:03:20.340 --> 00:03:23.080 So we are not going to run this whenever 93 00:03:23.080 --> 00:03:24.470 you press the close button. 94 00:03:24.470 --> 00:03:25.303 We're gonna run this when 95 00:03:25.303 --> 00:03:26.880 you use the menu to exit. 96 00:03:26.880 --> 00:03:30.010 So we'll say file menu at command. 97 00:03:30.010 --> 00:03:33.860 The label is going to be exit. 98 00:03:33.860 --> 00:03:36.233 And the command is confirm quit. 99 00:03:37.570 --> 00:03:38.610 Let's press play 100 00:03:39.810 --> 00:03:42.020 and we'll type an H 101 00:03:42.020 --> 00:03:44.830 and then I will press file exit. 102 00:03:44.830 --> 00:03:46.973 And this is what it asks me. 103 00:03:48.120 --> 00:03:49.160 You have unsaved changes, 104 00:03:49.160 --> 00:03:50.610 are you sure you want to quit? 105 00:03:50.610 --> 00:03:52.740 And I will say yes. 106 00:03:52.740 --> 00:03:54.070 So then it exits. 107 00:03:54.070 --> 00:03:56.900 But similarly if we type H 108 00:03:56.900 --> 00:03:58.620 and then we exit, 109 00:03:58.620 --> 00:04:00.050 and we say no, 110 00:04:00.050 --> 00:04:01.670 then that just returns and it doesn't 111 00:04:01.670 --> 00:04:03.433 actually terminate the application. 112 00:04:05.680 --> 00:04:07.150 Hopefully that makes sense. 113 00:04:07.150 --> 00:04:08.310 I am actually going 114 00:04:08.310 --> 00:04:10.100 to remove this print from here 115 00:04:10.960 --> 00:04:12.930 since we're printing the frame names there 116 00:04:12.930 --> 00:04:14.310 or the text area names there. 117 00:04:14.310 --> 00:04:15.380 We don't wanna do that. 118 00:04:15.380 --> 00:04:16.520 So that's it for this video. 119 00:04:16.520 --> 00:04:17.700 I hope you've learned something. 120 00:04:17.700 --> 00:04:18.670 Thank you for joining me 121 00:04:18.670 --> 00:04:20.320 and I'll see you in the next one.