dtechplus
dtechplus

Reputation: 599

Dynamically update wxPython wx.TimeCtrl widget

I am trying to get a wxPython TimeCtrl widget to toggle it's value 12hr (AM/PM) format to 24 hour format at the click of a button.

Now I know the TimeCtrl widget 12/24hr formatting is set at initialization by setting the attribute fmt24hr = False/True depending on if you want to display 24hr format or not.

Here's the API for TimeCtrl::

from wx.lib.masked import TimeCtrl

TimeCtrl(
     parent, id = -1,
     value = '00:00:00',
     pos = wx.DefaultPosition,
     size = wx.DefaultSize,
     style = wxTE_PROCESS_TAB,
     validator = wx.DefaultValidator,
     name = "time",
     format = 'HHMMSS',
     fmt24hr = False,
     displaySeconds = True,
     spinButton = None,
     min = None,
     max = None,
     limited = None,
     oob_color = "Yellow"
    )

Now I want to toggle fmt24hr from True to False when a button is clicked but I cannot find a way! As far as I can see in the docs there is no setter for it.

Any help will be appreciated.

Upvotes: 0

Views: 456

Answers (1)

Mike Driscoll
Mike Driscoll

Reputation: 33071

I am guessing that that is an attribute you cannot change after the widget is created. You have two options:

  1. Create both controls and hide one and when you toggle you hide the first one and show the second
  2. Create the first control and then destroy it when "toggle" it and create the other version in its place.

Either way, you'll almost certainly want to use Sizer methods to make it appear and disappear in the right location. If you're destroying it, then you'll want Sizer.Remove and Sizer.Insert. If you're just showing and hiding, then you can use the Sizer's Show and Hide methods. You may also want to look into the panel's Freeze and Thaw methods to reduce the amount of flicker.

Upvotes: 1

Related Questions