Tuesday, September 11, 2012

wxPYTHON SIMPLE TEXT PROGRAM





REVISED: Sunday, March 3, 2013



In this tutorial, you will learn how to create a wxPython simple text program.

I.  wxPYTHON SIMPLE TEXT PROGRAM

A.  wxPYTHON "DIALOG BOX" FRAME

A dialog box, text box, is used to prompt the user for information. The  text box title, DIALOG BOX, is shown at the top left of the panel. The question the user is to respond to, is show under the text box title. The text box, where the user enters a response, is in the center on the panel. The bottom of the panel has two buttons: OK, and Cancel. If the user clicks Cancel, the user response is not assigned to a variable and the DIALOG BOX frame is closed. If the user clicks OK, the user response is assigned to a variable, and the DIALOG BOX frame is closed.

B.  wxPYTHON "FRAME WINDOW" FRAME

When the DIALOG BOX frame closes, the plain FRAME WINDOW opens with the user response displayed.

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 worked with 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,'PLAIN FRAME WINDOW', size=(600,400))
                panel=wx.Panel(self)

# Creates a dialog box object named box
# Four parameters:
# 1. Parent name is panel
# 2. User prompt, question
# 3. Dialog Box frame title
# 4. Dialog Box default text
                box=wx.TextEntryDialog(panel, 'What color is the White House?','DIALOG BOX FRAME','Enter a color.')

# Stores user response in variable answer, if user clicks OK
                answer=" "
                if box.ShowModal()==wx.ID_OK:
                        answer=box.GetValue()

# Writes user input to the PLAIN FRAME WINDOW
# Four parameters:
# 1. Parent name is panel
# 2. ID is -1
# 3. Variable is answer
# 4. Position is 15 across and 20 down from top left corner of PLAIN FRAME WINDOW 
                wx.StaticText(panel, -1, answer, (15,20))

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 brightSimpleText.py to the same path which your computer used to download Python 2.7.3. For example, I saved my brightSimpleText.py  file  to my Python27 folder using the following path:

C:\Python27\brightSimpleText.py

From the "New Window", do a "Run" and then a "Run Module F5" and a wxPython GUI toolkit frame with the title "DIALOG BOX FRAME" in the top left corner will open.

The comments which start with  a hash, #, sign explain how this wxPython program works. 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 a wxPython simple text program.

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