Newbie wxPython questions.

nuffnough at gmail.com nuffnough at gmail.com
Fri Apr 7 13:38:08 EDT 2006


I am running through the wxPython guide and docs and extrapolating
enough to get confused.

BAsed on the tute in the getting started wiki I created a panel that
has most of the elements I want; some check boxes and a couple of
buttons.   The button I have is a simple thing that is supposed to just
close (destroy) the app.  Only thing is,  it destroys the panel and
leaves the app behind.   I have attempted to place this on teh frame by
defining a frame;  the button works great and closes the app,  but
because it isn't part of the panel, the panel is all squished up into
the very top left corner and all you can see is the first checkbox,
which you can't even check.  Can I make a button on the panel destroy
the frame?

The next question I have is about adding a second button to the panel
(or frame, I guess) that will then execute a bunch of things depending
on which checkboxes are ticked.  My attempts to add the button have
been wrong so far,  as you'll see with my code.

The first code block is my panel only app.  The second is how it looks
after I defined the Frame first and then moved the Cancel button.

TIA

Nuffnough



--

import wx, sys, os
class Form1(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.quote = wx.StaticText(self, -1, "Launching
:",wx.Point(20,30))

        st = wx.StaticText(self, -1, "Select the applications you need
to launch:")#, (10, 10)

        cb1 = wx.CheckBox(self, -1, "Read Calander, Check
Email")#,(65,40), (150, 20), wx.NO_BORDER)
        cb2 = wx.CheckBox(self, -1, "Internet Browser")

        self.Bind(wx.EVT_CHECKBOX, self.EvtCheckBox, cb1)
        self.Bind(wx.EVT_CHECKBOX, self.EvtCheckBox, cb2)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.AddMany( [ cb1,
                         cb2,
                          ])

        border = wx.BoxSizer(wx.VERTICAL)
        border.Add(st, 0, wx.ALL, 15)
        border.Add(sizer, 0, wx.LEFT, 50)
        self.SetSizer(border)

        pos = cb2.GetPosition().x + cb2.GetSize().width + 25
        btn0 = wx.Button(self, -1, "Cancel", (pos, 150))
        self.Bind(wx.EVT_BUTTON, self.OnCloseMe, btn0)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

	'''  Commented out btn1 cause I couldn't make it work
	btn1 = wx.Button(self, -1, "Open Apps", (pos + 60, 150))
	self.Bind(wx.EVT_BUTTON, self.OnTestButton, btn1)
	'''

    def EvtCheckBox(self, event):
        self.log.write('EvtCheckBox: %d\n' % event.IsChecked())
        cb = event.GetEventObject()
        if cb.Is3State():
            self.log.write("\t3StateValue: %s\n" % cb.Get3StateValue())


    def OnTestButton(self, evt):
        self.cb1.SetString(1, "FUBAR")

    def OnCloseMe(self, event):
        self.Close(True)

    def OnCloseWindow(self, event):
        self.Destroy()



app = wx.PySimpleApp()
frame = wx.Frame(None, -1, "An application launcher")
Form1(frame, -1)
frame.Show(1)
app.MainLoop()





--




import wx, sys, os

ID_ABOUT = 101
ID_EXIT = 110

class Frame1(wx.Frame):
        def __init__(self,parent,id,title):
            wx.Frame.__init__(self,parent,wx.ID_ANY, title, size =
(300, 400), style=wx.DEFAULT_FRAME_STYLE)
            #self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
            self.CreateStatusBar() # A Statusbar in the bottom of the
window
            # Setting up the menu.
            filemenu= wx.Menu()
            filemenu.Append(ID_ABOUT, "&About"," Information about this
program")
            filemenu.AppendSeparator()
            filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")
            # Creating the menubar.
            menuBar = wx.MenuBar()
            menuBar.Append(filemenu,"&File") # Adding the "filemenu" to
the MenuBar
            self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame
content.

            pos = 20
            btn0 = wx.Button(self, -1, "Button Text", (pos, 220))
            self.Bind(wx.EVT_BUTTON, self.OnCloseMe, btn0)
            self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)


#        def OnTestButton(self, evt):
            #Run some code that checks which boxes are ticked
            #Then perform a function for each ticked box

        def OnCloseMe(self, event):
            self.Close(True)

        def OnCloseWindow(self, event):
            self.Destroy()

            self.Show(True)


class Form1(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.quote = wx.StaticText(self, -1, "Choose Applications
:",wx.Point(20,30))

        st = wx.StaticText(self, -1, "Select the application(s) you
need to open:")#, (10, 10)

        cb1 = wx.CheckBox(self, -1, "Email, Calender")#,(65,40), (150,
20), wx.NO_BORDER)
        cb2 = wx.CheckBox(self, -1, "Browser")

        self.Bind(wx.EVT_CHECKBOX, self.EvtCheckBox, cb1)
        self.Bind(wx.EVT_CHECKBOX, self.EvtCheckBox, cb2)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.AddMany( [ cb1,
                         cb2,
                          ])

        border = wx.BoxSizer(wx.VERTICAL)
        border.Add(st, 0, wx.ALL, 15)
        border.Add(sizer, 0, wx.LEFT, 50)
        self.SetSizer(border)


    def EvtCheckBox(self, event):
        self.log.write('EvtCheckBox: %d\n' % event.IsChecked())
        cb = event.GetEventObject()
        if cb.Is3State():
            self.log.write("\t3StateValue: %s\n" % cb.Get3StateValue())


app = wx.PySimpleApp()
frame = Frame1(None, -1, "Nuffy's wxPython explorer")
Form1(frame, -1)
frame.Show(1)
app.MainLoop()




More information about the Python-list mailing list