Question

I am writing a script with wx.python to select a value from a wx.ComboBox, and then, depending on the value chosen from ComboBox, some widgets (textcontrols and static texts) must appear. I have a problem in managing layout: I want to put the widgets in a FlexGrid Box but I have some problems since I am new to wxpython. Here is my code

import wx,os,subprocess
class ExampleFrame(wx.Frame): 
def __init__(self, parent):
    wx.Frame.__init__(self, parent)

    self.panel = wx.Panel(self)
    self.quote = wx.StaticText(self.panel, label="Test:")  
    self.result = wx.StaticText(self.panel, label="")
    self.result.SetForegroundColour(wx.RED)
    self.quote1 = wx.StaticText(self.panel, label="select a choice")
    distros = ['Choice1', 'Choice2']
    self.cb = wx.ComboBox(self.panel, choices=distros, style=wx.CB_READONLY) 
    self.button = wx.Button(self.panel, label="Begin")

    # Set sizer for the frame, so we can change frame size to match widgets
    self.windowSizer = wx.BoxSizer()
    self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)        

    # Set sizer for the panel content
    self.sizer = wx.GridBagSizer(7, 7)
    self.sizer.Add(self.quote, (0, 0))
    self.sizer.Add(self.result, (0, 1))
    self.sizer.Add(self.quote1,(1,0))
    self.sizer.Add(self.cb, (1, 1))
    self.sizer.Add(self.button, (6, 0), (5, 2))

    # Set simple sizer for a nice border
    self.border = wx.BoxSizer()
    self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)

    # Use the sizers
    self.panel.SetSizerAndFit(self.border)  
    self.SetSizerAndFit(self.windowSizer)  
    self.SetSize((250, 230))
    self.SetTitle('Example')
    self.Centre()

    # Set event handlers
    self.button.Bind(wx.EVT_BUTTON, self.OnButton)
    self.cb.Bind(wx.EVT_COMBOBOX, self.OnSelect)
def OnSelect(self,e):
    distros = ['Choice1', 'Choice2']
    i = e.GetString()
    if i == distros[0]:
        self.result.SetLabel("1")
        self.res="1"
        panel1= wx.Panel(self)
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        fgs = wx.FlexGridSizer(3, 2, 9, 25)
        lblname = wx.StaticText(self.panel, label="label1")
        tc1 = wx.TextCtrl(self.panel)
        fgs.AddMany([(lblname), (tc1, 1, wx.EXPAND)])
        fgs.AddGrowableRow(2, 1)
        fgs.AddGrowableCol(1, 1)
        hbox.Add(fgs, proportion=1, flag=wx.ALL|wx.EXPAND, border=15)
        panel1.SetSizer(hbox)

    elif i==distros[1]:
        self.result.SetLabel("2")
        self.res="2"
def OnButton(self, e):
    if self.res=="1":
        wx.MessageBox('choice1','info',wx.OK)
    elif self.res=="2":
        wx.MessageBox('choice2','info',wx.OK)
app = wx.App(False)
frame = ExampleFrame(None)
frame.Show()
app.MainLoop()

My problem is between lines 47 and 56. Can you suggest me how to manage the layout. Thanks in advance

Était-ce utile?

La solution

You have lots of parent/child relationship errors. A new panel panel1 is created but the ctrls are not given this panel as the parent they are given the panel that is a child of the frame.

panel1 is parented to the frame is that how you wanted it to be or should it be a child of the frames panel.

panel1 has not been added to the appropriate sizer

The method SetSizerAndFit im not sure you need to be using that on the frames panel just using SetSizer shold be sufficient.

When adding things sizers after the initial setup, you quite often need to call the method Layout to get the sizers to adjust to the new controls.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top