Introduction

This is an example of using a Tabbed (wxNotebook) Dialog with resizable controls.
(It took me more than 2 days to realize I had to use wxPanel instead of wxWindows controls)

Hope it helps someone...
-- jbalague

See also: UsingSizers

Code Sample

   1 import wx
   2 
   3 #----------------------------------------------------------------------
   4 class MyTabbedDlg(wx.Dialog):
   5     def __init__(self, parent):
   6         title = "Resize the dialog and see how controls adapt!"
   7         wx.Dialog.__init__(self, parent, -1, title,
   8                            style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
   9 
  10         notebook = wx.Notebook(self, -1, size=(450,300))
  11         panel1 = wx.Panel(notebook)
  12         panel2 = wx.Panel(notebook)
  13         notebook.AddPage(panel1, "Panel 1")
  14         notebook.AddPage(panel2, "Panel 2")
  15 
  16         dialog_sizer = wx.BoxSizer(wx.VERTICAL)
  17         dialog_sizer.Add(notebook, 1, wx.EXPAND|wx.ALL, 5)
  18 
  19         panel1_sizer = wx.BoxSizer(wx.VERTICAL)
  20         text = wx.TextCtrl(panel1, -1, "Hi!", size=(400,90), style=wx.TE_MULTILINE)
  21         button1 = wx.Button(panel1, -1, "I only resize horizontally...")
  22         panel1_sizer.Add(text, 1, wx.EXPAND|wx.ALL, 10)
  23         panel1_sizer.Add(button1, 0, wx.EXPAND|wx.ALL, 10)
  24         panel1.SetSizer(panel1_sizer)
  25 
  26         panel2_sizer = wx.BoxSizer(wx.HORIZONTAL)
  27         button2 = wx.Button(panel2, -1, "I resize vertically")
  28         button3 = wx.Button(panel2, -1, "I don't like resizing!")
  29         panel2_sizer.Add(button2, 0, wx.EXPAND|wx.ALL, 20)
  30         panel2_sizer.Add(button3, 0, wx.ALL, 100)
  31         panel2.SetSizer(panel2_sizer)
  32 
  33         if "__WXMAC__" in wx.PlatformInfo:
  34            self.SetSizer(dialog_sizer)
  35         else:
  36            self.SetSizerAndFit(dialog_sizer)
  37         self.Centre()
  38 
  39         self.Bind(wx.EVT_BUTTON, self.OnButton)
  40 
  41 
  42     def OnButton(self, evt):
  43         self.EndModal(0)
  44 
  45 
  46 #----------------------------------------------------------------------
  47 class MyApp(wx.App):
  48     def OnInit(self):
  49         dlg = MyTabbedDlg(None)
  50         dlg.ShowModal()
  51         dlg.Destroy()
  52         return True
  53 
  54 myapp = MyApp(redirect=False)
  55 myapp.MainLoop()

Comments


I like the example, but it would be better if it were possible to get it to stop running. Since the dialog appears to have no way to quit, and it's modal, the controlling application can't be terminated. Or is this only a problem on Windows?

It works better if the dialog style is set to wxRESIZE_BORDER+wxDEFAULT_DIALOG_STYLE, then the dialog can be closed in a controlled way. SteveHolden


This example crashes with a Bus error (core dump) on MacOSX 10.3, wxPython 2.5.3.1. It only works if you add the wxDEFAULT_DIALOG_STYLE as mentioned above. Here is the trace...maybe some smart individual will see this and fix the source code to window.cpp...

0   libwx_macu-2.5.3.dylib      0x027b4ec4 wxWindow::MacSetBackgroundBrush(wxBrush const&) + 0x40
1   libwx_macu-2.5.3.dylib      0x027b4e2c wxWindow::SetBackgroundColour(wxColour const&) + 0x128
2   libwx_macu-2.5.3.dylib      0x027b126c wxTopLevelWindowMac::Create(wxWindow*, int, wxString const&, wxPoint const&, wxSize const&, long, wxString const&) + 0xdc
3   libwx_macu-2.5.3.dylib      0x027865f4 wxDialog::Create(wxWindow*, int, wxString const&, wxPoint const&, wxSize const&, long, wxString const&) + 0x6c
4   _windows_.so                0x02e0c590 _wrap_new_Dialog + 0x480 (dialog.h:57)

In addition, the demo has all sorts of refresh/paint issues once you get it running. Especially when you click on Panel 2.


On OSX, if you remove wx.EXPAND from:

         panel2_sizer.Add(button2, 0, wx.EXPAND|wx.ALL, 20)

Then, the text for the button appears in the right place instead of appearing as though it is floating in the middle of the window.


When I run the example on my pc, the layout is not so good ,I do not see the button at "panel1" until I resizer by drag the frame, in addition, the two buttones are overlapping.the images is [ATTACH]

        notebook.AddPage(panel1, "Panel 1")
        notebook.AddPage(panel2, "Panel 2")

behind the

        panel1_sizer = wx.BoxSizer(wx.VERTICAL)
        text = wx.TextCtrl(panel1, -1, "Hi!", size=(400,90), style=wx.TE_MULTILINE)
        button1 = wx.Button(panel1, -1, "I only resize horizontally...")
        panel1_sizer.Add(text, 1, wx.EXPAND|wx.ALL, 10)
        panel1_sizer.Add(button1, 0, wx.EXPAND|wx.ALL, 10)
        panel1.SetSizer(panel1_sizer)

        panel2_sizer = wx.BoxSizer(wx.HORIZONTAL)
        button2 = wx.Button(panel2, -1, "I resize vertically")
        button3 = wx.Button(panel2, -1, "I don't like resizing!")
        panel2_sizer.Add(button2, 0, wx.EXPAND|wx.ALL, 20)
        panel2_sizer.Add(button3, 0, wx.ALL, 100)
        panel2.SetSizer(panel2_sizer)

        notebook.AddPage(panel1, "Panel 1")
        notebook.AddPage(panel2, "Panel 2")

ResizableNotebookDialog (last edited 2009-11-10 05:45:45 by 133)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.