Sunday, September 9, 2012

CREATING wxPYTHON PANELS AND BUTTONS





REVISED: Sunday, March 3, 2013




In this tutorial, you will learn how to create panels and buttons inside the wxPython GUI frame.

I.  wxPYTHON PANELS AND BUTTONS

The wxPython GUI toolkit starts with a frame. Inside the frame is a panel.  Inside the panel are the buttons that the user uses to run the wxPython code.

II.  wxPYTHON EXAMPLE PROGRAM

Start the Python 2.7.3 interpreter shell by "double left mouse clicking" the Python icon on your desktop. When the "Python Shell" window opens  select "File", then select "New Window".
In the "New Window" type the following code, which is an expansion of the class bright we created in the last tutorial.  Use four spaces, columns, for each indent.
import wx

class bright(wx.Frame):

        def __init__(self,parent,id):
                wx.Frame.__init__(self,parent,id,'Bright Button', size=(800,400))
# Creates a panel object named panel
                panel=wx.Panel(self)
# Creates a button object named button
# Button label is Exit
# Button location inside the panel is in x, y coordinates
# x = 0 pixels from left hand side of the panel
# y = 0 pixels down from the top of the panel
# The x, y coordinates are to the top left corner of the button
# Button size is width, height; 50 wide, 50 tall
                button=wx.Button(panel,label="Exit",pos=(0,0),size=(50,50))

# Binds two events to the button
# First event is clicking button to close the button
# Clicking button calls method closebutton
                self.Bind(wx.EVT_BUTTON, self.closebutton, button)
# Second event is clicking X to close the window
# Clicking X calls method closewindow
                self.Bind(wx.EVT_CLOSE, self.closewindow)

# Defines method closebutton
        def closebutton(self,event):
                self.Close(True)

# Defines method closewindow
        def closewindow(self,event):
                self.Destroy()

if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=bright(parent=None,id=-1)
        frame.Show()
        app.MainLoop()

From the "New Window", do a "File Save As" and save the above Python program, using the file name brightButton.py to the same path which your computer used to download Python 2.7.3. For example, I saved my brightButton.py  file  to my Python27 folder using the following path:

C:\Python27\brightButton.py

From the "New Window", do a "Run" and then a "Run Module F5" and a wxPython GUI toolkit frame with the title "Bright Button" in the top left corner will open.  The comments which start with  a hash, #, sign explain how this wxPython program works. Also, notice some of the underscores are double underscores; e.g., __init__, __name__, and __main__ all have double underscores before and after them.

In this tutorial, you have learned how to  create panels and buttons inside the  wxPython GUI frame.

Elcric Otto Circle



-->



-->



-->






 







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"




No comments:

Post a Comment