473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

wxPython: Default Frame button?

Hello All,

I have a frame that contains a panel and several buttons.
I'd like to make one of the button the default button but
self.SetDefaultItem(btn) or btn.SetFocus() don't work. The item in
focus is a text control inside the panel.

Any Ideas? (see short example below)

Thanks.
Miki

--- btn.py ---
import wx

class P(wx.Panel):
def __init__(self, parent, id=-1):
wx.Panel.__init__(self, parent, id)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(wx.TextCtrl(self, -1, size=(250, -1), value="XXX"))

self.SetAutoLayout(True)
self.SetSizer(sizer)
sizer.Fit(self)

class F(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Test Frame")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(P(self), 0, wx.EXPAND)
b = wx.Button(self, wx.NewId(), "Quit")
wx.EVT_BUTTON(self, b.GetId(), self.on_quit)
sizer.Add(b, 0, wx.EXPAND)

self.SetDefaultItem(b)
b.SetFocus()

self.SetAutoLayout(True)
self.SetSizer(sizer)
sizer.Fit(self)
def on_quit(self, e):
self.Close(True)
def main():
app = wx.PySimpleApp()
f = F()
f.Show(True)
app.MainLoop()

if __name__ == "__main__":
main()

--- btn.py ---
Jul 18 '05 #1
1 5272
On Mon, 2003-08-04 at 06:22, Miki Tebeka wrote:
Hello Cliff,
In general, the only child of a frame should be a panel or some other
container (like a splitter). Frames should generally only have one
child. Make the button a child of the panel rather than a sibling.

10x. Works like a charm.
class F(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Test Frame")
panel = P(self)
self.Fit()


Is there a default frame that does the above?


No. However, you can combine the frame and panel code into a single
class if you like:

import wx

class F(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Test Frame")
panel = wx.Panel(self, -1)
sizer = wx.BoxSizer(wx.VERTICAL)

b = wx.Button(panel, -1, "Quit")
wx.EVT_BUTTON(panel, b.GetId(), self.on_quit)

sizer.AddMany([
(wx.TextCtrl(panel, -1, size = (250, -1), value = "XXX")),
(b, 0, wx.EXPAND),
])

panel.SetDefaultItem(b)
b.SetFocus()

panel.SetAutoLayout(True)
panel.SetSizer(sizer)
sizer.Fit(panel)

self.Fit()

def on_quit(self, e):
self.Close(True)
def main():
app = wx.PySimpleApp()
f = F()
f.Show(True)
app.MainLoop()
if __name__ == "__main__":
main()

Alternatively, you can dispense with creating a custom class for the
frame (which is perhaps a bit closer to what you are asking):
import wx

class P(wx.Panel):
def __init__(self, parent, id = -1):
wx.Panel.__init__(self, parent, id)
sizer = wx.BoxSizer(wx.VERTICAL)

b = wx.Button(self, -1, "Quit")
wx.EVT_BUTTON(self, b.GetId(), lambda evt: parent.Close(True))

sizer.AddMany([
(wx.TextCtrl(self, -1, size = (250, -1), value = "XXX")),
(b, 0, wx.EXPAND),
])

self.SetDefaultItem(b)
b.SetFocus()

self.SetAutoLayout(True)
self.SetSizer(sizer)
sizer.Fit(self)
def main():
app = wx.PySimpleApp()
f = wx.Frame(None, -1, "Test Frame")
P(f)
f.Fit()
f.Show(True)
app.MainLoop()
if __name__ == "__main__":
main()

Personally, I prefer to keep objects discrete, especially for panels
which might at some point get moved to some other container (say you
decide to put a notebook or splitter in the frame), but if you're not
concerned about that then these are both valid approaches.

Regards,

--
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 (800) 735-0555
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Grant Edwards | last post by:
I've decided to learn wxPython, and I'm afraid I just don't grok the whole "id" thing where you have to pull unique integers out of your, er, the air and then use those to refer to objects: ...
1
by: mdk.R | last post by:
Hello all: i'am installed wxPython 2.5 and Python2.3.4..i try execute script with wxPython but it show error: Traceback (most recent call last): File "E:\py\test.py", line 7, in ? import wx...
1
by: w.p. | last post by:
Hello! I want change default tab traversing in my app. But i don't know how to do it :( Belowe i include simple example - i want change default tab order: radiobutton "mode11" -> radiobutton...
3
by: John Salerno | last post by:
I'm using the sample code of the file 'simple.py' and trying to make a single window with a panel in it, but I keep getting an error. Here's my code: (I know I might need something else, like a...
9
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame....
2
by: crystalattice | last post by:
In my GUI app, I have a radio box allowing the user to pick his/her gender. When I click a button, I'd like the radio box to tell the program which choice is marked. I can get it to work when I...
3
by: citronelu | last post by:
Hi, I'm new to wxpython, and the following code is my first serious attempt: #~ start code import wx class MyPanel(wx.Panel):
9
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the...
16
by: Andrea Gavana | last post by:
Hi Diez & All, Do you mind explaining "why" you find it *buttugly*? I am asking just out of curiosity, obviously. I am so biased towards wxPython that I won't make any comment on this thread...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.