简体   繁体   English

如何在 wxpython 的 textctrl 中添加和编辑时间?

[英]How to add and edit time in a textctrl in wxpython?

I am developing a GUI using wxpython where i need a textctrl which selects the time .I tried with TimePickerCtrl but failed to fetch the time into the textctrl.我正在使用 wxpython 开发 GUI,我需要一个 textctrl 来选择时间。我尝试使用 TimePickerCtrl 但未能将时间提取到 textctrl 中。 It would be great if anyone shares a good example code which adds a time to a textctrl and can be edit the textctrl at any time.Thanks in advance.如果有人分享一个很好的示例代码,它会为 textctrl 添加时间并且可以随时编辑 textctrl,那就太好了。提前致谢。

Did you even look at the wxPython demo?你甚至看过 wxPython 的演示吗? It shows 3 different ways to create the picker control:它显示了 3 种不同的方式来创建选择器控件:

import wx
import wx.lib.masked as masked

class MyPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        # 12-hour format
        text1 = wx.StaticText( self, -1, "12-hour format:", size=(150,-1))
        self.time12 = masked.TimeCtrl( self, -1, name="12 hour control" )
        h = self.time12.GetSize().height
        spin1 = wx.SpinButton( 
                  self, -1, wx.DefaultPosition, (-1,h), wx.SP_VERTICAL )
        self.time12.BindSpinButton( spin1 )
        self.addWidgets([text1, self.time12, spin1])
        # 24-hour format
        text2 = wx.StaticText( self, -1, "24-hour format:")
        spin2 = wx.SpinButton(
                  self, -1, wx.DefaultPosition, (-1,h), wx.SP_VERTICAL )
        self.time24 = masked.TimeCtrl(
                        self, -1, name="24 hour control", fmt24hr=True,
                        spinButton = spin2
                        )
        self.addWidgets([text2, self.time24, spin2])
        # No seconds\nor spin button
        text3 = wx.StaticText( self, -1, "No seconds\nor spin button:")
        self.spinless_ctrl = masked.TimeCtrl(
                                self, -1, name="spinless control",
                                display_seconds = False
                                )
        self.addWidgets([text3, self.spinless_ctrl])
        # set sizer
        self.SetSizer(self.mainSizer)

    def addWidgets(self, widgets):
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        for widget in widgets:
            if isinstance(widget, wx.StaticText):
                sizer.Add(widget, 0, wx.ALL|wx.CENTER, 5),
            else:
                sizer.Add(widget, 0, wx.ALL, 5)
        self.mainSizer.Add(sizer)

class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title="Spinner Demo")
        panel = MyPanel(self)
        self.Show()

if __name__ == "__main__":        
    app = wx.App(False)
    f = MyFrame()
    app.MainLoop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM