I have a small bit of code here I am working with. The goal I have set forth was to scale the size of panel to the frame. Does the frame have a size property that can be fetched? Are there functions or methods (not sure I know the difference yet) that will allow scaling to the size of the frame maybe a sizer?

import wx

class KeyEvent(wx.Frame):
def __init__(self, parent, id, title):
frame = wx.Frame.__init__(self, parent, id, title)
panel = wx.Panel(self, -1)
button = wx.Button(self, -1, label="click me")
panel.Bind(wx.EVT_LEFT_UP, self.PanelLeftUp)
self.Show(True)

def PanelLeftUp(self, event):
print "left click occured"

app = wx.App()
KeyEvent(None, -1, 'keyevent.py')
app.MainLoop()import wx

By setting the button to be child of panel I see the panel will scale to 100% of its container (the frame). This though is not reallly what I wanted since I am then unable to set the dimension on the panel. It stays 100% no matter if I change its size value when created. Also would like to be able to change value of pos after creation.

possible?

additionally if I set panel to be child of frame the program ceases to run?


-- Andrew

Well, here're a couple of suggestions:

import wx

class KeyEvent(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)  ## <<<
        self.panel = wx.Panel(self, -1,size=wx.Size(100,100)) ## <<<
        self.button = wx.Button(self, -1, label="click me")

        self.sizer = wx.BoxSizer(orient=wx.VERTICAL) ## basic sizer stuff
        self.sizer.Add(self.panel)
        self.sizer.Add(self.button)
        self.SetSizer(self.sizer)                    ## I always forget to do this
        self.Fit()

        self.panel.Bind(wx.EVT_LEFT_UP, self.PanelLeftUp)
        

    def PanelLeftUp(self, event):
        print "left click occured"

app = wx.App()
k = KeyEvent(None, -1, 'keyevent.py')
k.Show()   ## windows should not, in general, Show() themselves in __init__
app.MainLoop()

BTW, did you mean to Bind the key-up event to the panel instead of the button?

Jeff

Thanks Jeff I will work with what you have provided and try and wrap my head around it.

I did mean to allow clicking of the panel. Just wanted to see if it would work.

I added another button and for some reason they are now over lapping. Guess I have to explicitly position them? Well... maybe the sizer will cover that too.

import wx

class KeyEvent(wx.Frame):
    def __init__(self, parent, id, title):
        frame = wx.Frame.__init__(self, parent, id, title)
        panel = wx.Panel(self,-1)
        button1 = wx.Button(panel, -1, label="hide other button")
        self.button2 = wx.Button(panel, -1, label="im just here to be hidden")
        button1.Bind(wx.EVT_LEFT_UP, self.ButtonLeftUp)
        self.Centre()
        self.Show(True)
        
    def ButtonLeftUp(self, event):
        self.button2.Show(False)
        event.Skip()
app = wx.App()
KeyEvent(None, -1, 'keyevent.py')
app.MainLoop()

Thanks for your replies...

-- Andrew

Working with you code I am getting errors. I am using Eclipse IDE and when the script runs a console pops up and displays the error but hides itself so fast I cannot read it. Also not sure if I can step through program to see where error is occuring. Is Eclipse Ok or am I on the wrong path as far a editors go?

Thanks -- Andrew

Seems Eclipse will give indentation errors if the indent are not all spaces or tabs. If they are mixed problems occur.

I tried using boa constructor but console window will hide real quick just like Eclipse.

Just wanted to let you know Jeff that it was not a result of your mods that were causing problems.

Starting to get a better hold on things now. Thanks for your help.
<code>

import wx

class Layout(wx.Frame):
def __init__(self, parent, id, title):

wx.Frame.__init__(self, parent, id, title)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer_two = wx.BoxSizer(wx.HORIZONTAL)
self.panel_one = wx.Panel(self, -1)
self.panel_two = wx.Panel(self, -1)

self.panel_two.SetBackgroundColour(wx.RED)

self.button_one = wx.Button(self.panel_one, -1, label = "button 1")
self.button_two = wx.Button(self.panel_two, -1, label = "button 2")

sizer.Add(self.panel_one, 2, wx.EXPAND | wx.ALL, 3)
sizer.Add(self.panel_two, 1, wx.EXPAND | wx.ALL, 3)
sizer_two.Add(self.button_one, 1, wx.ALIGN_CENTER)

self.button_one.Bind(wx.EVT_BUTTON,self.Button_One_Click)

self.SetSize((400,120))
#self.Fit()
self.SetSizer(sizer)
self.panel_one.SetSizer(sizer_two)
self.Centre()

def Button_One_Click(self,event):

if self.button_two.IsShown():

self.panel_two.Show(False)

else:
self.panel_two.Show(True)

event.Skip()

app = wx.App()
k = Layout(None, -1, 'layout.py')
k.Show(True)

app.MainLoop()

</code>

Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.

[code=python]
your Python code here

[/code]

This will put your error message into the cmd window:

# took Jeff's code to create error

import wx

class KeyEvent(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        self.panel = wx.Panel(self, -1,size=wx.Size(100,100))
        self.button = wx.Button(self, -1, label="click me")

        self.sizer = wx.BoxSizer(orient=wx.VERTICAL)
        self.sizer.Add(self.panel)
        self.sizer.Add(self.button)
        self.SetSizer(self.sizer)
        self.Fit()
        # self.PanelLeftUp is intentionally misspelled for error
        self.panel.Bind(wx.EVT_LEFT_UP, self.PanelLeftU)
        

    def PanelLeftUp(self, event):
        print "left click occured"

app = wx.App(False)  # <---- note the addition of False to show error in cmd window
k = KeyEvent(None, -1, 'keyevent.py')
k.Show()
app.MainLoop()

Thanks Jeff wx.App(False) did the trick!

Yeah, on the code wrap...thanks for the tip. I missed and used HTML style tag and without code type attribute. Oops.

BTW is there some way of cut and pasting line numbered code from web browser into an editor?

Currently I have to manually remove all the line numbers being carefull to preserve indent. Hoping there is an easier (faster) way.


--Andrew

wx.App(False) was Bumsfeld's trick, not mine.

If you click the "Toggle Plain Text" link in the code header, it will turn into paste-able text.

Jeff

wx.App(False) was Bumsfeld's trick, not mine.

If you click the "Toggle Plain Text" link in the code header, it will turn into paste-able text.

Jeff

Ahh... Yes i see... Doh!

This article has been dead for over six months. Start a new discussion instead.