📄 edit_sizers.py
字号:
wx.VERTICAL: 'wxVERTICAL' } return od.get(self.orient) def set_orient(self, value): od = { 'wxHORIZONTAL': wx.HORIZONTAL, 'wxVERTICAL': wx.VERTICAL } self.orient = od.get(value, wx.VERTICAL)# end of class EditBoxSizerclass wxGladeStaticBoxSizer(wx.StaticBoxSizer): def SetItemMinSize(self, item, w, h): try: w2, h2 = item.GetBestSize() if w == -1: w = w2 if h == -1: h = h2 except AttributeError: pass wx.StaticBoxSizer.SetItemMinSize(self, item, w, h)# end of class wxGladeStaticBoxSizer class EditStaticBoxSizer(SizerBase): """\ Class to handle wxStaticBoxSizer objects """ def __init__(self, name, window, orient=wx.VERTICAL, label='', elements=3, toplevel=True, show=True): self.label = label self.orient = orient SizerBase.__init__(self, name, 'wxStaticBoxSizer', window, toplevel, show) self.access_functions['orient'] = (self.get_orient, self.set_orient) self.properties['orient'] = HiddenProperty(self, 'orient', (orient==wx.HORIZONTAL and 'wxHORIZONTAL' or 'wxVERTICAL')) class Dummy: widget = None # add to self.children the SizerItem for self._btn self.children = [SizerItem(Dummy(), 0, 0, wx.EXPAND)] for i in range(1, elements+1): tmp = SizerSlot(self.window, self, i) self.children.append(SizerItem(tmp, i, 1, wx.EXPAND)) def create_widget(self): self.widget = wxGladeStaticBoxSizer(wx.StaticBox(self.window.widget, -1, self.label), self.orient) self.widget.Add(self._btn, 0, wx.EXPAND) for c in self.children[1:]: # we've already added self._btn c.item.show_widget(True) if isinstance(c.item, SizerSlot): self.widget.Add(c.item.widget, 1, wx.EXPAND) self.widget.SetItemMinSize(c.item.widget, 20, 20) else: sp = c.item.properties.get('size') if sp and sp.is_active() and \ (c.option == 0 or not (c.flag & wx.EXPAND)): size = sp.get_value().strip() if size[-1] == 'd': size = size[:-1] use_dialog_units = True else: use_dialog_units = False w, h = [ int(v) for v in size.split(',') ] if use_dialog_units: w, h = wx.DLG_SZE(c.item.widget, (w, h)) else: w, h = c.item.widget.GetBestSize() self.widget.SetItemMinSize(c.item.widget, w, h) self.layout() if not self.toplevel and getattr(self, 'sizer'): # getattr(self, 'sizer') is False only in case of a 'change_sizer' # call self.sizer.add_item(self, self.pos, self.option, self.flag, self.border, self.widget.GetMinSize()) def _property_setup(self): SizerBase._property_setup(self) self.access_functions['label'] = (self.get_label, self.set_label) lbl = self.properties['label'] = TextProperty(self, 'label', None, label=_("label")) def write(outfile, tabs): import widget_properties outfile.write(' ' * tabs + '<label>') outfile.write(widget_properties.escape(widget_properties._encode( self.get_label()))) outfile.write('</label>\n') # we must consider also "" a valid value lbl.write = write def create_properties(self): SizerBase.create_properties(self) panel = self.notebook.GetPage(0) sizer = panel.GetSizer() self.properties['label'].display(panel) sizer.Add(self.properties['label'].panel, 0, wx.EXPAND) sizer.Layout() w, h = sizer.GetMinSize() panel.SetScrollbars(1, 5, 1, int(math.ceil(h/5.0))) def set_label(self, value): """\ Sets the label of the static box """ self.label = misc.wxstr(value) if self.widget: self.widget.GetStaticBox().SetLabel(self.label) self.layout() def get_label(self): return self.label def delete(self): if self.widget: self.widget.GetStaticBox().Destroy() SizerBase.delete(self) def get_orient(self): od = { wx.HORIZONTAL: 'wxHORIZONTAL', wx.VERTICAL: 'wxVERTICAL' } return od.get(self.orient) def set_orient(self, value): od = { 'wxHORIZONTAL': wx.HORIZONTAL, 'wxVERTICAL': wx.VERTICAL } self.orient = od.get(value, wx.VERTICAL)# end of class EditStaticBoxSizerclass CustomSizer(wx.BoxSizer): """\ Custom wxSizer class used to implement a GridSizer with an additional handle button """ def __init__(self, parent, factory, rows, cols, vgap, hgap): wx.BoxSizer.__init__(self, wx.VERTICAL) self.parent = parent self._grid = factory(rows, cols, vgap, hgap) wx.BoxSizer.Add(self, self.parent._btn, 0, wx.EXPAND) wx.BoxSizer.Add(self, self._grid, 1, wx.EXPAND) def __getattr__(self, name): return getattr(self._grid, name) def GetBestSize(self): return self._grid.GetMinSize() def Add(self, *args, **kwds): self._grid.Add(*args, **kwds) def Insert(self, pos, *args, **kwds): self._grid.Insert(pos-1, *args, **kwds) def Remove(self, *args, **kwds): try: pos = int(args[0])-1 self._grid.Remove(pos) except TypeError: self._grid.Remove(*args, **kwds) def RemovePos(self, pos): self._grid.Remove(pos-1) def Detach(self, pos_or_obj): try: pos = int(pos_or_obj) - 1 self._grid.Detach(pos) except TypeError: self._grid.Detach(pos_or_obj) def SetItemMinSize(self, item, w, h): #*args, **kwds): try: w2, h2 = item.GetBestSize() if w == -1: w = w2 if h == -1: h = h2 except AttributeError: pass self._grid.SetItemMinSize(item, w, h) def GetChildren(self): return [None] + list(self._grid.GetChildren()) def Layout(self): self._grid.Layout() wx.BoxSizer.Layout(self)# end of class CustomSizerclass GridSizerBase(SizerBase): """\ Base class for Grid sizers. Must not be instantiated. """ def __init__(self, name, klass, window, rows=3, cols=3, vgap=0, hgap=0, toplevel=True, show=True): self.rows = rows; self.cols = cols self.vgap = vgap; self.hgap = hgap if self.cols or self.rows: if not self.rows: self.rows = 1 elif not self.cols: self.cols = 1 menu = [(_('Add slot'), self.add_slot), (_('Insert slot...'), self.insert_slot), (_('Add row'), self.add_row), (_('Add column'), self.add_col), (_('Insert row...'), self.insert_row), (_('Insert column...'), self.insert_col)] SizerBase.__init__(self, name, klass, window, toplevel, show, menu) class Dummy: widget = None # add to self.children the SizerItem for self._btn self.children = [SizerItem(Dummy(), 0, 0, wx.EXPAND)] for i in range(1, self.rows*self.cols+1): tmp = SizerSlot(self.window, self, i) self.children.append(SizerItem(tmp, i, 1, wx.EXPAND)) def create_widget(self): """\ This must be overriden and called at the end of the overriden version """ to_lay_out = [] for c in self.children[1:]: # we've already added self._btn c.item.show_widget(True) if isinstance(c.item, SizerSlot): self.widget.Add(c.item.widget, 1, wx.EXPAND) self.widget.SetItemMinSize(c.item.widget, 20, 20) else: sp = c.item.properties.get('size') if sp and sp.is_active(): if (c.option != 0 or (c.flag & wx.EXPAND)) and \ (misc.check_wx_version(2, 4) or \ not (c.flag & wx.FIXED_MINSIZE)): c.item.widget.Layout() w, h = c.item.widget.GetBestSize() if misc.check_wx_version(2, 5): c.item.widget.SetMinSize((w, h)) else: size = sp.get_value().strip() if size[-1] == 'd': size = size[:-1] use_dialog_units = True else: use_dialog_units = False w, h = [ int(v) for v in size.split(',') ] if use_dialog_units: w, h = wx.DLG_SZE(c.item.widget, (w, h)) # now re-set the item to update the size correctly... to_lay_out.append((c.item.pos, (w, h)))## sp = c.item.properties.get('size')## if sp and sp.is_active() and \## (c.option == 0 or not (c.flag & wxEXPAND)):## size = sp.get_value().strip()## if size[-1] == 'd':## size = size[:-1]## use_dialog_units = True## else: use_dialog_units = False## w, h = [ int(v) for v in size.split(',') ]## if use_dialog_units:## w, h = wxDLG_SZE(c.item.widget, (w, h))## else:## w, h = c.item.widget.GetBestSize()## self.widget.SetItemMinSize(c.item.widget, w, h) for pos, size in to_lay_out: #print 'set_item:', pos, size self.set_item(pos, size=size, force_layout=False) self.layout(True) def _property_setup(self): SizerBase._property_setup(self) self.access_functions['rows'] = (self.get_rows, self.set_rows) self.access_functions['cols'] = (self.get_cols, self.set_cols) self.access_functions['hgap'] = (self.get_hgap, self.set_hgap) self.access_functions['vgap'] = (self.get_vgap, self.set_vgap) props = { 'rows': SpinProperty(self, 'rows', None, label=_("rows")), 'cols': SpinProperty(self, 'cols', None, label=_("cols")), 'hgap': SpinProperty(self, 'hgap', None, label=_("hgap")), 'vgap': SpinProperty(self, 'vgap', None, label=_("vgap")) } self.properties = props def create_properties(self): SizerBase.create_properties(self) page = wx.ScrolledWindow(self.notebook, -1, style=wx.TAB_TRAVERSAL) sizer = wx.BoxSizer(wx.VERTICAL) props = self.properties props['rows'].display(page) props['cols'].display(page) props['vgap'].display(page) props['hgap'].display(page) sizer.Add(props['rows'].panel, 0, wx.EXPAND) sizer.Add(props['cols'].panel, 0, wx.EXPAND) sizer.Add(props['vgap'].panel, 0, wx.EXPAND) sizer.Add(props['hgap'].panel, 0, wx.EXPAND) page.SetAutoLayout(True) page.SetSizer(sizer) sizer.Fit(page) self.notebook.AddPage(page, _("Grid")) def get_rows(self): return self.rows def get_cols(self): return self.cols def get_vgap(self): return self.vgap def get_hgap(self): return self.hgap def _set_rows_cols(self, rows, cols): self.rows = rows self.cols = cols self.properties['rows'].set_value(rows) self.properties['cols'].set_value(cols) if self.widget: self.widget.SetRows(self.rows) self.widget.SetCols(self.cols) self.layout(True) def set_rows(self, rows): self.rows = int(rows) if self.widget: self.widget.SetRows(self.rows) self.layout(True) def set_cols(self, cols): self.cols = int(cols) if self.widget: self.widget.SetCols(self.rows) self.layout(True) def set_hgap(self, hgap): self.hgap = int(hgap) if self.widget: self.widget.SetHGap(self.hgap) self.layout() def set_vgap(self, vgap): self.vgap = int(vgap) if self.widget: self.widget.SetVGap(self.vgap) self.layout() def fit_parent(self, *args): """\ Tell the sizer to resize the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -