代码之家  ›  专栏  ›  技术社区  ›  Jon

wxpython应用程序启动时显示空白屏幕

  •  0
  • Jon  · 技术社区  · 6 年前

    我有Wxpython申请表。它只是一个功能不重要的示例应用程序。

    我可以在我的Ubuntu桌面上成功地启动程序,但是在Windows10上,我会收到一个初始的空白屏幕。直到我调整屏幕大小 轻微地 显示应用程序的内容。

    这个问题有具体的原因吗?

    代码

    import wx
    
    class MyFrame(wx.Frame):
        """ We simply derive a new class of Frame. """
        def __init__(self, parent, title):
            super().__init__(parent, title=title, size=(600,400)) 
            self.Show(True)
    
    
    class ExamplePanel(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
    
            # create some sizers
            mainSizer = wx.BoxSizer(wx.VERTICAL)
            grid = wx.GridBagSizer(hgap=5, vgap=5)
            hSizer = wx.BoxSizer(wx.HORIZONTAL)
    
            self.quote = wx.StaticText(self, label="Your quote: ")
            grid.Add(self.quote, pos=(0,0))
    
            # A multiline TextCtrl - This is here to show how the events work in this program, don't pay too much attention to it
            self.logger = wx.TextCtrl(self, size=(200,300), style=wx.TE_MULTILINE | wx.TE_READONLY)
    
            # A button
            self.button =wx.Button(self, label="Save")
    
            # the edit control - one line version.
            self.lblname = wx.StaticText(self, label="Your name :")
            grid.Add(self.lblname, pos=(1,0))
            self.editname = wx.TextCtrl(self, value="Enter here your name", size=(140,-1))
            grid.Add(self.editname, pos=(1,1))
    
    
            # the combobox Control
            self.sampleList = ['friends', 'advertising', 'web search', 'Yellow Pages']
            self.lblhear = wx.StaticText(self, label="How did you hear from us ?")
            grid.Add(self.lblhear, pos=(3,0))
            self.edithear = wx.ComboBox(self, size=(95, -1), choices=self.sampleList, style=wx.CB_DROPDOWN)
            grid.Add(self.edithear, pos=(3,1))
    
            # add a spacer to the sizer
            grid.Add((10, 40), pos=(2,0))
    
            # Checkbox
            self.insure = wx.CheckBox(self, label="Do you want Insured Shipment ?")
            grid.Add(self.insure, pos=(4,0), span=(1,2), flag=wx.BOTTOM, border=5)
    
            # Radio Boxes
            radioList = ['blue', 'red', 'yellow', 'orange', 'green', 'purple', 'navy blue', 'black', 'gray']
            rb = wx.RadioBox(self, label="What color would you like ?", pos=(20, 210), choices=radioList,  majorDimension=3,
                            style=wx.RA_SPECIFY_COLS)
            grid.Add(rb, pos=(5,0), span=(1,2))
    
            hSizer.Add(grid, 0, wx.ALL, 5)
            hSizer.Add(self.logger)
            mainSizer.Add(hSizer, 0, wx.ALL, 5)
            mainSizer.Add(self.button, 0, wx.CENTER)
            self.SetSizerAndFit(mainSizer)
    
    
    
    if __name__ == "__main__":
        app = wx.App(useBestVisual=True)
        frame = MyFrame(None, title="Demo with Notebook")
        nb = wx.Notebook(frame)
    
        nb.AddPage(ExamplePanel(nb), "Absolute Positioning")
        nb.AddPage(ExamplePanel(nb), "Page Two")
        nb.AddPage(ExamplePanel(nb), "Page Three")
        frame.Show()
        app.MainLoop()
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Jon    6 年前

    我发现解决方案是编写更干净、更有组织的代码。除了清洁剂代码,我在 MainFrame .

    # finally, put the notebook in a sizer for the panel to manage
    # the layout
    sizer = wx.BoxSizer()
    sizer.Add(nb, 1, wx.EXPAND)
    p.SetSizer(sizer)
    sizer.Fit(p)
    p.Layout()
    

    工作代码如下:

    import wx
    
    
    # Some classes to use for the notebook pages.  Obviously you would
    # want to use something more meaningful for your application, these
    # are just for illustration.
    
    class ExamplePanel(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
    
            # create some sizers
            mainSizer = wx.BoxSizer(wx.VERTICAL)
            grid = wx.GridBagSizer(hgap=5, vgap=5)
            hSizer = wx.BoxSizer(wx.HORIZONTAL)
    
            self.quote = wx.StaticText(self, label="Your quote: ")
            grid.Add(self.quote, pos=(0,0))
    
            # A multiline TextCtrl - This is here to show how the events work in this program, don't pay too much attention to it
            self.logger = wx.TextCtrl(self, size=(200,300), style=wx.TE_MULTILINE | wx.TE_READONLY)
    
            # A button
            self.button =wx.Button(self, label="Save")
    
            # the edit control - one line version.
            self.lblname = wx.StaticText(self, label="Your name :")
            grid.Add(self.lblname, pos=(1,0))
            self.editname = wx.TextCtrl(self, value="Enter here your name", size=(140,-1))
            grid.Add(self.editname, pos=(1,1))
    
    
            # the combobox Control
            self.sampleList = ['friends', 'advertising', 'web search', 'Yellow Pages']
            self.lblhear = wx.StaticText(self, label="How did you hear from us ?")
            grid.Add(self.lblhear, pos=(3,0))
            self.edithear = wx.ComboBox(self, size=(95, -1), choices=self.sampleList, style=wx.CB_DROPDOWN)
            grid.Add(self.edithear, pos=(3,1))
    
            # add a spacer to the sizer
            grid.Add((10, 40), pos=(2,0))
    
            # Checkbox
            self.insure = wx.CheckBox(self, label="Do you want Insured Shipment ?")
            grid.Add(self.insure, pos=(4,0), span=(1,2), flag=wx.BOTTOM, border=5)
    
            # Radio Boxes
            radioList = ['blue', 'red', 'yellow', 'orange', 'green', 'purple', 'navy blue', 'black', 'gray']
            rb = wx.RadioBox(self, label="What color would you like ?", pos=(20, 210), choices=radioList,  majorDimension=3,
                            style=wx.RA_SPECIFY_COLS)
            grid.Add(rb, pos=(5,0), span=(1,2))
    
            hSizer.Add(grid, 0, wx.ALL, 5)
            hSizer.Add(self.logger)
            mainSizer.Add(hSizer, 0, wx.ALL, 5)
            mainSizer.Add(self.button, 0, wx.CENTER)
            self.SetSizerAndFit(mainSizer)
    
    
    class MainFrame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, title="Simple Notebook Example")
    
            # Here we create a panel and a notebook on the panel
            p = wx.Panel(self)
            nb = wx.Notebook(p)
    
            # create the page windows as children of the notebook
            page1 = ExamplePanel(nb)
            page2 = ExamplePanel(nb)
            page3 = ExamplePanel(nb)
    
            # add the pages to the notebook with the label to show on the tab
            nb.AddPage(page1, "Page 1")
            nb.AddPage(page2, "Page 2")
            nb.AddPage(page3, "Page 3")
    
            # finally, put the notebook in a sizer for the panel to manage
            # the layout
            sizer = wx.BoxSizer()
            sizer.Add(nb, 1, wx.EXPAND)
            p.SetSizer(sizer)
            sizer.Fit(p)
            p.Layout()
    
    
    if __name__ == "__main__":
        app = wx.App()
        MainFrame().Show()
        app.MainLoop()