wxPython的wx.grid.Grid()将无法完全进入视图

问题描述 投票:0回答:1

问题:

我遇到的问题是,在一个地方调用时,只是创建Grid()的函数可以工作,而不是在另一个地方调用。当从“其他”非工作场所调用它时,它会在窗口的角落创建一个非常小的正方形。在这个时候,我不明白为什么,我希望有人可以提供帮助。

代码:(随意复制粘贴到文本编辑器中,然后开始吧!)

import wx
import wx.grid as gridlib


class MainFrame(wx.Frame):

    def __init__(self, parent, title):
        super(MainFrame, self).__init__(parent, title=title,
                                        size=(350, 250))
        self.init_ux()
        self.main_grid = None

    def init_ux(self):
        menu_bar = wx.MenuBar()
        file_menu = wx.Menu()

        file_menu.AppendSeparator()
        menu_bar.Append(file_menu, '&File')

        # add open file menu
        open_menu = wx.Menu()
        my_btn = open_menu.Append(wx.ID_ANY, 'button description')

        # append open_menu to the file_menu
        file_menu.Append(wx.ID_OPEN, '&Open', open_menu)
        self.SetMenuBar(menu_bar)
        self.Bind(wx.EVT_MENU, lambda event: self.open_dialog(data="i love string literals."), my_btn)
        self.SetSize((300, 200))
        self.Centre()
        # the create_grid_view() below DOES work when uncommented
        #self.create_grid_view(10, 10)

    def create_grid_view(self, row_count, col_count):
        print("Creating grid view!")
        # set up grid panel
        panel = wx.Panel(self)
        self.main_grid = gridlib.Grid(panel)
        self.main_grid.CreateGrid(row_count, col_count)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.main_grid, 1, wx.EXPAND)
        panel.SetSizer(sizer)

    def open_dialog(self, data):

        # data is being used for populating wildcard, etc

        with wx.FileDialog(self, "Open a file!",
                           style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:

            if fileDialog.ShowModal() == wx.ID_CANCEL:
                return

            file_path = fileDialog.GetPath()
            try:
                # here, I do some fun data "things" with the file_path
                # open it, use other functions, etc.
                # afterwards, I need to create a grid
                self.create_grid_view(10, 10)
                # ^^ This creates a small square instead of a grid.

            except IOError:
                wx.LogError("Cannot open file '%s'." % file_path)


if __name__ == "__main__":
    app = wx.App()

    frame = MainFrame(None, title='Window Title :)')
    frame.Show()

    app.MainLoop()

期望:

enter image description here

实际结果:

enter image description here

摘要:

为什么create_grid_view()函数在从init_ux()函数调用时显示正确的网格,而不是open_dialog()函数?

提前致谢!

python gridview datagrid grid wxpython
1个回答
0
投票

你有panel.SetSizer(sizer)使用的地方:

panel.SetSizerAndFit(sizer)

或使用:

panel.SetSizer(sizer)
panel.Fit()
© www.soinside.com 2019 - 2024. All rights reserved.