Hi,
In the code below, there is a frame, with a notebook and 2 notebook pages. What I’m trying to do is: when I change the notebook page, the frame’s size should change so that it completely displays the widgets on the notebook page. When I select the “Grid” page, the frame’s size should change so that the grid’s entire width will be visible (I don’t want all grid rows to be displayed, because if there are lots of rows in the grid, then the frame’s height will be too big. Entire grid width visibility is what I want.)
The problem is: when I select the “Grid” page, the frame’s size gets big but it doesn’t completely display the entire grid width, there appears a horizontal scroll bar, the user has to scroll to right to display last 2 columns. How to change the code so that the frame automatically resizes and displays all columns without adding a horizontal scroll bar? Thanks.
import wx
import wx.grid as grid_lib
import wx.lib.agw.flatnotebook as fnb
from wx.adv import CalendarCtrl, GenericCalendarCtrl, CalendarDateAttr
import random
import string
class TheFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "The Frame",
style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ^ wx.MAXIMIZE_BOX
)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.the_notebook = fnb.FlatNotebook(self, -1, agwStyle=fnb.FNB_VC8 | fnb.FNB_NO_X_BUTTON)
self.calendar_panel = wx.Panel(self, -1)
self.grid_panel = wx.Panel(self, -1)
self.the_notebook.AddPage(self.calendar_panel, "Calendar")
self.the_notebook.AddPage(self.grid_panel, "Grid")
self.the_notebook.Bind(fnb.EVT_FLATNOTEBOOK_PAGE_CHANGED, self.on_nb_page_change)
self.sizer.Add(self.the_notebook, 0, wx.ALL | wx.EXPAND, 10)
self.calendar1 = GenericCalendarCtrl(self.calendar_panel, -1, wx.DateTime().Today(),
style = wx.adv.CAL_SHOW_HOLIDAYS
| wx.adv.CAL_MONDAY_FIRST
| wx.adv.CAL_SEQUENTIAL_MONTH_SELECTION)
self.calendar2 = GenericCalendarCtrl(self.calendar_panel, -1, wx.DateTime().Today(),
style = wx.adv.CAL_SHOW_HOLIDAYS
| wx.adv.CAL_MONDAY_FIRST
| wx.adv.CAL_SEQUENTIAL_MONTH_SELECTION)
the_choices = [str(i) for i in range(1, 10)]
self.choice_box = wx.CheckListBox(self.calendar_panel, -1, choices=the_choices)
calendar_sizer = wx.BoxSizer(wx.HORIZONTAL)
calendar_sizer.Add(self.calendar1, 0, wx.ALL, 5)
calendar_sizer.Add(self.calendar2, 0, wx.ALL, 5)
calendar_sizer.Add(self.choice_box, 0, wx.ALL, 5)
self.calendar_panel.SetSizer(calendar_sizer)
self.the_grid = grid_lib.Grid(self.grid_panel, -1, name="grid")
self.the_grid.CreateGrid(50, 12)
self.the_grid.DisableDragColSize()
self.the_grid.DisableDragRowSize()
self.the_grid.SetDefaultCellAlignment(wx.ALIGN_CENTER, wx.ALIGN_CENTER)
for colx in range(12):
for rowx in range(50):
self.the_grid.SetCellValue(rowx, colx, random.choice(string.ascii_letters))
grid_sizer = wx.BoxSizer(wx.VERTICAL)
grid_sizer.Add(self.the_grid, 0, wx.ALL|wx.EXPAND, 5)
self.grid_panel.Layout()
self.grid_panel.SetSizer(grid_sizer)
self.grid_panel.Layout()
def on_nb_page_change(self, event):
w, h = self.GetSize()
the_page = self.the_notebook.GetCurrentPage()
bw, bh = the_page.GetBestSize()
self.SetSize((bw, h))
self.SendSizeEvent()
if __name__ == "__main__":
app = wx.App(False)
frame = TheFrame()
frame.Show()
app.MainLoop()