What is wxGlade?
wxGlade is a GUI designer for wxPython. It makes it very easy to create a user interface using drag and drop techniques without writing a single line of code. Anyone who has ever written wxPython code will understand that you can save a lot of time using a GUI designer such as wxGlade. wxGlade also generates C++, Perl and XRC (wxWidgets' XML resources) code. More information about this great piece of software here.Lets start
I assume you had no problems installing wxPython and/or wxGlade, so now it's time to start wxGlade. Click on the button "Add a frame" in the wxGlade's main window and choose for wxFrame, as a result a Frame will be created. wxGlade automatically adds a sizer to the frame, a sizer is a container for our widgets like buttons, etcetera.
When you click on a widget (e.g. text_ctrl_1) in your frame, its properties will be displayed in the "Properties" window. Now edit the properties of you widgets like I did and you should have something like this:
Generating Python code is also easy, just keep some things in mind. Click on "Application" in the "Tree" window and the "Properties" window appears. Now, look at the picture below and I'll explain it to you.
Open this file in your favorite editor and examine the code. It should run now, your wxPython program without having to write a single line of code. Isn't it amazing?
But if you click the buttons, nothing happens. That's because there are no events in this program yet. I will explain below how to add events to this little application.
Adding events
Adding events should be done "by hand", wxGlade cannot do this because it is a GUI builder and not an IDE. So you will need an editor or IDE, such as SPE , to add events and other lines of code. It is VERY important to add your code outside the # begin wxGlade # end wxGlade section! That is because whenever wxGlade (re)generates a new version (because you have added another button, for example) it leaves your code outside the # begin wxGlade # end wxGlade section intact. And that's why you have to uncheck "Overwrite existing sources" in the properties window!Now fire up your editor and look for the # end wxGlade line in the def __init__ section ot he class MyFrame. Below this line you should add:
| 1 | wx.EVT_BUTTON(self,self.button_1.GetId(), self.pushA) |
| 2 | wx.EVT_BUTTON(self,self.button_2.GetId(), self.pushB) |
| view plain | print | copy to clipboard | ? |
| 1 | def pushA(self, event): |
| 2 | self.text_ctrl_1.WriteText("You pressed A\n") |
| 3 | def pushB(self, event): |
| 4 | self.text_ctrl_1.WriteText("You pressed B\n") |
| view plain | print | copy to clipboard | ? |
| 1 | wx.EVT_BUTTON(self, self.button_3.GetId(), self.doSilly) |
| view plain | print | copy to clipboard | ? |
| 1 | def doSilly(self, event): |
| 2 | n = list(self.text_ctrl_1.GetValue()) |
| 3 | n.reverse() |
| 4 | n = ''.join(n) |
| 5 | self.text_ctrl_2.WriteText(n) |
| view plain | print | copy to clipboard | ? |
Try to understand the code, add more functionality as you learn more. And study the excellent examples that come with the wxPython package.
Here is another excellent site on wxPython.
The source code for this tutorial is available here
discuss this topic to forum
