wxpython面板大小和标题

2024-06-06 13:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

当我应用以下代码时,第二个面板显示在第一个面板上。我想知道如何才能得到两个面板没有重叠和适当大小的窗口(框架)。我还想知道是否有任何合适的方法可以在面板中绘制某种标题(在本例中是“Inputs:”和“Outputs:”)。谢谢!

import wx

class Input_Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # Input variables
        self.tittle1 = wx.StaticText(self, label="Inputs:")    
        self.lblname1 = wx.StaticText(self, label="Input 1:")
        self.format1 = ['Option 1','Option 2']
        self.combo1 = wx.ComboBox(self, size=(200, -1),value='', choices=self.format1, style=wx.CB_DROPDOWN)
        self.lblname2 = wx.StaticText(self, label="Input 2")
        self.format2 = ['Option 1','Option 2', 'Option 3']
        self.combo2 = wx.ComboBox(self, size=(200, -1),value='', choices=self.format2, style=wx.CB_DROPDOWN)

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(2, 2)
        self.sizer.Add(self.tittle1, (1, 2))
        self.sizer.Add(self.lblname1, (2, 1))
        self.sizer.Add(self.combo1, (2, 2))
        self.sizer.Add(self.lblname2, (3, 1))
        self.sizer.Add(self.combo2, (3, 2))
        self.SetSizer(self.sizer)

class Output_Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # Output variables
        self.tittle2 = wx.StaticText(self, label="Outputs:")    
        self.lblname3 = wx.StaticText(self, label="Output1")
        self.result3 = wx.StaticText(self, label="", size=(100, -1))

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(2, 2)
        self.sizer.Add(self.tittle2, (1, 2))
        self.sizer.Add(self.lblname3, (2, 1))
        self.sizer.Add(self.result3, (2, 2))
        self.SetSizer(self.sizer)

class Main_Window(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title)

        # Set variable panels
        self.splitter = wx.SplitterWindow(self)
        self.panel1 = Input_Panel(self.splitter)
        self.panel2 = Output_Panel(self.splitter)
        self.splitter.SplitVertically(self.panel1, self.panel2)

        self.windowSizer = wx.BoxSizer(wx.VERTICAL)
        self.windowSizer.Add(self.splitter, 1, wx.ALL | wx.EXPAND)   
        self.SetSizerAndFit(self.windowSizer)

def main():
    app = wx.App(False)
    frame = Main_Window(None, "App GUI")
    frame.Show()
    app.MainLoop()

if __name__ == "__main__" :
    main()

Tags: selfadd面板inputinitdeflabelclass
2条回答

修改

self.SetSizer(self.sizer)

self.SetSizerAndFit(self.sizer)

输入面板和输出面板类也能正确调整面板大小。

您可以使用小部件检查工具来帮助您了解发生了什么。我使用它的突出显示功能来确定每个面板的位置和大小。你可以在这里阅读更多关于它的信息:http://wiki.wxpython.org/Widget%20Inspection%20Tool

SplitterWindow不会平均拆分项。您需要调用拆分器的setminimumpanese使它们都可见。我修改了您的代码,向您展示了我所说的内容,并演示了如何添加检查工具:

import wx

class Input_Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # Input variables
        self.tittle1 = wx.StaticText(self, label="Inputs:")    
        self.lblname1 = wx.StaticText(self, label="Input 1:")
        self.format1 = ['Option 1','Option 2']
        self.combo1 = wx.ComboBox(self, size=(200, -1),value='', choices=self.format1, style=wx.CB_DROPDOWN)
        self.lblname2 = wx.StaticText(self, label="Input 2")
        self.format2 = ['Option 1','Option 2', 'Option 3']
        self.combo2 = wx.ComboBox(self, size=(200, -1),value='', choices=self.format2, style=wx.CB_DROPDOWN)

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(2, 2)
        self.sizer.Add(self.tittle1, (1, 2))
        self.sizer.Add(self.lblname1, (2, 1))
        self.sizer.Add(self.combo1, (2, 2))
        self.sizer.Add(self.lblname2, (3, 1))
        self.sizer.Add(self.combo2, (3, 2))
        self.SetSizer(self.sizer)

class Output_Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # Output variables
        self.tittle2 = wx.StaticText(self, label="Outputs:")    
        self.lblname3 = wx.StaticText(self, label="Output1")
        self.result3 = wx.StaticText(self, label="", size=(100, -1))

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(2, 2)
        self.sizer.Add(self.tittle2, (1, 2))
        self.sizer.Add(self.lblname3, (2, 1))
        self.sizer.Add(self.result3, (2, 2))
        self.SetSizer(self.sizer)

class Main_Window(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title, size=(800,600))

        # Set variable panels
        self.splitter = wx.SplitterWindow(self, style = wx.SP_LIVE_UPDATE)
        self.panel1 = Input_Panel(self.splitter)
        self.panel2 = Output_Panel(self.splitter)
        self.splitter.SplitVertically(self.panel1, self.panel2)
        w, h = self.GetSize()
        self.splitter.SetMinimumPaneSize(w/2)

        self.windowSizer = wx.BoxSizer(wx.VERTICAL)
        self.windowSizer.Add(self.splitter, 1, wx.ALL | wx.EXPAND)   

        #self.SetSizerAndFit(self.windowSizer)

def main():
    import wx.lib.inspection
    app = wx.App(False)
    frame = Main_Window(None, "App GUI")
    frame.Show()
    wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()

if __name__ == "__main__" :
    main()

相关问题 更多 >