wxWidgets:对话级别的sizer没有考虑到孩子ScrolledWindow的大小?

时间:2016-10-12 09:48:06

标签: python wxpython wxwidgets wxformbuilder

我在其中有一个ScrolledWindow对话框,并在该ScrolledWindow中有一个动态数量的控件。

对话框目前还不够大。我可以手动设置宽度和高度,但我不知道它们会是什么,所以我希望sizer自动设置大小,我相信wxWidgets希望你这样做。< / p>

ScrolledWindow有一个sizer,它似乎正确地计算了尺寸要求。

我已将ScrolledWindow添加到Dialog的sizer中,但Dialog的sizer似乎没有考虑到ScrolledWindow的大小。大小计算,因此小于ScrolledWindow的sizer认为它应该是。

下面的日志演示了我的问题。为什么scrolling_window和scrolling_window_sizer认为它们的大小是(267,480),但是dialog_sizer认为它可以逃脱(210,63)?为什么?

scrolling window best size (14, 14)
scrolling window client size (267, 480)
scrolling window effect min size (14, 14)
scrolling window min client size (-1, -1)
scrolling window min size (-1, -1)
scrolling window size (267, 480)
scrolling window virtual size (267, 480)
scrolling window best virtual size (267, 480)

scrolling_window_sizer after layout fit GetSize (267, 480)
scrolling_window_sizer after layout fit GetMinSize (267, 480)
scrolling_window_sizer after layout fit ComputeFittingClientSize(self) (267, 480)
scrolling_window_sizer after layout fit ComputeFittingWindowSize(self) (267, 480)

dialog_sizer after add scrolling window GetSize (0, 0)
dialog_sizer after add scrolling window GetMinSize (24, 24)
dialog_sizer after add scrolling window ComputeFittingClientSize(self) (24, 24)
dialog_sizer after add scrolling window ComputeFittingWindowSize(self) (24, 24)
dialog_sizer after add buttons GetSize (0, 0)
dialog_sizer after add buttons GetMinSize (210, 63)
dialog_sizer after add buttons ComputeFittingClientSize(self) (210, 63)
dialog_sizer after add buttons ComputeFittingWindowSize(self) (210, 63)

dialog best size (210, 63)
dialog client size (210, 63)
dialog effect min size (210, 63)
dialog min client size (-1, -1)
dialog min size (-1, -1)
dialog size (210, 63)
dialog virtual size (210, 63)
dialog best virtual size (210, 63)
Traceback (most recent call last):
  File "gui/klt_gui.py", line 208, in on_cancel_button_click
    raise NotImplementedError
NotImplementedError

请注意,这是使用wxPython的python,但我希望这是我使用wxWidgets而不是wxPython的一般问题。这是一个很大的代码转储,但希望它能够直接读取。打印上面日志的行标有#!。

这一行:             #这行是个问题。没有它,我不认为sizer会自动调整窗口。有了它,窗户很小。             size = dialog_sizer.Fit(self)             self.Layout()

我添加了自己,以便让sizer重新调整对话框的大小。我不确定我是否应该在布局前或布局后进行适合,但它似乎并不重要,因为正如上面的日志所示,sizer似乎没有考虑到按钮但不是滚动窗口。

class AddEditCustomerDialog(wx.Dialog):
    # Based on EditCustomerTemplateFrame generated by wxFrameBuilder

    @staticmethod
    def make_section_frame(parent, parent_sizer, section_name, field_names):
        static_box = wx.StaticBox(parent, wx.ID_ANY, section_name)
        section_static_box_sizer = wx.StaticBoxSizer(static_box, wx.VERTICAL)

        flex_grid = wx.FlexGridSizer(0, 2, 5, 5)
        flex_grid.AddGrowableCol(1)
        flex_grid.SetFlexibleDirection(wx.HORIZONTAL)
        flex_grid.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_ALL)

        # do we need to keep these around? I guess for getting text etc
        text_boxes = []

        for field_name in field_names:
            label = wx.StaticText(static_box, label=field_name)
            label.Wrap(-1)
            flex_grid.Add(label, 0,
                          wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT | wx.ALL,
                          5)

            # value=, validator=, name =??
            text_box = wx.TextCtrl(static_box)
            flex_grid.Add(text_box, 1, wx.ALL | wx.EXPAND, 5)

        section_static_box_sizer.Add(flex_grid, 1, wx.ALL | wx.EXPAND, 5)
        parent_sizer.Add(section_static_box_sizer, 1, wx.ALL | wx.EXPAND, 5)

    def add_dialog_buttons(self, add_or_edit, parent, parent_sizer):
        # ok, cancel buttons:
        dialog_buttons = wx.StdDialogButtonSizer()

        self.save_button = wx.Button(parent, wx.ID_SAVE)
        self.cancel_button = wx.Button(parent, wx.ID_CANCEL)
        dialog_buttons.AddButton(self.save_button)
        dialog_buttons.AddButton(self.cancel_button)

        if add_or_edit == "Edit":
            self.apply_button = wx.Button(parent, wx.ID_APPLY)
            dialog_buttons.AddButton(self.apply_button)

        dialog_buttons.Realize()
        parent_sizer.Add(dialog_buttons, 0,
                         wx.ALIGN_BOTTOM | wx.ALIGN_RIGHT | wx.ALL | wx.EXPAND,
                         5)

        # Connect Events
        if add_or_edit == "Edit":
            self.apply_button.Bind(wx.EVT_BUTTON, self.on_apply_button_click)
        self.cancel_button.Bind(wx.EVT_BUTTON, self.on_cancel_button_click)
        self.save_button.Bind(wx.EVT_BUTTON, self.on_save_button_click)

    def __init__(self, parent, add_or_edit, config):
        assert add_or_edit in ['Add', 'Edit']

        wx.Dialog.__init__(self, parent,
                           title=add_or_edit + " Customer",
                           style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER | wx.STAY_ON_TOP)

        self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
        dialog_sizer = wx.BoxSizer(wx.VERTICAL)

        scrolling_window = wx.ScrolledWindow(self, wx.ID_ANY,
                                             wx.DefaultPosition,
                                             wx.DefaultSize,
                                             wx.HSCROLL | wx.VSCROLL)
        scrolling_window.SetScrollRate(5, 5)
        scrolling_window_sizer = wx.BoxSizer(wx.VERTICAL)

        self.make_section_frame(scrolling_window,
                                scrolling_window_sizer,
                                "LOL", ["test", "two", "three"])
        self.make_section_frame(scrolling_window,
                                scrolling_window_sizer,
                                "rofl", ["12323 3 2 1 3 21 3 2", "abc", "123"])
        self.make_section_frame(scrolling_window,
                                scrolling_window_sizer,
                                "heehhee", ["sd f  ", "fdsf dsd ", "fff"])

        scrolling_window.SetSizer(scrolling_window_sizer)
        scrolling_window.Layout()
        scrolling_window_sizer.Fit(scrolling_window)
        self.print_window_sizes(scrolling_window, "scrolling window") #!

        self.print_sizer(scrolling_window_sizer, scrolling_window,
                         "scrolling_window_sizer after layout fit") #!
        dialog_sizer.Add(scrolling_window, 1, wx.ALL | wx.EXPAND, 5)
        self.print_sizer(dialog_sizer, self,
                         "dialog_sizer after add scrolling window") #!

        self.add_dialog_buttons(add_or_edit, self, dialog_sizer)

        self.SetSizer(dialog_sizer)

        self.print_sizer(dialog_sizer, self, "dialog_sizer after add buttons") #!


        # This line is a problem. Without it, I don't think the sizer will automatically adjust the window. With it, the window is tiny.
        size = dialog_sizer.Fit(self)

        self.Layout()
        self.Centre(wx.BOTH)
        self.print_window_sizes(self, "dialog") #!


    @staticmethod
    def print_window_sizes(scrolling_window, window_name):
        print(window_name, "best size", scrolling_window.GetBestSize())
        print(window_name, "client size", scrolling_window.GetClientSize())
        print(window_name, "effect min size", scrolling_window.GetEffectiveMinSize())
        print(window_name, "min client size", scrolling_window.GetMinClientSize())
        print(window_name, "min size", scrolling_window.GetMinSize())
        print(window_name, "size", scrolling_window.GetSize())
        print(window_name, "virtual size", scrolling_window.GetVirtualSize())
        print(window_name, "best virtual size", scrolling_window.GetBestVirtualSize())

    @staticmethod
    def print_sizer(sizer, window, sizer_name):
        print(sizer_name, "GetSize", sizer.GetSize())
        print(sizer_name, "GetMinSize", sizer.GetMinSize())
        print(sizer_name, "ComputeFittingClientSize(self)",
              sizer.ComputeFittingClientSize(window))
        print(sizer_name, "ComputeFittingWindowSize(self)",
              sizer.ComputeFittingWindowSize(window))

    def on_apply_button_click(self, event):
        event.Skip()
        raise NotImplementedError

    def on_cancel_button_click(self, event):
        event.Skip()
        raise NotImplementedError

    def on_save_button_click(self, event):
        event.Skip()
        raise NotImplementedError

app = wx.App(False)
AddEditCustomerDialog(None, "Add", None).Show(True)
app.MainLoop()

代码本身主要由wxFormBuilder生成,我自己进行了修改以使其动态化。我假设wxFormBuilder生成的代码是正确的。但是在这里,你认为这是错误的,并且做错了一些事情,或者我对它进行了严厉的修改;)

.fbp:https://gist.github.com/Poddster/bf203294e0267d3da575f297fa5bb0a6
输出代码:https://gist.github.com/Poddster/684896817452c3657ad65f86263a0a7a

0 个答案:

没有答案