Tuesday, September 11, 2012

wxPYTHON CUSTOM BITMAP BUTTONS





REVISED: Sunday, March 3, 2013



In this tutorial, you will learn how to create wxPython custom bitmap buttons.

I.  wxPYTHON BITMAP BUTTON

The PLAIN FRAME WINDOW opens with the Yoda.bmp file displayed in the top left corner of the frame.

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)

# Stores bitmap in object named pic
# Two parameters:
# 1. File name Yoda.bmp should be in same directory as Python
# 2. wx.BITMAP_TYPE_BMP
                pic=wx.Image("Yoda.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()

# Creates bitmap button
# Four parameters:
# 1. Parent name is panel
# 2. ID is -1
# 3. Button name is pic
# 4. Position is 1 across and 1 down from top left corner of PLAIN FRAME WINDOW 
                self.button=wx.BitmapButton(panel, -1, pic, pos=(1,1))
# Binds an event to the button
# Three parameters:
# 1. wx.EVT_BUTTON event is clicking the button
# 2. Calls self.ohOh method
# 3. self.button
                self.Bind(wx.EVT_BUTTON, self.ohOh, self.button)

# Button default
                self.button.SetDefault()

# Executes ohOh method when button is clicked
# Closes the button
        def ohOh(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 brightBitmapButton.py to the same path which your computer used to download Python 2.7.3. For example, I saved my brightBitmapButton.py  file  to my Python27 folder using the following path:

C:\Python27\brightBitmapButton.py

From the "New Window", do a "Run" and then a "Run Module F5" and a wxPython GUI toolkit frame with the title "PLAIN FRAME WINDOW" 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 wxPython custom bitmap buttons.

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