WEBVTT 1 00:00:00.260 --> 00:00:01.780 Hi guys, and welcome back. 2 00:00:01.780 --> 00:00:03.860 In this short video, we're going to be adding 3 00:00:03.860 --> 00:00:06.123 shortcuts to our menus. 4 00:00:07.070 --> 00:00:11.180 Let's begin by going into the add-commands down here 5 00:00:11.180 --> 00:00:13.990 and add an accelerator. 6 00:00:13.990 --> 00:00:17.080 This is some text that will be shown beside the menu bar 7 00:00:17.080 --> 00:00:18.760 as if it were a shortcut. 8 00:00:18.760 --> 00:00:23.650 So we will say, Ctrl + N, for new. 9 00:00:23.650 --> 00:00:28.380 The accelerator for open is Ctrl + O. 10 00:00:28.380 --> 00:00:33.380 And the accelerator for save is Ctrl + S. 11 00:00:33.560 --> 00:00:36.060 Now a couple of things is that in Mac, 12 00:00:36.060 --> 00:00:39.060 this should be command instead of control 13 00:00:39.060 --> 00:00:41.060 but, that's a slightly more complicated problem 14 00:00:41.060 --> 00:00:43.440 to make it differ between platforms. 15 00:00:43.440 --> 00:00:46.163 So for now, we're gonna stick to control only. 16 00:00:47.340 --> 00:00:49.310 Now if we run this app, 17 00:00:49.310 --> 00:00:51.980 you will see that something a little bit weird happens. 18 00:00:51.980 --> 00:00:56.030 When we press Ctrl + S, the menu lights up. 19 00:00:56.030 --> 00:00:56.863 As if to tell us, 20 00:00:56.863 --> 00:01:00.190 hey you're using a shortcut for something here. 21 00:01:00.190 --> 00:01:02.230 But it doesn't actually do anything 22 00:01:02.230 --> 00:01:06.163 unless you click the menu and then press Ctrl + S. 23 00:01:07.720 --> 00:01:09.880 This may not happen in other platforms, 24 00:01:09.880 --> 00:01:13.220 but in Mac, this is how the accelerator behaves. 25 00:01:13.220 --> 00:01:15.740 So what you wanna make sure is that you have 26 00:01:15.740 --> 00:01:20.740 shortcuts also set for when you don't have the menu clicked. 27 00:01:21.190 --> 00:01:24.760 And the way to do that, is anywhere really down here, 28 00:01:24.760 --> 00:01:28.880 you can say root.bind, and now we're going to bind 29 00:01:28.880 --> 00:01:32.320 some sequence of key-presses to the root, 30 00:01:32.320 --> 00:01:34.240 which is our main ticket into window. 31 00:01:34.240 --> 00:01:35.570 And what that means is that, 32 00:01:35.570 --> 00:01:38.050 as long as that window is the one that's selected, 33 00:01:38.050 --> 00:01:41.210 the shortcuts will take effect. 34 00:01:41.210 --> 00:01:43.643 So the first one is going to be Control-n. 35 00:01:44.600 --> 00:01:48.680 And it's really important to surround your shortcut 36 00:01:48.680 --> 00:01:50.320 with these crocodile clips 37 00:01:50.320 --> 00:01:53.150 and to spell the modifier keys correctly. 38 00:01:53.150 --> 00:01:55.490 So control has a capital C, 39 00:01:55.490 --> 00:01:58.150 the dash means that this is an extra key-press 40 00:01:58.150 --> 00:01:59.710 that you have to type, 41 00:01:59.710 --> 00:02:02.030 N which is a lowercase N. 42 00:02:02.030 --> 00:02:04.040 So again Control-n is what we're 43 00:02:04.040 --> 00:02:06.220 going to be typing for the shortcut. 44 00:02:06.220 --> 00:02:11.220 And then we are going to do lambda event is create file. 45 00:02:12.300 --> 00:02:17.000 Now, important that we do lambda event and then pass in, 46 00:02:17.000 --> 00:02:19.450 the create file executing. 47 00:02:19.450 --> 00:02:22.430 That's because whenever you bind a shortcut, 48 00:02:22.430 --> 00:02:26.590 the second argument to bind, is a function 49 00:02:26.590 --> 00:02:30.340 that must have the signature of you know, 50 00:02:30.340 --> 00:02:32.830 something like this def event function, 51 00:02:32.830 --> 00:02:36.010 which takes an event and then does things. 52 00:02:36.010 --> 00:02:38.900 That is because you could use the event 53 00:02:38.900 --> 00:02:42.250 that has been generated inside the function. 54 00:02:42.250 --> 00:02:44.930 In this case, we don't want to use the event. 55 00:02:44.930 --> 00:02:47.570 So, what we do is we create a lambda function 56 00:02:47.570 --> 00:02:50.070 that takes in the event as an argument 57 00:02:50.070 --> 00:02:51.120 and then doesn't use it. 58 00:02:51.120 --> 00:02:54.470 And all it does is, it calls the create file, function. 59 00:02:54.470 --> 00:02:58.930 Remember that here we aren't defining a lambda function, 60 00:02:58.930 --> 00:03:02.810 we are not executing the create file function. 61 00:03:02.810 --> 00:03:05.200 Executing this function only happens 62 00:03:05.200 --> 00:03:08.040 when the lambda function itself gets executed, 63 00:03:08.040 --> 00:03:12.540 which will only happen when Control-n is pressed, okay? 64 00:03:12.540 --> 00:03:14.360 Next up, we're gonna do root.bind 65 00:03:14.360 --> 00:03:17.330 and now we're gonna do Ctrl + O, 66 00:03:17.330 --> 00:03:21.093 and once again lambda event, open file, 67 00:03:22.150 --> 00:03:27.113 and finally, root.bind, Ctrl + S. 68 00:03:28.390 --> 00:03:30.710 Make sure to include your crocodile clips by the way, 69 00:03:30.710 --> 00:03:33.483 and lambda event, save file. 70 00:03:34.690 --> 00:03:37.430 Alright, now we can run this app here 71 00:03:37.430 --> 00:03:39.690 and pressing Ctrl + S 72 00:03:39.690 --> 00:03:43.763 does indeed execute this bound sequence. 73 00:03:45.620 --> 00:03:49.840 This is how you can add shortcuts to a particular component, 74 00:03:49.840 --> 00:03:50.820 in this case we're adding them 75 00:03:50.820 --> 00:03:53.770 to the root of our application, the main window. 76 00:03:53.770 --> 00:03:57.173 And how you can use accelerators in your menus. 77 00:03:58.340 --> 00:04:02.200 Remember that depending on which widget is selected 78 00:04:02.200 --> 00:04:04.760 at the time that you press these keys, 79 00:04:04.760 --> 00:04:07.670 you can execute different functions. 80 00:04:07.670 --> 00:04:10.560 In this case, we are binding Control-n to the root, 81 00:04:10.560 --> 00:04:12.340 which means that it will always run, 82 00:04:12.340 --> 00:04:14.850 no matter which element we've got selected. 83 00:04:14.850 --> 00:04:17.280 But if you were to bind Control-n, 84 00:04:17.280 --> 00:04:22.150 say to the text field, this text area here, 85 00:04:22.150 --> 00:04:25.190 then the shortcut would only run if you have the 86 00:04:25.190 --> 00:04:28.650 text area selected, which may not happen at all times. 87 00:04:28.650 --> 00:04:31.300 So, this is how you can add shortcuts to different 88 00:04:31.300 --> 00:04:34.240 parts of your application if you want. 89 00:04:34.240 --> 00:04:35.600 Thanks for joining me in this video, 90 00:04:35.600 --> 00:04:36.750 I hope you've learned something, 91 00:04:36.750 --> 00:04:38.400 and I'll see you in the next one.