wxPython - PLEASE Help

Cliff Wells clifford.wells at attbi.com
Tue Dec 24 06:55:01 EST 2002


On Tue, 2002-12-24 at 01:17, DP wrote:
> In advance, many thanks for any help on this. I'm stumped, mainly
> because I just don't know enough.
> 
> I can't seem to get the panel to display in the frame (see code
> below). What am I doing wrong? I've been at this too long now...
> 

Try it like this instead:

# -----------------------------------------------------
from wxPython.wx import *
# -----------------------------------------------------
def ruSure(parent):
    ru = wxMessageDialog(parent, "Are you sure you want to quit?",
                         "Leaving Application",
                         wxOK | wxCANCEL | wxICON_QUESTION)
    return ru.ShowModal()
# -----------------------------------------------------
class baseCopyPane(wxPanel):
    def __init__(self, parent, id, Info, sourceDir,
                 pos = wxDefaultPosition,
                 size = wxDefaultSize,
                 style = wxTAB_TRAVERSAL,
                 cbTT = "Select Individually",
                 loadTT = "Copy files",
                 backTT = "Backup files",
                 ):
        wxPanel.__init__(self, parent, id)
        
        hbox = wxBoxSizer(wxHORIZONTAL)

        self.cb = wxCheckBox(self, -1, "x", style = wxSUNKEN_BORDER)
        self.load = wxButton(self, -1, "Load")
        self.back = wxButton(self, -1, "Back-up")
        self.text = wxStaticText(self, -1, Info)
        
        hbox.Add(self.load, 0, wxALIGN_CENTER)
        hbox.Add(self.back, 0, wxALIGN_CENTER)
        hbox.Add(self.cb, 0, wxALIGN_CENTER)
        hbox.Add(self.text, 1, wxEXPAND)
        
        self.SetAutoLayout(true)
        self.SetSizer(hbox)
        self.SetAutoLayout(true)
        
# -----------------------------------------------------
class testframe(wxFrame):
    def __init__(self, parent, id, title):
        # initialize frame geometry & position
        wxFrame.__init__(self, parent, id, title, 
            size = (600, 200), 
            style = wxSYSTEM_MENU | wxMINIMIZE_BOX | wxRESIZE_BORDER |
wxCAPTION)
        # Add a panel to place things in
        self.panel = baseCopyPane(self, -1, "a", "b")
        EVT_CLOSE(self, self.OnCloseWindow)
        self.Centre(direction = wxBOTH)
        
    def OnCloseWindow(self, event):
        if ruSure(self) == wxID_OK:
            self.Destroy()
            
    def exitApp(self, event):
        if ruSure(self) == wxID_OK:
            self.Close()
            
# -----------------------------------------------------
class TestApp(wxApp):
    def OnInit(self):
        frame = testframe(NULL, -1, "Test Frame")
        frame.Show(true)
        self.SetTopWindow(frame)
        return true
# -----------------------------------------------------
if __name__ == "__main__":
    app = TestApp(0)
    app.MainLoop()


There were several problems and apparent misunderstandings evident in
your code:

1. Don't mix sizers with absolute positions.  That is, if you use
sizers, don't pass values for size and position when creating the
controls.  These are two incompatible methods for placing controls.

2. wxFrames don't need sizers.  They automatically resize whatever child
they have to fit themselves.

3. You don't need to explicitly assign id's to controls.  Usually you
can just pass -1 and let wxPython assign an unused id.  If you need to
find out that id at a later point, use control.GetId().  Usually it's
far more useful to save a reference to the control (self.control) than
the id, and if you have the control you can always find out the id.

4. It isn't necessary to Centre() anything but dialogs and frames.


Other than that, it looks as if you're off to a quick start.

Good luck,


-- 
Cliff Wells <clifford.wells at attbi.com>





More information about the Python-list mailing list