WEBVTT 1 00:00:00.210 --> 00:00:02.020 Hi guys, and welcome back. 2 00:00:02.020 --> 00:00:04.600 In this video, we're going to be adding a menu bar 3 00:00:04.600 --> 00:00:05.760 to our application, 4 00:00:05.760 --> 00:00:09.050 so that we can create new files using it. 5 00:00:09.050 --> 00:00:10.480 Let's get started! 6 00:00:10.480 --> 00:00:14.020 First of all, we are going to add a couple of things here 7 00:00:14.020 --> 00:00:15.860 to our root. 8 00:00:15.860 --> 00:00:18.540 A title, which we don't need for a menu, 9 00:00:18.540 --> 00:00:19.940 but nonetheless, it looks good. 10 00:00:19.940 --> 00:00:22.840 And then we're going to add an option, 11 00:00:22.840 --> 00:00:26.500 which is a slightly confusing part of TkInter, 12 00:00:26.500 --> 00:00:29.060 but essentially through these options, 13 00:00:29.060 --> 00:00:33.050 you can change the behaviour of certain elements 14 00:00:33.050 --> 00:00:35.980 and certain things in different operating systems. 15 00:00:35.980 --> 00:00:38.140 Really, you have to read the documentation 16 00:00:38.140 --> 00:00:41.580 for each option in order to understand what they do, 17 00:00:41.580 --> 00:00:44.820 but when you add an option, then TkInter 18 00:00:44.820 --> 00:00:48.841 will communicate that to TK and TCL, 19 00:00:48.841 --> 00:00:51.470 and the UI will behave differently. 20 00:00:51.470 --> 00:00:56.270 In this case, we're going to add a tearOff of False. 21 00:00:56.270 --> 00:00:59.050 And what that means is, in some operating systems, 22 00:00:59.050 --> 00:01:02.900 the menus we'll be adding can be torn off 23 00:01:02.900 --> 00:01:07.380 from the interface and placed elsewhere in your screen. 24 00:01:07.380 --> 00:01:10.340 And we want to prevent that, so the menus that we add 25 00:01:10.340 --> 00:01:12.830 to our application, you won't be able to like 26 00:01:12.830 --> 00:01:14.930 separate from the application window. 27 00:01:14.930 --> 00:01:16.430 That's what this option means. 28 00:01:17.610 --> 00:01:21.780 Now underneath main here, we're going to add a menu bar. 29 00:01:21.780 --> 00:01:24.840 And that is just a tk.Menu. 30 00:01:24.840 --> 00:01:27.150 It's not a ttk menu, 31 00:01:27.150 --> 00:01:29.530 because it's not really a themable widget, 32 00:01:29.530 --> 00:01:30.780 it's just a menu bar. 33 00:01:30.780 --> 00:01:33.110 And again, this menu bar will be different 34 00:01:33.110 --> 00:01:35.530 in each operating system, it will look different. 35 00:01:35.530 --> 00:01:37.910 For example, in Mac, it will show up here 36 00:01:37.910 --> 00:01:39.530 on the top of the screen. 37 00:01:39.530 --> 00:01:42.820 In Windows, it'll show up as like File, and so on, 38 00:01:42.820 --> 00:01:46.440 and in Linux, it'll differ depending on the distribution. 39 00:01:46.440 --> 00:01:49.040 Then we're going to say root.config(menu = menubar). 40 00:01:51.840 --> 00:01:53.900 And let's remove those spaces there. 41 00:01:53.900 --> 00:01:56.620 And what this means is we have created a menu bar, 42 00:01:56.620 --> 00:01:59.640 which is, you know, PyCharm, File, Edit, View, 43 00:01:59.640 --> 00:02:01.980 all of these things are one menu bar. 44 00:02:01.980 --> 00:02:04.880 Now we have to add a particular cascade, 45 00:02:04.880 --> 00:02:07.160 or a particular drop-down of elements. 46 00:02:07.160 --> 00:02:09.000 So we're going to add one called File. 47 00:02:09.000 --> 00:02:12.660 So we'll say file_menu is another tk.menu, 48 00:02:12.660 --> 00:02:16.322 but now the parent is the menubar, 49 00:02:16.322 --> 00:02:18.587 and we'll say menubar.add_cascade 50 00:02:20.010 --> 00:02:21.980 and we'll pass in the file_menu, 51 00:02:21.980 --> 00:02:24.810 and we'll pass in a label called "File." 52 00:02:24.810 --> 00:02:28.100 And what that does is, it creates a file element 53 00:02:28.100 --> 00:02:30.550 inside of the menu, and tells the menubar, 54 00:02:30.550 --> 00:02:33.850 which is our main menu, to add it as a drop-down 55 00:02:33.850 --> 00:02:35.380 that you can click. 56 00:02:35.380 --> 00:02:39.060 Then, in the file_menu, we're going to add a command, 57 00:02:39.060 --> 00:02:41.870 not a cascade, and the command 58 00:02:41.870 --> 00:02:44.020 is gonna have a label of "New", 59 00:02:44.020 --> 00:02:47.530 and the command is going to be create_file. 60 00:02:47.530 --> 00:02:49.940 Make sure to not call the create_file function there, 61 00:02:49.940 --> 00:02:52.270 just like we had with buttons earlier on, 62 00:02:52.270 --> 00:02:55.380 this command is just a callable function. 63 00:02:55.380 --> 00:02:57.540 Oh, by the way, we do have to specify that this 64 00:02:57.540 --> 00:03:01.050 is a menu item that we want to add to the menu bar, 65 00:03:01.050 --> 00:03:02.870 and not an element that we want to add 66 00:03:02.870 --> 00:03:05.950 to an already existing cascade inside an item. 67 00:03:05.950 --> 00:03:09.173 Just make sure to pass in the named argument there. 68 00:03:10.480 --> 00:03:12.420 Running the app now gives you the same thing 69 00:03:12.420 --> 00:03:14.420 that we had before, but now you can see at the top, 70 00:03:14.420 --> 00:03:17.230 we've got a File menu that we didn't have before, 71 00:03:17.230 --> 00:03:19.120 and then here we have New. 72 00:03:19.120 --> 00:03:23.425 Whenever we click New, we run the create_file function, 73 00:03:23.425 --> 00:03:26.120 which again creates a text area, 74 00:03:26.120 --> 00:03:29.680 makes sure that it fills everything inside the notebook, 75 00:03:29.680 --> 00:03:32.860 adds it to the notebook itself, which creates the new tab, 76 00:03:32.860 --> 00:03:35.380 and then selects it, which places focus 77 00:03:35.380 --> 00:03:37.600 on the newly created element. 78 00:03:37.600 --> 00:03:40.470 Again, if we type here, "Hello world," 79 00:03:40.470 --> 00:03:42.470 and then we hit a new one, 80 00:03:42.470 --> 00:03:43.880 this one is still here in the middle, 81 00:03:43.880 --> 00:03:47.380 this one is now the end, and it's the one that's selected. 82 00:03:47.380 --> 00:03:51.070 All right, this is how you create menus in TkInter. 83 00:03:51.070 --> 00:03:53.160 First of all, you create a menu bar, 84 00:03:53.160 --> 00:03:55.930 which is the top level that contains 85 00:03:55.930 --> 00:03:58.640 all of the different menu cascades, 86 00:03:58.640 --> 00:04:00.840 and then you create different cascades, 87 00:04:00.840 --> 00:04:03.533 one for each group of actions. 88 00:04:04.410 --> 00:04:05.490 That's everything for this video. 89 00:04:05.490 --> 00:04:06.570 Thank you for joining me. 90 00:04:06.570 --> 00:04:08.103 I'll see you in the next one!