代码之家  ›  专栏  ›  技术社区  ›  Joshua Schlichting

wxPython的wx.grid.grid()不会完全进入视野

  •  0
  • Joshua Schlichting  · 技术社区  · 6 年前

    发行日期:

    我遇到了一个问题,一个简单地创建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() 功能?

    提前谢谢!

    0 回复  |  直到 6 年前
        1
  •  0
  •   Rolf of Saxony    6 年前

    你在哪里 panel.SetSizer(sizer) 或者使用:

    panel.SetSizerAndFit(sizer)
    

    或使用:

    panel.SetSizer(sizer)
    panel.Fit()