Focusing on the hard parts of wxPython - layout and events

Posted in doh , python

I like wxPython a lot more than the other gui tooltkits i’ve tried. I’m finding it really easy to keep it from getting cluttered, and it’s easy to pick up and run with, but there are examples like this one below that i’ve seen in various forms all over the place that I think are a waste of time. Why would you have an application that has nothing but a menu bar? You wouldn’t, so doesn’t it make sense to get the confusing parts of gui coding out of the way? Namely, layout and events. These two concepts need to be focused on, without all the extra effects. You can add fancy font changes and other junk later. But for now, It only serves to confuse a noob.

This is dumb.

#!/bin/env python

import wx

class wtfno (wx.Frame):
    def __init__(self, parent, id, title, size):
        wx.Frame.__init__(self, parent, id, title=title, size=size)
        menubar = wx.MenuBar()

	file = wx.Menu()
        menubar.Append(file, '&File')
        help = wx.Menu()
        menubar.Append(help, '&Help')

        self.SetMenuBar(menubar)
        self.Centre()
        self.Show(True)

app = wx.App(0)
wtfno(None, -1, 'Stupid Demo', (250,250))
app.MainLoop()

There, Isn’t this better?

class myFrame (wx.Frame):
    def __init__(self, parent, id, title, size):
        wx.Frame.__init__(self, parent, id, title, size)
        panel = wx.Panel(self, -1)

        # Vertical Sizer (vbox) will contain a few horizontal sizers
        vbox = wx.BoxSizer(wx.VERTICAL)

        # These horizontal sizers will go in the vertical sizer (vbox)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        st1 = wx.StaticText(panel, -1, 'Subject')
        hbox1.Add(st1, 0, wx.RIGHT, 8)
        tc = wx.TextCtrl(panel, -1)
        hbox1.Add(tc, 1)

        vbox.Add(hbox1, 0, wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, 10)

        vbox.Add((-1, 10))
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        st2 = wx.StaticText(panel, -1, 'Body')
        hbox2.Add(st2, 0)

        vbox.Add(hbox2, 0, wx.LEFT|wx.TOP, 10)

        vbox.Add((-1, 10))
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        tc2 = wx.TextCtrl(panel, -1, style=wx.TE_MULTILINE)
        hbox3.Add(tc2, 1, wx.EXPAND)

        vbox.Add(hbox3, 1, wx.LEFT|wx.RIGHT|wx.EXPAND, 10)

        # Make it all happen
        panel.SetSizer(vbox)
        self.Centre()
        self.Show(True)

app = wx.App()
myFrame(None, -1, 'Rootninja.com', (250,250))
app.MainLoop()

And event demos should include different types of events, without a lot of fancy dancy decoration.

    def onWidgetSetup(self, widget, event, handler, sizer):
        widget.Bind(event, handler)
        sizer.Add(widget, 0, wx.ALL, 5)
        return widget

    def onButtonKeyEvent(self, event):
        keycode = event.GetKeyCode()
        if keycode == wx.WXK_SPACE:
            print "The spacebar was pressed"
        event.Skip()
Posted by admica   @   17 October 2009

Related Posts

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Uruguay has become the first country to provide a laptop for every child attending state primary school
Next Post
wxPython calculator example skeleton code »
Powered by Wordpress   |   Lunated designed by ZenVerse