简体   繁体   English

wxPython 中的 wx.adv.CalendarCtrl 不适用于 sizer

[英]wx.adv.CalendarCtrl in wxPython does not work with sizers

I would need add calendar to my small utility.我需要将日历添加到我的小型实用程序中。 I have a hello world code like this:我有这样一个你好世界代码:

import wx
import wx.stc as stc
import wx.adv as adv

class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None, title='My fancy form app with calendar')
        panel = wx.Panel(self)
        my_sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetMinSize(wx.Size(400,450))

        # Headline Text
        self.headline_text = wx.StaticText(panel, style = wx.TE_CENTER & ~wx.TE_LEFT & ~wx.TE_RIGHT, label="This text will be placed to the top of my form")
        my_sizer.Add(self.headline_text, 0, wx.ALL | wx.EXPAND, 5)

        # Calendar under the text
        self.cal = adv.CalendarCtrl(self, 10, wx.DateTime.Now())
        my_sizer.Add(self.cal, 0, wx.ALL | wx.CENTER, 5)


if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()

Without the block of code with Calendar, this works well.没有带有日历的代码块,这工作得很好。 BoxSizer can arrange buttons, text labels, tables etc. - But the calendar kills the sizer and everything is in the top left corner. BoxSizer 可以安排按钮、文本标签、表格等。 - 但日历会扼杀 sizer,一切都在左上角。

So please, what is the proper usage of CalendarCtrl object?那么请问,CalendarCtrl object 的正确用法是什么? Thanks!谢谢!

You were placing the calendar on the frame你把日历放在相框上

self.cal = adv.CalendarCtrl(self ...

and the text on the panel和面板上的文字

self.headline_text = wx.StaticText(panel ...
  • all widgets should really go on panels.面板上的所有小部件实际上应该是 go。

I have also moved all of your widgets to a panel class which helps with separation of concerns我还将您所有的小部件移至面板 class,这有助于分离关注点

import wx
import wx.stc as stc
import wx.adv as adv

class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None, title='My fancy form app with calendar')
        panel = wx.Panel(self)
        self.SetMinSize(wx.Size(400,450))
        self.panel = MainPanel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel)
        self.SetSizer(sizer)
        self.Center()
        self.Show()

class MainPanel(wx.Panel):
    """Create a panel class to contain screen widgets."""
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        sizer = wx.BoxSizer(wx.VERTICAL)
        # Headline Text
        self.headline_text = wx.StaticText(self, style = wx.TE_CENTER & ~wx.TE_LEFT & ~wx.TE_RIGHT, label="This text will be placed to the top of my form")
        sizer.Add(self.headline_text, 0, wx.ALL | wx.EXPAND, 5)

        # Calendar under the text
        self.cal = adv.CalendarCtrl(self, 10, wx.DateTime.Now())
        sizer.Add(self.cal, 0, wx.ALL | wx.CENTER, 5)
        self.SetSizer(sizer)


if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()

在此处输入图像描述

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

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