From 99711321a27e652c454eca99558094dcb6142ae9 Mon Sep 17 00:00:00 2001 From: Josh Orndorff Date: Sun, 19 Oct 2014 11:34:41 -0400 Subject: [PATCH 01/11] Updated Lesson 1 to make comments more clear, follow standard format, and fix typos. --- Lesson1-HelloWorld.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Lesson1-HelloWorld.py b/Lesson1-HelloWorld.py index 59ed19a..c4c91b2 100755 --- a/Lesson1-HelloWorld.py +++ b/Lesson1-HelloWorld.py @@ -10,24 +10,32 @@ #In wx we always sart by creating a wx.App. I like to call it "app". -app = wx.App(False) #False means error messages will print in the normal way +app = wx.App(False) #False means error messages will print in the normal way. -#Create a new frame (A frame is often casually called a window). -frame = wx.Frame(None, wx.ID_ANY, "Hello World") # None means it is top level. It has no parent frame. +# Create a new frame (A frame is often casually called a window). +frame = wx.Frame(None, wx.ID_ANY, "Hello World") + # None means it is top level. It has no parent frame. + # wx.ID_ANY is always the second argument. It is just a variable that = -1. + # The third argument is the window's title. # Show the frame. frame.Show(True) -#MainLoop makes the app listen for clicks, and other events. -#This is always the last line of a wxPython app. +# MainLoop makes the app listen for clicks, and other events. +# This is always the last line of a wxPython app. app.MainLoop() # ----------- Exercises Below ----------------- #1. Change the title of your Frame to something new. -#2. Add the argument size = (400,100) to the frome constructor on line 16. -#What does it do? +#2. Rename the variable bound to your frame to something like JoshsFrame. -#3. Add the argument pos=(1000,300) to the frome constructor on line 16. -#What does it do? \ No newline at end of file +#3. Add the argument size = (400,100) to the frame constructor on line 16. +# What does it do? + +#4. Add the argument pos=(1000,300) to the frame constructor on line 16. +# What does it do? + +#5. I've claimed that wx.ID_ANY is just a variable that equals -1. +# Confirm that this is true. (Hint: This is not hard; don't overthink it.) From 71cff96f4b237fabb79d82c94276e7032fabafd8 Mon Sep 17 00:00:00 2001 From: PF Date: Wed, 3 Dec 2014 08:33:01 -0500 Subject: [PATCH 02/11] Lesson1 exercise... with errors of toolbars. --- Lesson1-HelloWorld.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Lesson1-HelloWorld.py b/Lesson1-HelloWorld.py index 59ed19a..1c7808e 100755 --- a/Lesson1-HelloWorld.py +++ b/Lesson1-HelloWorld.py @@ -13,11 +13,14 @@ app = wx.App(False) #False means error messages will print in the normal way #Create a new frame (A frame is often casually called a window). -frame = wx.Frame(None, wx.ID_ANY, "Hello World") # None means it is top level. It has no parent frame. +window = wx.Frame(None, wx.ID_ANY, "Hello World", size = (1400,1000), pos = (1000,300), name = "dD") # None means it is top level. It has no parent frame. # Show the frame. -frame.Show(True) +tool = window.CreateToolBar() +tool.AddTool() +tool.Realize() +window.Show(True) #MainLoop makes the app listen for clicks, and other events. #This is always the last line of a wxPython app. app.MainLoop() @@ -26,8 +29,8 @@ #1. Change the title of your Frame to something new. -#2. Add the argument size = (400,100) to the frome constructor on line 16. +#2. Add the argument size = (400,100) to the frame constructor on line 16. #What does it do? -#3. Add the argument pos=(1000,300) to the frome constructor on line 16. +#3. Add the argument pos=(1000,300) to the frame constructor on line 16. #What does it do? \ No newline at end of file From 0e3aef1378c11909dbe5aba14e2a104de32ee3a8 Mon Sep 17 00:00:00 2001 From: Josh Orndorff Date: Fri, 5 Dec 2014 15:52:26 -0500 Subject: [PATCH 03/11] Restructured Exercises, Updated Readme, Made shell file for new Lesson 6 --- .gitignore | 2 +- EXERCISES.txt | 69 ----------- Exercises/EXERCISES.txt | 108 ++++++++++++++++++ .../tenButtonRaceSkeleton.py | 8 +- Lesson6-EventDetails.py | 41 +++++++ .../INSTRUCTIONS.txt | 18 --- README.txt | 29 ++++- 7 files changed, 181 insertions(+), 94 deletions(-) delete mode 100644 EXERCISES.txt create mode 100644 Exercises/EXERCISES.txt rename {Lesson6-TenButtonRace-Exercise => Exercises}/tenButtonRaceSkeleton.py (77%) create mode 100755 Lesson6-EventDetails.py delete mode 100644 Lesson6-TenButtonRace-Exercise/INSTRUCTIONS.txt diff --git a/.gitignore b/.gitignore index de4f8c1..53d5145 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .DS_Store -wxProjects.pdf +Exercises/Solutions diff --git a/EXERCISES.txt b/EXERCISES.txt deleted file mode 100644 index 6e74859..0000000 --- a/EXERCISES.txt +++ /dev/null @@ -1,69 +0,0 @@ -These exercises do not go in any particular order. Complete them whenever you know -the skills necessary. Occasionally they may require a minor skill that was not taught -directly in the lessons. You may be able to infer it, or you may have to search the -internet or ask for help. - -The basic task is stated right away, and ideas to enhance it are listed afterward. - - --------------------------------------- -#1. Write a graphical version of the height converter program. Use text boxes for -feet and inches, and a StaticText for centimeters. - -** Enhancements ** - -A. Update the output as the user types input with no submit button by using an -EVT_TEXT event (http://docs.wxwidgets.org/trunk/classwx_text_ctrl.html) - -B. Include a checkbox labeled "round the answer" to only display a few decimal places. - -C. Allow the program to convert from English to SI units or the other way around. - - - - --------------------------------------- -#2. Create a simple TicTacToe game where players take turns clicking where the X and -O should go. - -** Enhancements ** - -A. Make the program detect when one player has won the game. - -B. Make the player keep track of how many wins. -(eg. a xw.StaticText saying "player 1: 4 wins player 2: 17 wins") - -C. Allow the users to enter names and the win counter to refer to them by name. - -D. Allow the users to choose images as their markers instead of just X and O. - -E. Create one player mode where the human plays against the computer. - - --------------------------------------- -#3. Mastermind (http://en.wikipedia.org/wiki/Mastermind_%28board_game%29) - - --------------------------------------- -#4. Write a program with a text box for you to type text and a convert button that -converts your text to a secret code. A simple kind of substitution cipher just replaces -one letter with another (http://en.wikipedia.org/wiki/Substitution_cipher). - -** Enhancements ** - -A. Also include a decode button to change secret code back to normal language - -B. Use a fancier cipher than substitution cipher - -C. Have an "autodetect" feature like google translate that tries to determine whether -you have provided plain English or secret code text. You could do this by scanning the -text for common words like "is", "the", "a", etc. - --------------------------------------- -#5. I need suggestions here guys! What are some cool project ideas? - - - - --------------------------------------- -For more practice, recreate any of the terminal-based programs you wrote as GUI programs. diff --git a/Exercises/EXERCISES.txt b/Exercises/EXERCISES.txt new file mode 100644 index 0000000..3e13515 --- /dev/null +++ b/Exercises/EXERCISES.txt @@ -0,0 +1,108 @@ +These exercises do not go in any particular order. Complete them whenever you +know the skills necessary. Occasionally they may require a minor skill that was +not taught directly in the lessons. You may be able to infer it, or you may have +to search the internet or ask for help. + +It is best to try the exercises from scratch first. But if you are stuck, many +of the exercises have a skeleton file that you can reference or build on. + +-------------------------------------- +#1. Write a graphical version of the height converter program. Use text boxes +for feet and inches, and a StaticText for centimeters. + +** Enhancements ** + +A. Update the output as the user types input with no submit button by using an +EVT_TEXT event (http://docs.wxwidgets.org/trunk/classwx_text_ctrl.html) + +B. Include a checkbox labeled "Round" to only display a few decimal places. + +C. Allow the program to convert from English to SI units or the vice-versa. + + + + +-------------------------------------- +#2. Create a simple TicTacToe game where players take turns clicking where the X +and O should go. + +** Enhancements ** + +A. Make the program detect when one player has won the game. + +B. Make the player keep track of how many wins. +(eg. a xw.StaticText saying "player 1: 4 wins player 2: 17 wins") + +C. Allow the users to enter names and the win counter to refer to them by name. + +D. Allow the users to choose images as their markers instead of just X and O. + +E. Create one player mode where the human plays against the computer. + + +-------------------------------------- +#3. Mastermind (http://en.wikipedia.org/wiki/Mastermind_%28board_game%29) + + +-------------------------------------- +#4. Write a program with a text box for you to type text and a convert button +that converts your text to a secret code. A simple kind of substitution cipher +just replaces one letter with another. +(http://en.wikipedia.org/wiki/Substitution_cipher) + +** Enhancements ** + +A. Also include a decode button to change secret code back to normal language + +B. Use a fancier cipher than substitution cipher + +C. Have an "autodetect" feature like google translate that tries to determine +whether you have provided plain English or secret code text. You could do this +by scanning the text for common words like "is", "the", "a", etc. + +-------------------------------------- +#5. Make a graphical calculator like the one that comes with most OSs. It should +support addition, subtraction, multiplication, and division. + +A. Also support square root and squaring. + +B. Support trigonometry, and provide raio buttons to select degrees or radians. + +-------------------------------------- +#6. A simple game to see how quickly to user can click through ten buttons. + +When the user clicks the start button, the start button disappears and a new +button labeled "Button 1" appears somewhere in the window, and the program notes +the current time. When the user clicks Button 1, it disappears and Button 2 +appears somewhere in the window. When the user finally clicks Button 10, the +program tells the user how long it took them to click all ten buttons. + +In order to keep track of the time, you will need to import time and use the +time.time() function which returns the number of seconds since January 1st 1970. + +** Enhancements ** +A. Use random numbers to make the buttons appear at new locations each time. + +B. Show a running clock in the top corner of the window. + +C. Store the buttons in an array and create them with a for loop instead of +creating each one individually. + +D. Use a file to keep track of the best time ever for the game. + +E. Before starting the game, ask the user how many buttons to put in the race. +If you've also made enhancement D, consider keeping track of the high score for +each number of buttons. + +F. Instead of using a standard wx.Button, write your own class raceButton which +has a property called index or number. This will make it easier to test which +button was clicked in your event handler. + +-------------------------------------- +#7. I need suggestions here guys! What are some cool project ideas? + + + + +-------------------------------------- +For more practice, recreate any of the terminal-based programs you wrote as GUI programs. diff --git a/Lesson6-TenButtonRace-Exercise/tenButtonRaceSkeleton.py b/Exercises/tenButtonRaceSkeleton.py similarity index 77% rename from Lesson6-TenButtonRace-Exercise/tenButtonRaceSkeleton.py rename to Exercises/tenButtonRaceSkeleton.py index 59ecadd..20683d5 100644 --- a/Lesson6-TenButtonRace-Exercise/tenButtonRaceSkeleton.py +++ b/Exercises/tenButtonRaceSkeleton.py @@ -21,7 +21,11 @@ def OnStart(self, e): self.startTime = time.time() #Make Button One appear - #Other event handlers here + def OnAnyButton(self, e): + clickedButton = e.GetEventObject() + clickedIndex = buttons.index(clickedButton) + buttons[clickedIndex].Show(False) + buttons[clickedIndex + 1].Show(True) #Remember the last event handler needs to print the final time. @@ -31,4 +35,4 @@ def OnStart(self, e): app = wx.App(False) frame = TenButtonFrame(None) frame.Show() -app.MainLoop() \ No newline at end of file +app.MainLoop() diff --git a/Lesson6-EventDetails.py b/Lesson6-EventDetails.py new file mode 100755 index 0000000..c4c91b2 --- /dev/null +++ b/Lesson6-EventDetails.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +#You will experiment with this file by changing it and adding to it. +#But first just run it and see what it does. + +#Now that you have run the file, read through it and look at the exercises below. + +#We always have to import wx before we can use it. +import wx + + +#In wx we always sart by creating a wx.App. I like to call it "app". +app = wx.App(False) #False means error messages will print in the normal way. + +# Create a new frame (A frame is often casually called a window). +frame = wx.Frame(None, wx.ID_ANY, "Hello World") + # None means it is top level. It has no parent frame. + # wx.ID_ANY is always the second argument. It is just a variable that = -1. + # The third argument is the window's title. + +# Show the frame. +frame.Show(True) + +# MainLoop makes the app listen for clicks, and other events. +# This is always the last line of a wxPython app. +app.MainLoop() + +# ----------- Exercises Below ----------------- + +#1. Change the title of your Frame to something new. + +#2. Rename the variable bound to your frame to something like JoshsFrame. + +#3. Add the argument size = (400,100) to the frame constructor on line 16. +# What does it do? + +#4. Add the argument pos=(1000,300) to the frame constructor on line 16. +# What does it do? + +#5. I've claimed that wx.ID_ANY is just a variable that equals -1. +# Confirm that this is true. (Hint: This is not hard; don't overthink it.) diff --git a/Lesson6-TenButtonRace-Exercise/INSTRUCTIONS.txt b/Lesson6-TenButtonRace-Exercise/INSTRUCTIONS.txt deleted file mode 100644 index bb82424..0000000 --- a/Lesson6-TenButtonRace-Exercise/INSTRUCTIONS.txt +++ /dev/null @@ -1,18 +0,0 @@ -In this exercise you will use what you have learned in the first five lessons to make a program called the ten button race. - -This program should open a window with the title "Ten Button Race", and a button in the middle that says start. - -When the user clicks the start button, the start button disappears and a new button labeled "Button 1" appears somewhere in the window, and the program notes the current time. - -When the user clicks Button 1, it disappears and Button 2 appears somewhere in the window. This goes on and on until finally the user clicks Button 10, and the program tells the user how long it took them to click all ten buttons. - -In order to keep track of the time, you will need to import time and use the time.time() function which returns the number of seconds since January 1st 1970. - -If you need some help getting started, there is a skeleton program in this directory called tenButtonRaceSkeleton.py - - ** Optional Enhancements ** -1. Use random numbers to make the buttons appear at new locations each time. -2. Show a running clock in the top corner of the window. -3. Store the buttons in an array and create them with a for loop instead of creating each one individually. -4. Use a file to keep track of the best time ever for the game. -5. Before showing the window, use the terminal to ask the user how many buttons they want in their race. If you've also made enhancement #4, consider keeping track of the high score for all numbers of buttons. diff --git a/README.txt b/README.txt index e3729dc..b1665f2 100644 --- a/README.txt +++ b/README.txt @@ -1,7 +1,28 @@ -This tutorial will teach the basics of wxPython through example and experimentation. +This tutorial will teach the basics of wxPython by example and experimentation. +The tutorials are meant to be run, read, modified and completed. For best +results, change anything and everything you are curious about. Also -It is written by Josh Orndorff +learnWXPython is written by: +Josh Orndorff +admin@joshorndorff.com +www.joshorndorff.com + +Additional resources are available around the web: +http://wiki.wxpython.org +The official wxPython wiki. It has been down for troubleshooting for a while. +I'm beginning to wonder if it will ever come back up. -Additional resources are available at: -wiki.wxpython.org http://zetcode.com/wxpython/ +Another 3rd party tutorial that I've referenced in writing my own tutorial. It +is written in the more traditional style of providing examples and explaining +how they work. + +http://wxpython.org/docs/api/ +The official wxPython API documentation. It is relatively complete. + +http://docs.wxwidgets.org/ +The official WXWidgets documentation. wxWidgets is the c++ library on which +wxPython is based. It is developed upstream of wxPython, and this documentation +technically doesn't have anything to do with wxPython. However, wxPython is +written to be similar in structure to wxWidgets, and the wxWidgets documentation +is more complete and up-to-date than that of wxPython so it is of use. From b1f2c783fb9622bf3fe4b7f8d471d0be4966a7e8 Mon Sep 17 00:00:00 2001 From: Josh Orndorff Date: Sat, 6 Dec 2014 14:39:24 -0500 Subject: [PATCH 04/11] Filled in Lesson6. (Previously it had been a copy of another lesson) --- Lesson6-EventDetails.py | 82 ++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/Lesson6-EventDetails.py b/Lesson6-EventDetails.py index c4c91b2..142ee0c 100755 --- a/Lesson6-EventDetails.py +++ b/Lesson6-EventDetails.py @@ -1,41 +1,73 @@ #!/usr/bin/env python +import wx -#You will experiment with this file by changing it and adding to it. -#But first just run it and see what it does. +# By now you're good at binding event handler classes to events. But so far we +# have bound a new event handler function to each button. In this lesson we'll +# discover another way to do it. -#Now that you have run the file, read through it and look at the exercises below. +# It doesn't have to be called CoolerFrame. Since this is an example, I'll +# choose a name that reflects that. In general, you should give your classes +# meaningful names that reflect what they do. +class ExampleFrame(wx.Frame): + # Remember __init__ is the constructor function. + def __init__(self, parent): + + # As usual, we call the original wx.Frame constructor. + wx.Frame.__init__(self, parent, wx.ID_ANY, "Adding Numbers", size=(300, 120)) + + # Create a new panel that is a member of the frame + self.panel = wx.Panel(self) -#We always have to import wx before we can use it. -import wx + # Create the static text and buttons + self.question = wx.StaticText(self.panel, label="How many should I add to the total?", pos=(20, 20)) + self.result = wx.StaticText(self.panel, label="Total is: 0", pos=(40, 90)) + self.btn1 = wx.Button(self.panel, label="1", pos=(20,50)) + self.btn3 = wx.Button(self.panel, label="3", pos=(120, 50)) + + + # Bind the buttons to the event handler + self.btn1.Bind(wx.EVT_BUTTON, self.OnClick) + self.btn3.Bind(wx.EVT_BUTTON, self.OnClick) + + + # We will need a variable to keep track of the total. + # This does not have anything to do with wxPython, it is just normal OOP. + self.total = 0 + + # The event handler function + def OnClick(self, e): + buttonClicked = e.GetEventObject() + numToAdd = int(buttonClicked.GetLabel()) + self.total += numToAdd -#In wx we always sart by creating a wx.App. I like to call it "app". -app = wx.App(False) #False means error messages will print in the normal way. + self.result.SetLabel("Total is: {}".format(self.total)) -# Create a new frame (A frame is often casually called a window). -frame = wx.Frame(None, wx.ID_ANY, "Hello World") - # None means it is top level. It has no parent frame. - # wx.ID_ANY is always the second argument. It is just a variable that = -1. - # The third argument is the window's title. -# Show the frame. -frame.Show(True) -# MainLoop makes the app listen for clicks, and other events. -# This is always the last line of a wxPython app. +# ----------- Main Program Below ----------------- + +app = wx.App(False) + +frame = ExampleFrame(None) + +frame.Show() + app.MainLoop() -# ----------- Exercises Below ----------------- -#1. Change the title of your Frame to something new. -#2. Rename the variable bound to your frame to something like JoshsFrame. +# ----------- Exercises Below ----------------- -#3. Add the argument size = (400,100) to the frame constructor on line 16. -# What does it do? +#1. Add another button to add 5 to the total. +# Note, you don't need to add another event handler function. Isn't that nice? -#4. Add the argument pos=(1000,300) to the frame constructor on line 16. -# What does it do? +#2. Add a button that allows the user to reset the total. +# Is it best to make a new event handler in this case? -#5. I've claimed that wx.ID_ANY is just a variable that equals -1. -# Confirm that this is true. (Hint: This is not hard; don't overthink it.) +#3. It is not ideal to take the number we are adding from the button's label. +# It would be better to write our own button class that extends wx.Button. +# Our class will be almost identical, but it will have a variable that stores +# An int representing how much we should increase the total by. This allows +# us to change the label on the button to something like "Add Three" too. +# Write that button class and change the program to use it. From a965ddfd89cb9000322ab73a0238f1f9f22cddb2 Mon Sep 17 00:00:00 2001 From: Josh Orndorff Date: Sat, 6 Dec 2014 14:39:46 -0500 Subject: [PATCH 05/11] Minor updates to Lesson7 --- Lesson7-Images.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Lesson7-Images.py b/Lesson7-Images.py index 5603d0c..ff9db24 100644 --- a/Lesson7-Images.py +++ b/Lesson7-Images.py @@ -1,10 +1,12 @@ #!/usr/bin/env python import wx -# Here is a fairly simple lesson that demonstrates a fun and very useful feature of wx python. -# We will display some pictures in your programs. +# Here is a fairly simple lesson that demonstrates a fun and very useful feature +# of wx python. It will display some pictures in your programs. # As usual, you may run the program to see what it does. +# Just for fun, let's extend the wx.Panel class this time instead of wx.Frame. +# Notice the meaningful name. class ImagePanel(wx.Panel): def __init__(self, parent): @@ -32,7 +34,7 @@ def __init__(self, parent): # Create a regular old wx.Frame frame = wx.Frame(None, wx.ID_ANY, "Let's look at some images") -# Create a copy of our new custom Imagepanel and give it frame as a parent +# Create a copy of our new custom ImagePanel and give it frame as a parent panel = ImagePanel(frame) # Show the frame @@ -66,4 +68,4 @@ def __init__(self, parent): #3. Add a second picture from your own documents to the panel and display it beside the awesome baby. # Resize the frame as necessary to make sure both pictures fit properly. # -# Hint: If you don't remember how to resize a frame, it goes on the line that starts with frame = wx.Frame \ No newline at end of file +# Hint: If you don't remember how to resize a frame, it goes on the line that starts with frame = wx.Frame From a5ff74ed0456d57cafd4fd0edc4fe25f333b33f3 Mon Sep 17 00:00:00 2001 From: Josh Orndorff Date: Sat, 6 Dec 2014 14:47:02 -0500 Subject: [PATCH 06/11] Oops, forgot a few necessary comments in Lesson 6. --- Lesson6-EventDetails.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Lesson6-EventDetails.py b/Lesson6-EventDetails.py index 142ee0c..a1d5429 100755 --- a/Lesson6-EventDetails.py +++ b/Lesson6-EventDetails.py @@ -25,7 +25,7 @@ def __init__(self, parent): self.btn3 = wx.Button(self.panel, label="3", pos=(120, 50)) - # Bind the buttons to the event handler + # Bind the buttons to the SAME event handler function self.btn1.Bind(wx.EVT_BUTTON, self.OnClick) self.btn3.Bind(wx.EVT_BUTTON, self.OnClick) @@ -34,15 +34,22 @@ def __init__(self, parent): # This does not have anything to do with wxPython, it is just normal OOP. self.total = 0 - # The event handler function + # There is only one event handler function even though there are two buttons. def OnClick(self, e): + # We can determine which button is clicked by using GetEventObject() buttonClicked = e.GetEventObject() + + # Now that we know what button was clicked, we can read it's label and + # determine how much to add to the total. numToAdd = int(buttonClicked.GetLabel()) + # Finally we can do the adding and update the total. self.total += numToAdd - self.result.SetLabel("Total is: {}".format(self.total)) + # There are more useful methods like GetEventObject discussed in the + # Documentation at http://www.wxpython.org/docs/api/wx.Event-class.html + # In my experience GetEventObject is the most commonly used though. # ----------- Main Program Below ----------------- From 86c3d6b094304e2cbc2b1f5f0e074a5734fe8355 Mon Sep 17 00:00:00 2001 From: Josh Orndorff Date: Sat, 6 Dec 2014 14:56:00 -0500 Subject: [PATCH 07/11] Tiny update and typo correntiong to lesson8 --- Lesson8-TextBoxes.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Lesson8-TextBoxes.py b/Lesson8-TextBoxes.py index 1d06bd0..ced2a7b 100644 --- a/Lesson8-TextBoxes.py +++ b/Lesson8-TextBoxes.py @@ -42,7 +42,7 @@ def OnSubmitSimple(self, e): # Now we finally greet the user by displaying a message in our response wx.StaticText self.response.SetLabel("Nice to meet you, " + name) - def OnSubmitComplex(self, e): + def OnSubmitFancy(self, e): # This event handler is used instead after you change the binding on line 30. # This one makes sure the user entered a name. @@ -85,7 +85,7 @@ def OnSubmitComplex(self, e): #1. What is the purpose of line 12? Write you answer below. Writing helps clearly identify answers. #2. The submit button is currently bound to a simple even handler called OnSubmitSimple. -# But there is also a fancier event handler called OnSubmitComplex. +# But there is also a fancier event handler called OnSubmitFancy. # Change the binding on line 30 to use the fancier event handler and then run the program again. #3. The last argument on line 58 is wx.OK That tells the program to include an "OK" button. @@ -108,8 +108,8 @@ def OnSubmitComplex(self, e): #6. Good job on #5. It was a little easy. Now we need to actually call the user Dave. # wx.TextCtrl widgets have a method .SetValue("text here") -# Use this message in the complex event handler to call the anonymous user Dave. +# Use this message in the fancy event handler to call the anonymous user Dave. -#7. It would be nice it Dave didn't have to click the submit button after we named him. -# Even though OnSubmitComplex is an event handler, it can still be called just like any other function. -# After we name him Dave, call self.OnSubmitComplex(None) \ No newline at end of file +#7. It would be nice if Dave didn't have to click the submit button after we named him. +# Even though OnSubmitFancy is an event handler, it can still be called just like any other function. +# After we name him Dave, call self.OnSubmitFancy(None) From 4c3432a9ee96f3a5a0b37442e8115e332b76dba1 Mon Sep 17 00:00:00 2001 From: Josh Orndorff Date: Sun, 7 Dec 2014 15:27:39 -0500 Subject: [PATCH 08/11] Finished Lesson10 --- Lesson10-BoxSizers.py | 86 +++++++++++++++++++++++++++++++++++++++++++ Lesson10-Sizers.py | 0 2 files changed, 86 insertions(+) create mode 100644 Lesson10-BoxSizers.py delete mode 100644 Lesson10-Sizers.py diff --git a/Lesson10-BoxSizers.py b/Lesson10-BoxSizers.py new file mode 100644 index 0000000..41f46f7 --- /dev/null +++ b/Lesson10-BoxSizers.py @@ -0,0 +1,86 @@ +#! /usr/bin/env python + +# By now you've probably been frustrated trying to get your buttons, static text +# labels and other controls (or "windows" as they're known in wx) to line up +# properly. Luckily wx has a native solution to that problem called sizers. +# There are several kinds of sizers, but this lesson will focus on BoxSizers +# because I find them to be the most useful and commonly used. (And I'm not good at using the other kinds :-/ ) + +import wx + +class SizerFrame(wx.Frame): + def __init__(self, parent, title): + wx.Frame.__init__(self, parent, wx.ID_ANY, title, size=(200, 150)) + + self.panel = wx.Panel(self) + + # I'll create a few buttons and static texts. + # These lines should be familiar to you by now. + # Notice I am NOT specifying positions with pos=(x, y) + self.question = wx.StaticText(self.panel, label = "Choose a fruit?") + self.btnApple = wx.Button(self.panel, label = "Apple") + self.btnOrange = wx.Button(self.panel, label = "Orange") + + + # Now we create the BoxSizer object that will layout our windows + self.sizer = wx.BoxSizer(wx.VERTICAL) + + # We'll add each of the windows to the sizer + self.sizer.Add(self.question, 1, wx.ALL | wx.EXPAND, 3) + self.sizer.Add(self.btnApple, 2, wx.ALL | wx.EXPAND, 3) + self.sizer.Add(self.btnOrange, 2, wx.ALL | wx.EXPAND, 3) + # The first argument is clearly the window that you want to add. + # Next is the relative amount of space each window will occupy. + # Currently, the StaticText takes 1/5 of the vertical space and each Button takes 2/5 of the vertical space. + # The third argument is a series of flags that affect display. These are discussed in the exercises. + # Finally, we specify the border space in pixels for the window. + + + # And finally make the sizer work for our panel + self.panel.SetSizerAndFit(self.sizer) + + self.Show() + + +# ----------- Main Program Below --------------- +app = wx.App(False) +# This time I'm passing parent and title to my new SizerFrame. +frame = SizerFrame(None, "Our first sizer") +app.MainLoop() + +# ----------- Exercises Below ----------------- + +#0. Remember that these exercises are meant to guide you, but do not necessarily +# Cover everything. You are encouraged to change anything and everything you +# are curious about even if it is not mentioned in the exercises. + +#1. Create another Button or StaticText and add it to the sizer. + +#2. Change wx.VERTICAL to wx.HORIZONTAL when you make the sizer on line 26. + +#3. Change the order in which you create the buttons. Does it affect the order +# in which they are shown? + +#4. The flags I've used are wx.ALL and wx.EXPAND. +# Remove wx.EXPAND from one of the buttons and see what happens. Be sure to +# resize the frame to really explore this. Now replace wx.EXPAND with +# wx.SHAPED and see what happens.More information aobut these flags is at: +# http://www.wxpython.org/docs/api/wx.Sizer-class.html + +#5. This example uses only one vertical sizer. But it is common to nest one +# sizers inside of each other. Create a second sizer to arrange the buttons +# horizontally and insert it inside the first sizer. +# |--------------------------| +# | Choose a fruit | +# | | +# | |-------| |--------| | +# | | Apple | | Orange | | +# | |-------| |--------| | +# |--------------------------| + +#6. We can also SHOW the box around the BoxSizer like we did with StaticBox. +# Create a StaticBox before your second sizer. +# Change BoxSizer to StaticBoxSizer for your second sizer. +# Then insert your StaticBox as the first argument. + + diff --git a/Lesson10-Sizers.py b/Lesson10-Sizers.py deleted file mode 100644 index e69de29..0000000 From 96d3bea3da2d03195f999a49cfdd39a00389ba8a Mon Sep 17 00:00:00 2001 From: Josh Orndorff Date: Sun, 7 Dec 2014 15:31:07 -0500 Subject: [PATCH 09/11] Fixed typo in Lesson11 filename --- Lesson11-RadioButtonr.py => Lesson11-RadioButtons.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Lesson11-RadioButtonr.py => Lesson11-RadioButtons.py (100%) diff --git a/Lesson11-RadioButtonr.py b/Lesson11-RadioButtons.py similarity index 100% rename from Lesson11-RadioButtonr.py rename to Lesson11-RadioButtons.py From 6e90be06b8f7b8d64a55d4b3b6098a30bef33f2b Mon Sep 17 00:00:00 2001 From: Josh Orndorff Date: Sun, 7 Dec 2014 15:31:56 -0500 Subject: [PATCH 10/11] Added two more shell files and a todo.txt listing things to add in the future. --- Lesson13-StatusBar.py | 0 Lesson14-Timers.py | 0 todo.txt | 4 ++++ 3 files changed, 4 insertions(+) create mode 100644 Lesson13-StatusBar.py create mode 100644 Lesson14-Timers.py create mode 100644 todo.txt diff --git a/Lesson13-StatusBar.py b/Lesson13-StatusBar.py new file mode 100644 index 0000000..e69de29 diff --git a/Lesson14-Timers.py b/Lesson14-Timers.py new file mode 100644 index 0000000..e69de29 diff --git a/todo.txt b/todo.txt new file mode 100644 index 0000000..3e5abe4 --- /dev/null +++ b/todo.txt @@ -0,0 +1,4 @@ +Image buttons +Static Box +Other sizers + From 156fe919ff6087edf8673681361430d310a2cb6c Mon Sep 17 00:00:00 2001 From: PF Date: Sun, 7 Dec 2014 16:46:16 -0500 Subject: [PATCH 11/11] modify alien + delete mistaking senteces --- Lesson1-HelloWorld.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Lesson1-HelloWorld.py b/Lesson1-HelloWorld.py index 1c7808e..d31efd2 100755 --- a/Lesson1-HelloWorld.py +++ b/Lesson1-HelloWorld.py @@ -13,13 +13,9 @@ app = wx.App(False) #False means error messages will print in the normal way #Create a new frame (A frame is often casually called a window). -window = wx.Frame(None, wx.ID_ANY, "Hello World", size = (1400,1000), pos = (1000,300), name = "dD") # None means it is top level. It has no parent frame. +window = wx.Frame(None, wx.ID_ANY, "Hello Alien", size = (1400,1000), pos = (1000,300), name = "a name") # None means it is top level. It has no parent frame. # Show the frame. - -tool = window.CreateToolBar() -tool.AddTool() -tool.Realize() window.Show(True) #MainLoop makes the app listen for clicks, and other events. #This is always the last line of a wxPython app.