Linking onClick event to other controls on the Frame

D

DataSmash

Hello,
I've created a simple form with 2 radio boxes, 2 text boxes and a
button.
When I click the button, I'd like to write each "choice" to a text
file.
I can't figure out how to "link" the onClick event to the other 4
controls.
Any help would be much appreciated!
R.D. Harles

import wx, sys

class Form1(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)

# Create button
self.button =wx.Button(self, 50, "GO!", wx.Point(20, 200))
wx.EVT_BUTTON(self, 50, self.OnClick)

## First name
# Edit control
self.lblname = wx.StaticText(self, -1, "First Name
:",wx.Point(20,50))
self.editname = wx.TextCtrl(self, 10, "", wx.Point(90, 50),
wx.Size(100,-1))
wx.EVT_TEXT(self, 10, self.EvtText)

## Last Name
# Edit control
self.lblname = wx.StaticText(self, -1, "Last Name
:",wx.Point(20,75))
self.editname = wx.TextCtrl(self, 20, "", wx.Point(90, 75),
wx.Size(100,-1))
wx.EVT_TEXT(self, 20, self.EvtText)

# Radio Boxes
self.radioList = ['Employed', 'Unemployed']
rb = wx.RadioBox(self, 30, "Status:", wx.Point(20, 100),
wx.DefaultSize,
self.radioList, 2, wx.RA_SPECIFY_COLS)
wx.EVT_RADIOBOX(self, 30, self.EvtRadioBox)

# Radio Boxes
self.radioList = ['Married', 'Single']
rb = wx.RadioBox(self, 40, "Status:", wx.Point(20, 150),
wx.DefaultSize,
self.radioList, 2, wx.RA_SPECIFY_COLS)
wx.EVT_RADIOBOX(self, 40, self.EvtRadioBox)

# Text event
def EvtText(self, event):
print event.GetString()

# RadioBox event
def EvtRadioBox(self, event):
print event.GetId()

# Button event
def OnClick(self,event):

print "Writing job.cfg..."
file = open("job.cfg", "w")
file.write("")


app = wx.PySimpleApp()
frame = wx.Frame(None, -1, " Questions")
Form1(frame,-1)
frame.Show(1)
app.MainLoop()
 
M

Miki

Hello Harles,

Please define "link" - is it bind event, get information from control,
....?
If I'm guessing the you want to the the value of each control then you
need to store a reference to this control and call the method that gets
the value of each control.
(GetValue() for most, GetStringSelection() for others ...)

Also I suggest you use sizers instead of hard coding the widgets
location.

My best suggestion however is that you download the wxPython demo and
view the examples over there.

See revised code below:

import wx
class Form1(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
sizer = wx.GridSizer(wx.VERTICAL)
gsizer = wx.FlexGridSizer(2, 2)

# First Name: _______
gsizer.Add(wx.StaticText(self, -1, "First Name:"))
self._first_name = wx.TextCtrl(self, -1, size=(100, -1))
gsizer.Add(self._first_name, 0, wx.EXPAND)

# Last Name: _______
gsizer.Add(wx.StaticText(self, -1, "Last Name:"))
self._last_name = wx.TextCtrl(self, -1, size=(100, -1))
gsizer.Add(self._last_name, 0, wx.EXPAND)
gsizer.AddGrowableCol(1)

sizer.Add(gsizer, 1, wx.EXPAND)

self._work_status = wx.RadioBox(self, -1, "Work Status",
choices=["Employed", "Unemployed"])
sizer.Add(self._work_status, 0, wx.EXPAND)

self._martial_status = wx.RadioBox(self, -1, "Martial Status",
choices=["Married", "Single"])
sizer.Add(self._martial_status, 0, wx.EXPAND)

b = wx.Button(self, -1, "GO!")
self.Bind(wx.EVT_BUTTON, self.OnClick, b)
sizer.Add(b)

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

# Button event
def OnClick(self,event):
fo = open("job.cfg", "w")
print >> fo, "First Name:", self._first_name.GetValue()
print >> fo, "Last Name:", self._last_name.GetValue()
print >> fo, "Work Status:",
self._work_status.GetStringSelection()
print >> fo, "Martial Status:",
self._martial_status.GetStringSelection()
fo.close()

HTH,
Miki
http://pythonwise.blogspot.com/
 
D

DataSmash

Hi Miki,
I appreciate you taking the time to figure out what I was trying to do.
This is exactly what I was looking for.
I plan on studing this code and looking at the wxpython demo as you
mentioned.
Thanks again!
R.D. Harles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top