WEBVTT 1 00:00:00.090 --> 00:00:01.730 line:15% Hi guys and welcome back. 2 00:00:01.730 --> 00:00:03.030 line:15% In this video we're going to be 3 00:00:03.030 --> 00:00:06.509 closing individual tabs using a shortcut. 4 00:00:06.509 --> 00:00:09.323 So let's go ahead and start with that. 5 00:00:10.380 --> 00:00:11.520 We're gonna create a function 6 00:00:11.520 --> 00:00:13.810 called close current tab. 7 00:00:13.810 --> 00:00:17.010 So I'm gonna go ahead and implement it here. 8 00:00:17.010 --> 00:00:19.610 Close current tab. 9 00:00:19.610 --> 00:00:22.710 And what it's gonna do, is it's gonna get the current tab. 10 00:00:22.710 --> 00:00:26.910 So we'll say current is get text widget. 11 00:00:26.910 --> 00:00:31.253 And then we'll do notebook, don't forget current. 12 00:00:32.130 --> 00:00:34.760 Now let's add a shortcut for this. 13 00:00:34.760 --> 00:00:36.540 So in the file menu, 14 00:00:36.540 --> 00:00:38.350 we're gonna add a shortcut for it 15 00:00:38.350 --> 00:00:42.840 so just before exit, we'll do file menu and command. 16 00:00:42.840 --> 00:00:46.780 The label is gonna be close tab and 17 00:00:46.780 --> 00:00:50.050 the command is gonna be close current tab. 18 00:00:50.050 --> 00:00:54.943 And the accelerator is gonna be Ctrl plus Q. 19 00:00:56.430 --> 00:00:58.790 Next up, we're also gonna add a shortcut. 20 00:00:58.790 --> 00:01:03.120 So we'll do root.bind control-q 21 00:01:04.810 --> 00:01:09.253 and the event is close current tab. 22 00:01:10.610 --> 00:01:14.360 All right, let's run the app and see what happens. 23 00:01:14.360 --> 00:01:18.330 So control Q and the tab is closed. 24 00:01:18.330 --> 00:01:20.473 Create new and it comes back. 25 00:01:21.340 --> 00:01:23.680 Of course if you do, create multiple tabs, 26 00:01:23.680 --> 00:01:25.530 you can then close them individually. 27 00:01:26.930 --> 00:01:31.930 Now if the tab is unsaved, you want to be able 28 00:01:32.270 --> 00:01:35.280 to tell the user that they have unsaved changes 29 00:01:35.280 --> 00:01:37.664 and they probably shouldn't close 30 00:01:37.664 --> 00:01:38.980 the tab until they save them. 31 00:01:38.980 --> 00:01:40.680 So let's do that first of all. 32 00:01:40.680 --> 00:01:45.680 We would say, if the current tab is unsaved, 33 00:01:45.885 --> 00:01:50.885 and the confirm close then we will do notebook forget. 34 00:01:51.860 --> 00:01:54.010 So two functions here that don't exist 35 00:01:54.010 --> 00:01:55.840 that we have to implement. 36 00:01:55.840 --> 00:01:59.260 So let's go ahead and do current tab unsaved. 37 00:01:59.260 --> 00:02:00.160 What this is going to do, 38 00:02:00.160 --> 00:02:03.570 is it's going to get the current text widget. 39 00:02:03.570 --> 00:02:06.330 So we'll say text widget is get text widget. 40 00:02:06.330 --> 00:02:07.880 Then it's gonna get the content 41 00:02:09.037 --> 00:02:10.770 and finally its going to see whether 42 00:02:10.770 --> 00:02:13.880 it matches the stuff in the dictionary. 43 00:02:13.880 --> 00:02:16.610 So we'll say return hash of content 44 00:02:17.600 --> 00:02:22.600 is different from text contents str text widget. 45 00:02:24.304 --> 00:02:26.270 So if this returns true, 46 00:02:26.270 --> 00:02:28.123 this tab is unsaved. 47 00:02:29.040 --> 00:02:32.870 Next up, let's do a confirm close. 48 00:02:32.870 --> 00:02:36.807 So this is gonna do return messagebox.askyesno 49 00:02:36.807 --> 00:02:39.950 and it's going to contain the stuff 50 00:02:39.950 --> 00:02:41.473 that we have down here. 51 00:02:42.897 --> 00:02:45.290 I just gonna make it a little bit more generic 52 00:02:45.290 --> 00:02:49.950 and by saying that you've got unsaved changes 53 00:02:49.950 --> 00:02:52.863 and call it closed. 54 00:02:54.100 --> 00:02:56.930 All right, so what we've got here now is this logic. 55 00:02:56.930 --> 00:02:58.950 If the current tab is unsaved, 56 00:02:58.950 --> 00:03:01.160 which means we need to save it, 57 00:03:01.160 --> 00:03:04.310 and we confirm that we do want to close 58 00:03:04.310 --> 00:03:07.160 then we will forget the current tab. 59 00:03:07.160 --> 00:03:09.600 But there's a piece of logic missing. 60 00:03:09.600 --> 00:03:13.270 What if the current tab is not unsaved. 61 00:03:13.270 --> 00:03:16.220 What if the current tab is actually all right to be closed? 62 00:03:16.220 --> 00:03:20.630 Well at no point in this function would we close that tab. 63 00:03:20.630 --> 00:03:22.483 So we want to do the following, 64 00:03:23.899 --> 00:03:26.920 which is that we wanna invert this confirm close. 65 00:03:26.920 --> 00:03:31.310 The current tab is unsaved and not confirm close. 66 00:03:31.310 --> 00:03:33.990 Then we will just return. 67 00:03:33.990 --> 00:03:36.743 Otherwise we will forget the tab. 68 00:03:37.770 --> 00:03:39.950 Okay, it's important to test your application 69 00:03:39.950 --> 00:03:42.820 and make sure to verify the different scenarios 70 00:03:42.820 --> 00:03:45.700 to make sure that you don't miss out on any important logic. 71 00:03:45.700 --> 00:03:47.930 Like we almost did here. 72 00:03:47.930 --> 00:03:50.350 Next up, we wanna fix another problem. 73 00:03:50.350 --> 00:03:52.871 Which is that if I run the application 74 00:03:52.871 --> 00:03:54.830 and I run this tab, you can see that 75 00:03:54.830 --> 00:03:57.440 we've got a pretty ugly window here. 76 00:03:57.440 --> 00:03:59.520 So let's create an empty file 77 00:03:59.520 --> 00:04:01.423 if we don't have any left. 78 00:04:03.173 --> 00:04:08.173 So we'll say, if the len of notebook.tabs is 1. 79 00:04:10.150 --> 00:04:12.830 That is before we forget the current tab, 80 00:04:12.830 --> 00:04:15.493 then we're going to create a new one. 81 00:04:17.010 --> 00:04:19.580 All right, let's press play now. 82 00:04:19.580 --> 00:04:22.908 And you can see that as we press control Q 83 00:04:22.908 --> 00:04:24.701 which I am doing, promise. 84 00:04:24.701 --> 00:04:26.469 Then this stays there. 85 00:04:26.469 --> 00:04:29.543 That's because we are creating new ones. 86 00:04:30.540 --> 00:04:32.430 Okay, let's press control Q now 87 00:04:32.430 --> 00:04:34.690 and you can see that we get this message box. 88 00:04:34.690 --> 00:04:37.696 You have unsaved changes and you should go in and close. 89 00:04:37.696 --> 00:04:39.960 And we're gonna say no and it stays. 90 00:04:39.960 --> 00:04:42.960 We're gonna press yes and it goes away and we get a new one. 91 00:04:44.520 --> 00:04:47.120 Going down to the confirm quit. 92 00:04:47.120 --> 00:04:48.430 Here what we're gonna do is 93 00:04:48.430 --> 00:04:50.900 we are going to use our function. 94 00:04:50.900 --> 00:04:55.900 So we say, not and not confirm close. 95 00:04:56.580 --> 00:04:58.680 Then we will return. 96 00:04:58.680 --> 00:05:01.300 So just making use of those confirm close function 97 00:05:01.300 --> 00:05:03.950 that we defined above made it a bit more generic 98 00:05:03.950 --> 00:05:06.140 so that we could use it in both closing a tab, 99 00:05:06.140 --> 00:05:07.650 and closing the application 100 00:05:07.650 --> 00:05:09.730 and that makes our code a little bit cleaner, 101 00:05:09.730 --> 00:05:11.910 a little bit shorter as well. 102 00:05:11.910 --> 00:05:13.520 That's everything for this video. 103 00:05:13.520 --> 00:05:14.820 Thank you for joining me. 104 00:05:14.820 --> 00:05:16.686 I hope you've learned something 105 00:05:16.686 --> 00:05:17.763 and I'll see you in the next one.