1 00:00:00,000 --> 00:00:00,320 2 00:00:00,320 --> 00:00:03,260 SPEAKER 1:Let's write a program with a graphical user interface that includes 3 00:00:03,260 --> 00:00:05,180 a window and a button there in. 4 00:00:05,180 --> 00:00:06,540 Let's get started. 5 00:00:06,540 --> 00:00:10,690 >> First, let me go ahead and declare a g window, calling the variable window. 6 00:00:10,690 --> 00:00:13,510 And assign to it, the return value of new g window. 7 00:00:13,510 --> 00:00:15,230 Which will instantiate our window. 8 00:00:15,230 --> 00:00:19,530 And then we specify a width of 320 pixels, and the height of 240 pixel. 9 00:00:19,530 --> 00:00:22,160 Somewhat arbitrarily, but small enough to fit on the screen. 10 00:00:22,160 --> 00:00:24,060 Let's now instantiate a g button. 11 00:00:24,060 --> 00:00:27,530 Not only with a label that the user will see, but also with a unique 12 00:00:27,530 --> 00:00:28,150 identifier. 13 00:00:28,150 --> 00:00:32,560 A so-called action command, that will indeed uniquely identify that button. 14 00:00:32,560 --> 00:00:37,680 g button, button, gets the return value of new g button. 15 00:00:37,680 --> 00:00:40,940 And it's a label shall be, quite simply, button. 16 00:00:40,940 --> 00:00:47,380 And then let's set action command, passing in that button and a unique, 17 00:00:47,380 --> 00:00:49,870 and some what arbitrary word, click. 18 00:00:49,870 --> 00:00:55,440 Let's now add the button to a region of the window, bypassing in window, 19 00:00:55,440 --> 00:00:59,630 and the button, and an identifier for the southern region of the window-- 20 00:00:59,630 --> 00:01:03,020 that according to the Stanford portable library, is the lower portion 21 00:01:03,020 --> 00:01:04,170 of the screen. 22 00:01:04,170 --> 00:01:07,000 >> And now let's deliberately induce an infinite loop. 23 00:01:07,000 --> 00:01:10,250 24 00:01:10,250 --> 00:01:12,600 Inside of this loop we're going to listen for two things. 25 00:01:12,600 --> 00:01:15,680 One, whether the user has chosen to close the window. 26 00:01:15,680 --> 00:01:19,010 And two, whether or not the user has clicked on that specific button. 27 00:01:19,010 --> 00:01:22,960 First, we're going to declare a g action event, calling 28 00:01:22,960 --> 00:01:24,190 the variable event. 29 00:01:24,190 --> 00:01:31,480 And assign to it, the return value of wait for event, action event. 30 00:01:31,480 --> 00:01:34,460 >> And now let's check if that event is the closing of a window. 31 00:01:34,460 --> 00:01:40,040 If, get, event, type, passing an event, equals, equals, window 32 00:01:40,040 --> 00:01:40,955 underscore closed. 33 00:01:40,955 --> 00:01:44,630 A special constant declared in the Stanford portable library that 34 00:01:44,630 --> 00:01:47,170 represents exactly that, the closing of a window. 35 00:01:47,170 --> 00:01:49,820 Then I'm going to break out of this otherwise infinite loop, so that we 36 00:01:49,820 --> 00:01:51,770 reach the end of the function. 37 00:01:51,770 --> 00:01:58,460 Otherwise, I'm going to check if string compare of get, action, 38 00:01:58,460 --> 00:02:02,990 command, passing an event, comma, quote unquote, click-- that unique 39 00:02:02,990 --> 00:02:04,570 identifier from before-- 40 00:02:04,570 --> 00:02:06,360 equals, equals, zero. 41 00:02:06,360 --> 00:02:13,290 Then, I'm going to print out something arbitrary, like button was clicked. 42 00:02:13,290 --> 00:02:17,220 >> In other words, if upon hearing that a button was clicked, and that event 43 00:02:17,220 --> 00:02:20,690 indicates that the button was clicked was the one with the unique identifier 44 00:02:20,690 --> 00:02:24,180 of quote unquote click, then I want to report as much on the screen. 45 00:02:24,180 --> 00:02:25,780 Now in this case, I only have one button. 46 00:02:25,780 --> 00:02:29,040 But just in case my application had multiple buttons, this technique would 47 00:02:29,040 --> 00:02:32,130 allow me to distinguish among those several buttons. 48 00:02:32,130 --> 00:02:36,465 Outside of this loop, now, I'm going to proceed to close g window, passing 49 00:02:36,465 --> 00:02:40,280 in window as its argument, and then return zero. 50 00:02:40,280 --> 00:02:43,930 >> Let's now save, compile, and run this program. 51 00:02:43,930 --> 00:02:47,690 Make button, dot slash button. 52 00:02:47,690 --> 00:02:49,640 There's my window, there's the button. 53 00:02:49,640 --> 00:02:51,720 Let's go ahead and click on the button. 54 00:02:51,720 --> 00:02:52,810 And the button was clicked. 55 00:02:52,810 --> 00:02:54,400 Let's click on that again. 56 00:02:54,400 --> 00:02:55,310 Button was clicked. 57 00:02:55,310 --> 00:02:59,160 >> Let's now click on the x in the top right hand corner to close the window, 58 00:02:59,160 --> 00:03:01,400 and we break out of that loop and we return zero. 59 00:03:01,400 --> 00:03:03,638