def __init__(self, parent, value=0.0, range_val=(0.0, 1.0), step=0.01, digits=2, size=DEF_SIZE, width=0, onchange=None, onenter=None, check_focus=True): self.callback = onchange self.enter_callback = onenter spin_overlay = const.SPIN['overlay'] spin_sep = const.SPIN['sep'] if const.is_mac(): spin_overlay = False if not width and const.is_msw(): width = 5 wx.Panel.__init__(self, parent) if spin_overlay: if const.is_gtk(): self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) size = (-1, self.entry.GetSize()[1]) self.entry.SetPosition((0, 0)) self.sb = SpinButton(self, size=size, onchange=self._check_spin) w_pos = self.entry.GetSize()[0] - 5 if spin_sep: self.line = HPanel(self) self.line.SetSize((1, self.sb.GetSize()[1] - 2)) self.line.set_bg(const.UI_COLORS['dark_shadow']) self.line.SetPosition((w_pos - 1, 1)) self.sb.SetPosition((w_pos, 0)) self.SetSize((-1, self.entry.GetSize()[1])) elif const.is_msw(): width += 2 self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) size = (-1, self.entry.GetSize()[1] - 3) self.sb = SpinButton(self.entry, size=size, onchange=self._check_spin) w_pos = self.entry.GetSize()[0] - self.sb.GetSize()[0] - 3 self.sb.SetPosition((w_pos, 0)) w, h = self.entry.GetSize() self.entry.SetSize((w, h + 1)) else: self.box = wx.BoxSizer(const.HORIZONTAL) self.SetSizer(self.box) self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) self.box.Add(self.entry, 0, wx.ALL) size = (-1, self.entry.GetSize()[1]) self.sb = SpinButton(self, size=size, onchange=self._check_spin) self.box.Add(self.sb, 0, wx.ALL) if check_focus: self.entry.Bind(wx.EVT_KILL_FOCUS, self._entry_lost_focus, self.entry) self.entry.Bind(wx.EVT_CONTEXT_MENU, self._ctxmenu, self.entry) self.set_step(step) self.set_range(range_val) self._set_digits(digits) self._set_value(value) self.flag = False self.Fit()
class CloseDialog(SimpleDialog): button_box = None close_btn = None left_button_box = None def __init__(self, parent, title, size=(-1, -1), style=VERTICAL, resizable=True, on_load=None, add_line=True, margin=None): SimpleDialog.__init__(self, parent, title, size, style, resizable, on_load, add_line, margin) def set_dialog_buttons(self): if self.add_line: self.box.pack(HLine(self.box), fill=True, padding=5) else: self.box.pack((3, 3)) self.button_box = HPanel(self.box) self.box.pack(self.button_box, fill=True) self.close_btn = Button(self.button_box, '', onclick=self.on_close, default=True, pid=const.BUTTON_CLOSE) self.left_button_box = HPanel(self.button_box) self.button_box.pack(self.left_button_box, expand=True, fill=True) self.button_box.pack(self.close_btn, padding=5)
def set_dialog_buttons(self): if self.add_line: self.box.pack(HLine(self.box), fill=True, padding=5) else: self.box.pack((3, 3)) self.button_box = HPanel(self.box) self.box.pack(self.button_box, fill=True) self.ok_btn = Button( self.button_box, '', onclick=self.on_ok, default=True, pid=self.action_button) self.cancel_btn = Button( self.button_box, '', onclick=self.on_cancel, pid=const.BUTTON_CANCEL) self.left_button_box = HPanel(self.button_box) self.button_box.pack(self.left_button_box, expand=True, fill=True) if const.IS_MAC: self.button_box.pack(self.ok_btn, padding=5) self.button_box.pack(self.cancel_btn, padding=5) elif const.IS_MSW: self.button_box.pack(self.ok_btn, padding=2) self.button_box.pack(self.cancel_btn) else: self.button_box.pack(self.cancel_btn, padding=2) self.button_box.pack(self.ok_btn)
def __init__(self, parent, draw_top=True, custom_bg=None, painter=0): self.draw_top = draw_top self.custom_bg = custom_bg HPanel.__init__(self, parent) SensitiveCanvas.__init__(self, check_move=True) self.set_double_buffered() self.pack((TAB_PADDING, TAB_HEIGHT)) self.set_painter(painter if painter in PAINTERS else 0)
def __init__(self, parent, draw_top=True, custom_bg=None, painter_index=0): self.draw_top = draw_top self.custom_bg = custom_bg self.tabs = [] HPanel.__init__(self, parent) SensitiveCanvas.__init__(self, check_move=True) self.set_double_buffered() self.set_panel_size() self.set_painter(painter_index)
def set_dialog_buttons(self): self.button_box = HPanel(self.box) self.box.pack(self.button_box, fill=True, padding_all=self.button_box_padding) self.button_box.pack(HPanel(self.button_box), fill=True, expand=True) self.cancel_btn = Button(self.button_box, '', onclick=self.on_cancel, default=True, pid=const.BUTTON_CANCEL) self.cancel_btn.set_enable(False) self.button_box.pack(self.cancel_btn) self.fit()
def __init__(self, parent, modes, icons, names, on_change=None, allow_none=False): self.modes = modes self.mode_buts = [] self.callback = on_change self.allow_none = allow_none HPanel.__init__(self, parent) for item in self.modes: but = ModeToggleButton(self, self, item, icons, names, self.changed, self.allow_none) self.mode_buts.append(but) self.pack(but)
def __init__(self, parent, modes, icons, names, on_change=None, allow_none=False): self.modes = modes self.mode_buts = [] self.callback = on_change self.allow_none = allow_none HPanel.__init__(self, parent) for item in self.modes: but = ModeToggleButton( self, self, item, icons, names, self.changed, self.allow_none) self.mode_buts.append(but) self.pack(but)
def set_dialog_buttons(self): if self.add_line: self.box.pack(HLine(self.box), fill=True, padding=5) else: self.box.pack((3, 3)) self.button_box = HPanel(self.box) self.box.pack(self.button_box, fill=True) self.close_btn = Button(self.button_box, '', onclick=self.on_close, default=True, pid=const.BUTTON_CLOSE) self.left_button_box = HPanel(self.button_box) self.button_box.pack(self.left_button_box, expand=True, fill=True) self.button_box.pack(self.close_btn, padding=5)
def set_dialog_buttons(self): if self.add_line: self.box.pack(HLine(self.box), fill=True, padding=5) else: self.box.pack((3, 3)) self.button_box = HPanel(self.box) self.box.pack(self.button_box, fill=True) self.ok_btn = Button(self.button_box, '', onclick=self.on_ok, pid=self.action_button) self.cancel_btn = Button(self.button_box, '', onclick=self.on_cancel, default=True, pid=const.BUTTON_CANCEL) self.left_button_box = HPanel(self.button_box) self.button_box.pack(self.left_button_box, expand=True, fill=True) if const.is_mac(): self.button_box.pack(self.ok_btn, padding=5) self.button_box.pack(self.cancel_btn, padding=5) elif const.is_msw(): self.button_box.pack(self.ok_btn, padding=2) self.button_box.pack(self.cancel_btn) else: self.button_box.pack(self.cancel_btn, padding=2) self.button_box.pack(self.ok_btn)
def __init__(self, parent, title, size=(-1, -1), style=VERTICAL, resizable=False, on_load=None, add_line=True, margin=None): stl = wx.DEFAULT_DIALOG_STYLE stl = stl | wx.RESIZE_BORDER | wx.MAXIMIZE_BOX if resizable else stl self.add_line = add_line wx.Dialog.__init__(self, parent, -1, tr(title), wx.DefaultPosition, size, style=stl) sizer = wx.BoxSizer(wx.VERTICAL) self.SetSizer(sizer) if margin is None: margin = 5 if const.IS_GTK else 10 self.box = VPanel(self) sizer.Add(self.box, 1, ALL | EXPAND, margin) if style == HORIZONTAL: self.panel = HPanel(self.box) else: self.panel = VPanel(self.box) self.box.pack(self.panel, expand=True, fill=True) self.build() self.set_dialog_buttons() if size == (-1, -1): self.Fit() self.CenterOnParent() self.panel.layout() self.Bind(wx.EVT_CLOSE, self.on_close, self) if on_load: self._timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, on_load) self._timer.Start(200)
def __init__(self, parent, value=0.0, range_val=(0.0, 1.0), step=0.01, digits=2, size=DEF_SIZE, width=0, spin_overlay=True, onchange=None, onenter=None, check_focus=True): self.callback = onchange self.enter_callback = onenter if const.is_mac(): spin_overlay = False if not width and const.is_msw(): width = 5 wx.Panel.__init__(self, parent) if spin_overlay: if const.is_gtk(): self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) size = (-1, self.entry.GetSize()[1]) self.entry.SetPosition((0, 0)) self.line = HPanel(self) self.sb = SpinButton(self, size=size, onchange=self._check_spin) w_pos = self.entry.GetSize()[0] - 5 self.line.SetSize((1, self.sb.GetSize()[1] - 2)) self.line.set_bg(const.UI_COLORS['dark_shadow']) self.line.SetPosition((w_pos - 1, 1)) self.sb.SetPosition((w_pos, 0)) self.SetSize((-1, self.entry.GetSize()[1])) elif const.is_msw(): width += 2 self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) size = (-1, self.entry.GetSize()[1] - 3) self.sb = SpinButton(self.entry, size=size, onchange=self._check_spin) w_pos = self.entry.GetSize()[0] - self.sb.GetSize()[0] - 3 self.sb.SetPosition((w_pos, 0)) w, h = self.entry.GetSize() self.entry.SetSize((w, h + 1)) else: self.box = wx.BoxSizer(const.HORIZONTAL) self.SetSizer(self.box) self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) self.box.Add(self.entry, 0, wx.ALL) size = (-1, self.entry.GetSize()[1]) self.sb = SpinButton(self, size=size, onchange=self._check_spin) self.box.Add(self.sb, 0, wx.ALL) if check_focus: self.entry.Bind(wx.EVT_KILL_FOCUS, self._entry_lost_focus, self.entry) self.entry.Bind(wx.EVT_CONTEXT_MENU, self._ctxmenu, self.entry) self.set_step(step) self.set_range(range_val) self._set_digits(digits) self._set_value(value) self.flag = False self.Fit()
class CloseDialog(SimpleDialog): def __init__(self, parent, title, size=(-1, -1), style=VERTICAL, resizable=True, on_load=None, add_line=True): SimpleDialog.__init__(self, parent, title, size, style, resizable, on_load, add_line) def set_dialog_buttons(self): if self.add_line: self.box.pack(HLine(self.box), fill=True, padding=5) else: self.box.pack((3, 3)) self.button_box = HPanel(self.box) self.box.pack(self.button_box, fill=True) self.close_btn = Button(self.button_box, '', onclick=self.on_close, default=True, pid=const.BUTTON_CLOSE) self.left_button_box = HPanel(self.button_box) self.button_box.pack(self.left_button_box, expand=True, fill=True) self.button_box.pack(self.close_btn, padding=5)
def __init__(self, parent, title, size=(-1, -1), style=VERTICAL, resizable=False, on_load=None, add_line=True): dlg_style = wx.DEFAULT_DIALOG_STYLE if resizable: dlg_style |= wx.RESIZE_BORDER self.add_line = add_line wx.Dialog.__init__(self, parent, -1, title, wx.DefaultPosition, size, style=dlg_style) sizer = wx.BoxSizer(wx.VERTICAL) self.SetSizer(sizer) margin = 5 if not const.is_gtk(): margin = 10 self.box = VPanel(self) sizer.Add(self.box, 1, ALL | EXPAND, margin) if style == HORIZONTAL: self.panel = HPanel(self.box) else: self.panel = VPanel(self.box) self.box.pack(self.panel, expand=True, fill=True) self.build() self.set_dialog_buttons() if size == (-1, -1): self.Fit() self.CenterOnParent() self.Bind(wx.EVT_CLOSE, self.on_close, self) if on_load: self._timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, on_load) self._timer.Start(500)
def refresh(self, **kwargs): self.arrange_tabs() HPanel.refresh(self)
class OkCancelDialog(SimpleDialog): sizer = None box = None button_box = None ok_btn = None cancel_btn = None action_button = None def __init__( self, parent, title, size=(-1, -1), style=VERTICAL, resizable=False, action_button=const.BUTTON_OK, on_load=None, add_line=True): self.action_button = action_button SimpleDialog.__init__( self, parent, title, size, style, resizable, on_load, add_line) def set_dialog_buttons(self): if self.add_line: self.box.pack(HLine(self.box), fill=True, padding=5) else: self.box.pack((3, 3)) self.button_box = HPanel(self.box) self.box.pack(self.button_box, fill=True) self.ok_btn = Button( self.button_box, '', onclick=self.on_ok, default=True, pid=self.action_button) self.cancel_btn = Button( self.button_box, '', onclick=self.on_cancel, pid=const.BUTTON_CANCEL) self.left_button_box = HPanel(self.button_box) self.button_box.pack(self.left_button_box, expand=True, fill=True) if const.IS_MAC: self.button_box.pack(self.ok_btn, padding=5) self.button_box.pack(self.cancel_btn, padding=5) elif const.IS_MSW: self.button_box.pack(self.ok_btn, padding=2) self.button_box.pack(self.cancel_btn) else: self.button_box.pack(self.cancel_btn, padding=2) self.button_box.pack(self.ok_btn) def on_ok(self): self.end_modal(const.BUTTON_OK) def on_cancel(self): self.end_modal(const.BUTTON_CANCEL) def show(self): ret = None if self.show_modal() == const.BUTTON_OK: ret = self.get_result() self.destroy() return ret
class FloatSpin(wx.Panel, RangeDataWidget): entry = None sb = None line = None flag = True ctxmenu_flag = False value = 0.0 range_val = (0.0, 1.0) step = 0.01 digits = 2 callback = None enter_callback = None def __init__(self, parent, value=0.0, range_val=(0.0, 1.0), step=0.01, digits=2, size=DEF_SIZE, width=0, spin_overlay=True, onchange=None, onenter=None, check_focus=True): self.callback = onchange self.enter_callback = onenter if const.is_mac(): spin_overlay = False if not width and const.is_msw(): width = 5 wx.Panel.__init__(self, parent) if spin_overlay: if const.is_gtk(): self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) size = (-1, self.entry.GetSize()[1]) self.entry.SetPosition((0, 0)) self.line = HPanel(self) self.sb = SpinButton(self, size=size, onchange=self._check_spin) w_pos = self.entry.GetSize()[0] - 5 self.line.SetSize((1, self.sb.GetSize()[1] - 2)) self.line.set_bg(const.UI_COLORS['dark_shadow']) self.line.SetPosition((w_pos - 1, 1)) self.sb.SetPosition((w_pos, 0)) self.SetSize((-1, self.entry.GetSize()[1])) elif const.is_msw(): width += 2 self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) size = (-1, self.entry.GetSize()[1] - 3) self.sb = SpinButton(self.entry, size=size, onchange=self._check_spin) w_pos = self.entry.GetSize()[0] - self.sb.GetSize()[0] - 3 self.sb.SetPosition((w_pos, 0)) w, h = self.entry.GetSize() self.entry.SetSize((w, h + 1)) else: self.box = wx.BoxSizer(const.HORIZONTAL) self.SetSizer(self.box) self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) self.box.Add(self.entry, 0, wx.ALL) size = (-1, self.entry.GetSize()[1]) self.sb = SpinButton(self, size=size, onchange=self._check_spin) self.box.Add(self.sb, 0, wx.ALL) if check_focus: self.entry.Bind(wx.EVT_KILL_FOCUS, self._entry_lost_focus, self.entry) self.entry.Bind(wx.EVT_CONTEXT_MENU, self._ctxmenu, self.entry) self.set_step(step) self.set_range(range_val) self._set_digits(digits) self._set_value(value) self.flag = False self.Fit() def set_enable(self, val): self.entry.Enable(val) self.sb.Enable(val) if not self.line is None: if val: self.line.set_bg(const.UI_COLORS['dark_shadow']) else: self.line.set_bg(const.UI_COLORS['light_shadow']) def get_enabled(self): return self.entry.IsEnabled() def _check_spin(self, event): if self.flag:return coef = pow(10, self.digits) dval = float(self.sb.get_value() - int(self.value * coef)) if not self.value == self._calc_entry(): self._set_value(self._calc_entry()) self.SetValue(dval * self.step + self.value) event.Skip() def _entry_enter(self): if self.flag:return self.SetValue(self._calc_entry()) if not self.enter_callback is None: self.enter_callback() def _ctxmenu(self, event): self.ctxmenu_flag = True event.Skip() def _entry_lost_focus(self, event): if not self.flag and not self.ctxmenu_flag: self.SetValue(self._calc_entry()) elif not self.flag and self.ctxmenu_flag: self.ctxmenu_flag = False event.Skip() def _check_entry(self): if self.flag:return txt = self.entry.get_value() res = '' for item in txt: chars = '.0123456789-+/*' if not self.digits: chars = '0123456789-+/*' if item in chars: res += item if not txt == res: self.flag = True self.entry.set_value(res) self.flag = False def _calc_entry(self): txt = self.entry.get_value() val = 0 try: line = 'val=' + txt code = compile(line, '<string>', 'exec') exec code except:return self.value return val def _check_in_range(self, val): minval, maxval = self.range_val if val < minval:val = minval if val > maxval:val = maxval coef = pow(10, self.digits) val = round(val * coef) / coef return val def _set_value(self, val): coef = pow(10, self.digits) self.value = self._check_in_range(val) if not self.digits: self.value = int(self.value) self.entry.set_value(str(self.value)) self.sb.set_value(int(self.value * coef)) def _set_digits(self, digits): self.digits = digits self.set_range(self.range_val) def set_value(self, val): self.flag = True self._set_value(val) self.flag = False #----- Native API emulation def SetValue(self, val): self.flag = True old_value = self.value self._set_value(val) self.flag = False if not self.callback is None and not self.value == old_value: self.callback() def GetValue(self): if not self.value == self._calc_entry(): self._set_value(self._calc_entry()) return self.value def SetRange(self, minval, maxval): coef = pow(10, self.digits) self.range_val = (minval, maxval) self.sb.set_range((int(minval * coef), int(maxval * coef))) #----- Control API def set_step(self, step): self.step = step def set_digits(self, digits): self._set_digits(digits) self.SetValue(self.value)
class CustomProgressDialog(SimpleDialog): label = None progressbar = None button_box = None cancel_btn = None result = None callback = None args = None msg = '' def __init__(self, parent, title, size=(500, 100), style=VERTICAL, resizable=False, action_button=const.BUTTON_CANCEL, add_line=False, margin=None, button_box_padding=0): self.label = title self.action_button = action_button self.button_box_padding = button_box_padding SimpleDialog.__init__(self, parent, title, size, style, resizable, self.on_load, add_line, margin) def build(self): self.panel.pack((5, 5)) self.label = Label(self.panel, self.label) self.panel.pack(self.label, fill=True) self.progressbar = ProgressBar(self.panel) self.panel.pack(self.progressbar, fill=True, padding=15) def set_dialog_buttons(self): self.button_box = HPanel(self.box) self.box.pack(self.button_box, fill=True, padding_all=self.button_box_padding) self.button_box.pack(HPanel(self.button_box), fill=True, expand=True) self.cancel_btn = Button(self.button_box, '', onclick=self.on_cancel, default=True, pid=const.BUTTON_CANCEL) self.cancel_btn.set_enable(False) self.button_box.pack(self.cancel_btn) self.fit() def on_cancel(self): self.end_modal(const.BUTTON_CANCEL) def show(self): self.show_modal() return self.result def update_data(self, value, msg): self.label.set_text(msg) self.label.Update() self.progressbar.set_value(value) self.progressbar.Update() self.Update() wx.Yield() def on_load(self, *args): self._timer.Stop() self.progressbar.set_value(5) try: if self.callback and self.args: self.result = self.callback(*self.args) except Exception as e: LOG.exception('Error in progress dialog running: %s', e) finally: self.progressbar.set_value(98) self.end_modal(const.BUTTON_CANCEL) def run(self, callback, args): self.callback, self.args = callback, args return self.show()
class MegaSpin(wx.Panel, RangeDataWidgetMixin): entry = None sb = None line = None flag = True ctxmenu_flag = False value = 0.0 range_val = (0.0, 1.0) step = 0.01 digits = 2 callback = None enter_callback = None def __init__(self, parent, value=0.0, range_val=(0.0, 1.0), step=0.01, digits=2, size=DEF_SIZE, width=5, onchange=None, onenter=None, check_focus=True): self.callback = onchange self.enter_callback = onenter spin_overlay = const.SPIN['overlay'] spin_sep = const.SPIN['sep'] if const.IS_MAC: spin_overlay = False if not width and const.IS_MSW: width = 5 wx.Panel.__init__(self, parent) if spin_overlay: if const.IS_GTK: self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) size = (-1, self.entry.GetSize()[1]) self.entry.SetPosition((0, 0)) self.sb = SpinButton(self, size=size, onchange=self._check_spin) w_pos = self.entry.GetSize()[0] - 5 if spin_sep: self.line = HPanel(self) self.line.SetSize((1, self.sb.GetSize()[1] - 2)) self.line.set_bg(const.UI_COLORS['dark_shadow']) self.line.SetPosition((w_pos - 1, 1)) self.sb.SetPosition((w_pos, 0)) self.SetSize((-1, self.entry.GetSize()[1])) elif const.IS_MSW: width += 2 self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) size = (-1, self.entry.GetSize()[1] - 3) self.sb = SpinButton(self.entry, size=size, onchange=self._check_spin) w_pos = self.entry.GetSize()[0] - self.sb.GetSize()[0] - 3 self.sb.SetPosition((w_pos, 0)) w, h = self.entry.GetSize() self.entry.SetSize((w, h + 1)) else: self.box = wx.BoxSizer(const.HORIZONTAL) self.SetSizer(self.box) self.entry = Entry(self, '', size=size, width=width, onchange=self._check_entry, onenter=self._entry_enter) self.box.Add(self.entry, 0, wx.ALL) size = (-1, self.entry.GetSize()[1]) self.sb = SpinButton(self, size=size, onchange=self._check_spin) self.box.Add(self.sb, 0, wx.ALL) if check_focus: self.entry.Bind(wx.EVT_KILL_FOCUS, self._entry_lost_focus, self.entry) self.entry.Bind(wx.EVT_CONTEXT_MENU, self._ctxmenu, self.entry) self.set_step(step) self.set_range(range_val) self._set_digits(digits) self._set_value(value) self.flag = False self.Fit() def set_enable(self, val): self.entry.Enable(val) self.sb.Enable(val) if self.line is not None: if val: self.line.set_bg(const.UI_COLORS['dark_shadow']) else: self.line.set_bg(const.UI_COLORS['light_shadow']) def get_enabled(self): return self.entry.IsEnabled() def _check_spin(self, event): if self.flag: return coef = pow(10, self.digits) dval = float(self.sb.get_value() - int(self.value * coef)) if not self.value == self._calc_entry(): self._set_value(self._calc_entry()) self.SetValue(dval * self.step + self.value) event.Skip() def _entry_enter(self): if self.flag: return self.SetValue(self._calc_entry()) if self.enter_callback is not None: self.enter_callback() def _ctxmenu(self, event): self.ctxmenu_flag = True event.Skip() def _entry_lost_focus(self, event): if not self.flag and not self.ctxmenu_flag: self.SetValue(self._calc_entry()) elif not self.flag and self.ctxmenu_flag: self.ctxmenu_flag = False event.Skip() def _check_entry(self): if self.flag: return txt = self.entry.get_value() res = '' for item in txt: chars = '.0123456789-+/*' if not self.digits: chars = '0123456789-+/*' if item in chars: res += item if not txt == res: self.flag = True self.entry.set_value(res) self.flag = False def _calc_entry(self): txt = self.entry.get_value() val = 0 try: line = 'val=' + txt code = compile(line, '<string>', 'exec') exec code except Exception: return self.value return val def _check_in_range(self, val): minval, maxval = self.range_val if val < minval: val = minval if val > maxval: val = maxval coef = pow(10, self.digits) val = round(val * coef) / coef return val def _set_value(self, val): coef = pow(10, self.digits) self.value = self._check_in_range(val) if not self.digits: self.value = int(self.value) self.entry.set_value(str(self.value)) self.sb.set_value(int(self.value * coef)) def _set_digits(self, digits): self.digits = digits self.set_range(self.range_val) def set_value(self, val): self.flag = True self._set_value(val) self.flag = False # ----- Native API emulation def SetValue(self, val): self.flag = True old_value = self.value self._set_value(val) self.flag = False if self.callback is not None and not self.value == old_value: self.callback() def GetValue(self): if not self.value == self._calc_entry(): self._set_value(self._calc_entry()) return self.value def SetRange(self, minval, maxval): coef = pow(10, self.digits) self.range_val = (minval, maxval) self.sb.set_range((int(minval * coef), int(maxval * coef))) # ----- Control API def set_step(self, step): self.step = step def set_digits(self, digits): self._set_digits(digits) self.SetValue(self.value)
class OkCancelDialog(SimpleDialog): sizer = None box = None button_box = None ok_btn = None cancel_btn = None action_button = None def __init__(self, parent, title, size=(-1, -1), style=VERTICAL, resizable=False, action_button=const.BUTTON_OK, on_load=None, add_line=True): self.action_button = action_button SimpleDialog.__init__(self, parent, title, size, style, resizable, on_load, add_line) def set_dialog_buttons(self): if self.add_line: self.box.pack(HLine(self.box), fill=True, padding=5) else: self.box.pack((3, 3)) self.button_box = HPanel(self.box) self.box.pack(self.button_box, fill=True) self.ok_btn = Button(self.button_box, '', onclick=self.on_ok, pid=self.action_button) self.cancel_btn = Button(self.button_box, '', onclick=self.on_cancel, default=True, pid=const.BUTTON_CANCEL) self.left_button_box = HPanel(self.button_box) self.button_box.pack(self.left_button_box, expand=True, fill=True) if const.is_mac(): self.button_box.pack(self.ok_btn, padding=5) self.button_box.pack(self.cancel_btn, padding=5) elif const.is_msw(): self.button_box.pack(self.ok_btn, padding=2) self.button_box.pack(self.cancel_btn) else: self.button_box.pack(self.cancel_btn, padding=2) self.button_box.pack(self.ok_btn) def on_ok(self): self.end_modal(const.BUTTON_OK) def on_cancel(self): self.end_modal(const.BUTTON_CANCEL) def show(self): ret = None if self.show_modal() == const.BUTTON_OK: ret = self.get_result() self.destroy() return ret