Okay, so I'm thinking of taking the plunge into wxPython. But it's especially important to me to know if wxPython can do the following things easily and well:

1) Display a vertical array of radio buttons (or perhaps any buttons will do). They must have no labels, and I need to record exactly which button the person pressed. Pressing needs to trigger going to the next screen.

2) Show images/play sounds for a certain duration. For example, I need to show an image for 500 milliseconds, pause for 400 ms, then show another for 500 ms.

Also, I'm looking forward to finding out if certain little annoyances from Tkinter are fixed (e.g., the difficulty in assigning a button command that involves parameters).

Thanks to vegaseat for enticing me with promises of making the cursor invisible... ;)

Recommended Answers

All 9 Replies

The easiest way is to use a wx.RadioBox:

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent=None):
        wx.Frame.__init__(self, parent, wx.ID_ANY, size=(300, 160))
        # match to the number of radiobuttons
        self.mychoices = ['image1', 'image2', 'image3', 'image4']

        # create a box with 4 radio buttons, no labels here
        label_choices = [' ', ' ', ' ', ' ']
        self.radiobox = wx.RadioBox(self, wx.ID_ANY,
            " click on a button ",
            choices=label_choices, style=wx.VERTICAL)
        # bind mouse click to an action
        self.radiobox.Bind(wx.EVT_RADIOBOX, self.onAction)

        # show present selection
        self.onAction(None)

    def onAction(self, event):
        """show the selected choice"""
        index = self.radiobox.GetSelection()
        s = "Selected " + self.mychoices[index]
        # show the result in the frame title
        self.SetTitle(s)


app = wx.App(0)
MyFrame().Show()
app.MainLoop()

Showing images is easy with wx.StaticBitmap.
http://www.wxpython.org/docs/api/wx.StaticBitmap-class.html

NOTE:This code is borrowed from the sticky

# show  .jpg .png .bmp or .gif image on wx.Panel

import wx

class ImagePanel(wx.Panel):
  """ create the panel and put image on it """
  def __init__(self, parent, id):
    # create the panel, this will be self
    wx.Panel.__init__(self, parent, id)
    try:
        # pick your image file you have in the working folder
        # or use the full file path
        image_file = 'strawberry.jpg'
        bmp = wx.Bitmap(image_file)
        # show the bitmap, image's upper left corner anchors
        # at panel coordinates (5, 5), default is center
        wx.StaticBitmap(self, -1, bmp, (5, 5))
        # show some image information
        info = "%s  %dx%d" % (image_file, bmp.GetWidth(), bmp.GetHeight())
        # the parent is the frame 
        parent.SetTitle(info)
    except IOError:
        print "Image file %s not found" % imageFile
        raise SystemExit


# redirect=False sends stdout/stderr to the console window
# redirect=True sends stdout/stderr to a wx popup window (default) 
app = wx.App(redirect=False)
# create window/frame, no parent, -1 is the default ID
# also increase the size of the frame for larger images
frame = wx.Frame(None, -1, size = (480, 320))
# create the panel instance
imp = ImagePanel(frame, -1)
# show the frame
frame.Show(True)
# start the GUI event loop
app.MainLoop()

Hope that helps

Wow, thanks! You guys are great!

Now I just need to know if it can play sounds, and if the images can be displayed for a given duration? Tkinter has the after method for dealing with this. For example:

frame.after(500, doSomethingElse)

Also, is there a better way to maximize the frame than just setting its size to the screen size? (When I do that I can still see the frame bar at the top -- in Mac -- and I'd prefer not to, although it's not a big deal.)

Here is an example without a titlebar and showing fullscreen:

# wx.Frame with no title bar and showing fullscreen
# modified from vegaseat's example

import wx

def exit(event):
    frame.Close(True)

app = wx.App(0)
# create a window, no-parent, -1 is default ID, style with no titlebar
frame = wx.Frame(parent=None, id=-1, pos=(50,100), size=(300,200), 
    style=wx.MINIMIZE_BOX)
frame.SetBackgroundColour('green')
frame.ShowFullScreen(True, style=wx.FULLSCREEN_ALL)

# provide exit for a frame without titlebar
quit = wx.Button(frame, id=-1, label='Exit', pos=(0, 0))
quit.Bind(wx.EVT_BUTTON, exit)

# show the window
frame.Show(True)

# start the event loop
app.MainLoop()

Brilliant! I just need to know now if it can play sounds...?

Oh yeah, wxPython has its own sound functions, one of them is wx.Sound() ...

# playing a wave file with wxPython's 
# wx.Sound(fileName, isResource=False)
# and its method Play(flags=wx.SOUND_ASYNC)
# vega

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
        self.SetBackgroundColour("red")

        # pick a .wav sound file you have ...
        sound = wx.Sound('anykey.wav')
        # wx.SOUND_SYNC --> sound plays completely through
        # wx.SOUND_ASYNC --> sound can be stopped or reset
        # wx.SOUND_ASYNC|wx.SOUND_LOOP --> loop until stopped
        # with sound.Stop()
        sound.Play(wx.SOUND_SYNC)


app = wx.App(0)
# create a MyFrame instance and show the frame
MyFrame(None, 'wx.Sound()', (300, 100)).Show()
app.MainLoop()

Also check the example at:
http://www.daniweb.com/forums/post872330-109.html

I have to say, I am really impressed. Somehow when I was first looking at wxPython ages ago, I must have downloaded the worst tutorial ever, because it seemed so obtuse and useless for my needs -- yet now it seems so simple and has everything I need built in.

I'm going to make a go at switching over my program today... wish me luck! Thanks for all the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.