Python: Enable Radiobox based upon Checkbox Selection - python

I have a simple python GUI developed using WXPython. I have a checkbox1, checkbox2 and radiobox1. Based upon the Checkbox2 selection, I want the radiobox1 to be enabled or disabled. For example if Checkbox2 is unchecked, I want the radiobox1 to be disabled. I tried using getvalue() parameter but with no success. Here is the sample code.
Any help is much appreciated.
import wx
def create(parent):
return Frame1(parent)
[wxID_FRAME1, wxID_FRAME1CHECKBOX1, wxID_FRAME1CHECKBOX2,
wxID_FRAME1NOTEBOOK1, wxID_FRAME1PANEL1, wxID_FRAME1RADIOBOX1,
wxID_FRAME1STATICBOX1, wxID_FRAME1STATICTEXT1,
] = [wx.NewId() for _init_ctrls in range(8)]
class Frame1(wx.Frame):
def _init_coll_notebook1_Pages(self, parent):
# generated method, don't edit
parent.AddPage(imageId=-1, page=self.panel1, select=True, text='Pages0')
parent.AddPage(imageId=-1, page=self.staticText1, select=False,
text='Pages1')
def _init_ctrls(self, prnt):
# generated method, don't edit
wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
pos=wx.Point(470, 184), size=wx.Size(677, 492),
style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
self.SetClientSize(wx.Size(661, 454))
self.notebook1 = wx.Notebook(id=wxID_FRAME1NOTEBOOK1, name='notebook1',
parent=self, pos=wx.Point(0, 0), size=wx.Size(661, 454), style=0)
self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1',
parent=self.notebook1, pos=wx.Point(0, 0), size=wx.Size(653, 428),
style=wx.TAB_TRAVERSAL)
self.staticBox1 = wx.StaticBox(id=wxID_FRAME1STATICBOX1,
label='staticBox1', name='staticBox1', parent=self.panel1,
pos=wx.Point(16, 16), size=wx.Size(232, 128), style=0)
self.checkBox1 = wx.CheckBox(id=wxID_FRAME1CHECKBOX1, label='checkBox1',
name='checkBox1', parent=self.panel1, pos=wx.Point(24, 40),
size=wx.Size(70, 13), style=0)
self.checkBox2 = wx.CheckBox(id=wxID_FRAME1CHECKBOX2, label='checkBox2',
name='checkBox2', parent=self.panel1, pos=wx.Point(24, 64),
size=wx.Size(70, 13), style=0)
self.checkBox2.Set3StateValue(1)
self.checkBox2.Bind(wx.EVT_CHECKBOX, self.OnCheckBox2Checkbox,
id=wxID_FRAME1CHECKBOX2)
self.radioBox1 = wx.RadioBox(choices=['Phase A', 'PhaseB', 'Phase C'],
id=wxID_FRAME1RADIOBOX1, label='radioBox1', majorDimension=3,
name='radioBox1', parent=self.panel1, pos=wx.Point(32, 88),
size=wx.Size(200, 44), style=wx.RA_SPECIFY_COLS)
self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
label='staticText1', name='staticText1', parent=self.notebook1,
pos=wx.Point(0, 0), size=wx.Size(653, 428), style=0)
self._init_coll_notebook1_Pages(self.notebook1)
def __init__(self, parent):
self._init_ctrls(parent)
def OnCheckBox2Checkbox(self, event):
#event.Skip()
if self.checkBox2.GetValue() == 'False':
self.radioBox1.Enable(False)
print self.checkBox2.IsChecked()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = create(None)
frame.Show()
app.MainLoop()

I think that your problem is that you are only changing things in one direction - change:
def OnCheckBox2Checkbox(self, event):
#event.Skip()
if self.checkBox2.GetValue() == 'False':
self.radioBox1.Enable(False)
print self.checkBox2.IsChecked()
to:
def OnCheckBox2Checkbox(self, event):
self.radioBox1.Enable(self.checkBox2.IsChecked())
print self.checkBox2.IsChecked()

Related

WxPython using Listbox and other UserInput with a Button

I am trying to create a web crawler based on specific user input. For example, the User Input I am trying to receive is from a ListBox and a text field. Once I have that information, I would like the user to click a button to start the search with the information collected.
This is where I have been getting problems. The EVT function doesn't recognize the listbox since its been linked to the Button evt. Is there a way to solve the problem? Can EVT information be shared with other EVTs?
Here is what I have so far:
import wx # for gui
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Title', size=(300,200))
tournLevel = ['$10,000', '$15,000', '$20,000', '$50,000','$75,000','$100,000']
levelBox = wx.ListBox(panel, -1, (40, 50), (90, 90), tournLevel)
levelBox.SetSelection(1) # default selection
checkButton = wx.Button(panel, label= "Check Now", pos = (150, 50), size = (90, 40))
self.Bind(wx.EVT_BUTTON, self.OnClick, checkButton)
def OnClick(self, event):
currLevel = event.GetSelection()
print(currLevel) # to test if GetSelection is working
if __name__ == '__main__':
app = wx.App()
frame = MyFrame(parent=None, id=-1)
frame.Show()
app.MainLoop()
I would be very happy if I could just get the button to recognize the ListBox results.
Thank you for your time!
You can just grab it from the listbox, you don't need it from the event. See below:
import wx # for gui
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Title', size=(300,200))
tournLevel = ['$10,000', '$15,000', '$20,000', '$50,000','$75,000','$100,000']
self.levelBox = wx.ListBox(panel, -1, (40, 50), (90, 90), tournLevel)
self.levelBox.SetSelection(1) # default selection
self.checkButton = wx.Button(panel, label= "Check Now", pos = (150, 50), size = (90, 40))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.checkButton)
def OnClick(self, event):
currLevel = self.levelBox.GetSelection()
print(currLevel) # to test if GetSelection is working
if __name__ == '__main__':
app = wx.App()
frame = MyFrame(parent=None, id=-1)
frame.Show()
app.MainLoop()
More specifically, if you store levelBox as self.levelBox, it will be accessible inside the OnClick method as a MyFrame attribute. You can then use the GetSelection method for this object (not the event), which will get the current selection.
You can make levelBox into a property of the class by turning it into self.levelBox and accessing it that way as #brettb mentioned. However you can get a bit sneakier and do it using a lambda for your callback to pass the Listbox widget to the event handler:
import wx # for gui
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Title', size=(300,200))
panel = wx.Panel(self)
tournLevel = ['$10,000', '$15,000', '$20,000', '$50,000','$75,000','$100,000']
levelBox = wx.ListBox(panel, -1, (40, 50), (90, 90), tournLevel)
levelBox.SetSelection(1) # default selection
checkButton = wx.Button(panel, label= "Check Now", pos = (150, 50), size = (90, 40))
evt = lambda caller, widget=levelBox: self.OnClick(caller, widget)
checkButton.Bind(wx.EVT_BUTTON, evt)
def OnClick(self, event, widget):
currLevel = widget.GetSelection()
print(currLevel) # to test if GetSelection is working
print widget.GetString(currLevel)
if __name__ == '__main__':
app = wx.App()
frame = MyFrame(parent=None, id=-1)
frame.Show()
app.MainLoop()
Also note that you didn't have panel defined, so your original code doesn't work. See the following for more information:
http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks

Unable to get wxPython Sizers to work correctly

I am a newbie to Python although I have many years' experience in other languages. The main issues I am having is with Sizers. Using only BoxSizers I have managed to get things working as expected. However when I incorporated a GridBagSizer I found that I could only stretch the Window horizontally and not vertically. I have searched through previous queries but not found anything that exactly matches my problem.
I have been bogged down for over a day so thanks in advance for any responses...
The code for the App and Frame is:-
#!/usr/bin/env python
#Boa:App:BoaApp`enter code here`
import wx
import Frame9
modules ={'Frame9': [1, 'Main frame of Application', 'none://Frame9.py']}
class BoaApp(wx.App):
def OnInit(self):
self.main = Frame9.create(None)
self.main.Show()
self.SetTopWindow(self.main)
return True
def main():
application = BoaApp(0)
application.MainLoop()
if __name__ == '__main__':
main()
#Boa:Frame:Frame9
import wx
def create(parent):
return Frame9(parent)
[wxID_FRAME9, wxID_FRAME9BUTTON1, wxID_FRAME9BUTTON2, wxID_FRAME9BUTTON3,
wxID_FRAME9PANEL1, wxID_FRAME9STATICTEXT1, wxID_FRAME9STATICTEXT2,
wxID_FRAME9STATICTEXT3, wxID_FRAME9STATICTEXT4, wxID_FRAME9STATICTEXT5,
wxID_FRAME9STATUSBAR2, wxID_FRAME9TEXTCTRL1, wxID_FRAME9TEXTCTRL2,
wxID_FRAME9TEXTCTRL3, wxID_FRAME9TEXTCTRL4,
] = [wx.NewId() for _init_ctrls in range(15)]
class Frame9(wx.Frame):
def _init_coll_boxSizer1_Items(self, parent):
# generated method, don't edit
parent.AddWindow(self.panel1, 0, border=0, flag=wx.ALIGN_BOTTOM)
parent.AddWindow(self.statusBar2, 0, border=0, flag=0)
parent.AddWindow(self.staticText5, 0, border=0, flag=0)
def _init_sizers(self):
# generated method, don't edit
self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)
self.boxSizer2 = wx.BoxSizer(orient=wx.HORIZONTAL)
self.gridBagSizer1 = wx.GridBagSizer(hgap=50, vgap=75)
self.boxSizer3 = wx.BoxSizer(orient=wx.HORIZONTAL)
self._init_coll_boxSizer1_Items(self.boxSizer1)
self.SetSizer(self.boxSizer2)
def _init_ctrls(self, prnt):
# generated method, don't edit
wx.Frame.__init__(self, id=wxID_FRAME9, name='', parent=prnt,
pos=wx.Point(432, 157), size=wx.Size(721, 315),
style=wx.DEFAULT_FRAME_STYLE, title='Frame9')
self.SetClientSize(wx.Size(721, 315))
self.SetMinSize(wx.Size(721, 315))
self.SetAutoLayout(False)
self.Bind(wx.EVT_ACTIVATE, self.OnFrame9Activate)
self.button1 = wx.Button(id=wxID_FRAME9BUTTON1, label='Clear',
name='button1', parent=self, pos=wx.Point(25, 232),
size=wx.Size(85, 28), style=0)
self.button2 = wx.Button(id=wxID_FRAME9BUTTON2, label='Convert',
name='button2', parent=self, pos=wx.Point(313, 232),
size=wx.Size(85, 28), style=0)
self.button3 = wx.Button(id=wxID_FRAME9BUTTON3, label='Now',
name='button3', parent=self, pos=wx.Point(611, 232),
size=wx.Size(85, 28), style=0)
self.staticText1 = wx.StaticText(id=wxID_FRAME9STATICTEXT1,
label=u'2^10 * Hex', name='staticText1', parent=self,
pos=wx.Point(0, 0), size=wx.Size(90, 15), style=wx.ALIGN_RIGHT)
self.staticText2 = wx.StaticText(id=wxID_FRAME9STATICTEXT2,
label=u'Hex Time', name='staticText2', parent=self,
pos=wx.Point(0, 0), size=wx.Size(90, 15), style=wx.ALIGN_RIGHT)
self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME9TEXTCTRL1, name='textCtrl1',
parent=self, pos=wx.Point(200, 72), size=wx.Size(145, 25),
style=0, value='textCtrl1')
self.textCtrl2 = wx.TextCtrl(id=wxID_FRAME9TEXTCTRL2, name='textCtrl2',
parent=self, pos=wx.Point(520, 72), size=wx.Size(145, 25),
style=0, value='textCtrl2')
self.staticText3 = wx.StaticText(id=wxID_FRAME9STATICTEXT3,
label=u'Epoch Time', name='staticText3', parent=self,
pos=wx.Point(48, 172), size=wx.Size(90, 15), style=0)
self.staticText4 = wx.StaticText(id=wxID_FRAME9STATICTEXT4,
label=u'Date And Time', name='staticText4', parent=self,
pos=wx.Point(360, 172), size=wx.Size(90, 15), style=0)
self.textCtrl3 = wx.TextCtrl(id=wxID_FRAME9TEXTCTRL3, name='textCtrl3',
parent=self, pos=wx.Point(200, 172), size=wx.Size(145, 25),
style=0, value=u'textCtrl3')
self.textCtrl4 = wx.TextCtrl(id=wxID_FRAME9TEXTCTRL4, name='textCtrl4',
parent=self, pos=wx.Point(520, 172), size=wx.Size(145, 25),
style=0, value=u'textCtrl4')
self.panel1 = wx.Panel(id=wxID_FRAME9PANEL1, name='panel1', parent=self,
pos=wx.Point(0, 0), size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)
self.panel1.Show(False)
self.statusBar2 = wx.StatusBar(id=wxID_FRAME9STATUSBAR2,
name='statusBar2', parent=self, style=0)
self.statusBar2.SetStatusText(u'Current status...')
self.statusBar2.SetToolTipString(u'statusBar2')
self.staticText5 = wx.StaticText(id=wxID_FRAME9STATICTEXT5, label=u'',
name='staticText5', parent=self, pos=wx.Point(0, 20),
size=wx.Size(1, 15), style=0)
self._init_sizers()
def __init__(self, parent):
self._init_ctrls(parent)
def OnSize(self, event):
event.Skip()
cl_size = self.GetClientSize()
if cl_size.height < self.min_ClientHeight:
self.SetClientSize((cl_size.width, self.min_ClientHeight))
def OnFrame9Activate(self, event):
#self.panel = wx.Panel(self)
self._init_sizers()
self.gridBagSizer1.Add(self.staticText1, pos=(1,1), span = (1,1), flag=wx.EXPAND)
self.gridBagSizer1.Add(self.textCtrl1, pos=(1,2), span = (1,2), flag=wx.EXPAND)
self.gridBagSizer1.Add(self.staticText2, pos=(1,5), span = (1,1), flag=wx.EXPAND)
self.gridBagSizer1.Add(self.textCtrl2, pos=(1,6), span = (1,2), flag=wx.EXPAND)
#Add second row
self.gridBagSizer1.Add(self.staticText3, pos=(2,1), span = (1,1), flag=wx.EXPAND)
self.gridBagSizer1.Add(self.textCtrl3, pos=(2,2), span = (1,2), flag=wx.EXPAND)
self.gridBagSizer1.Add(self.staticText4, pos=(2,5), span = (1,1), flag=wx.EXPAND)
self.gridBagSizer1.Add(self.textCtrl4, pos=(2,6), span = (1,2), flag=wx.EXPAND)
self.gridBagSizer1.Add(self.staticText5, pos=(2,8), span = (1,1), flag=wx.EXPAND)
self.gridBagSizer1.AddGrowableCol(0)
self.gridBagSizer1.AddGrowableCol(1)
self.gridBagSizer1.AddGrowableCol(2)
self.gridBagSizer1.AddGrowableCol(4)
self.gridBagSizer1.AddGrowableCol(5)
self.gridBagSizer1.AddGrowableCol(6)
self.gridBagSizer1.AddGrowableCol(7)
self.gridBagSizer1.AddGrowableCol(8)
self.gridBagSizer1.AddGrowableRow(0)
self.gridBagSizer1.AddGrowableRow(1)
self.gridBagSizer1.AddGrowableRow(2)
self.gridBagSizer1.AddGrowableRow(3)
self.boxSizer1.Add(self.gridBagSizer1, 0, wx.ALL|wx.EXPAND)
self.boxSizer3.Add((20, 20), 1)
self.boxSizer3.Add(self.button1)
self.boxSizer3.Add((20, 20), 1)
self.boxSizer3.Add(self.button2)
self.boxSizer3.Add((20, 20), 1)
self.boxSizer3.Add(self.button3)
self.boxSizer3.Add((20, 20), 1)
self.boxSizer1.Add(self.boxSizer3, 0, wx.ALL|wx.EXPAND|wx.BOTTOM, 50)
self.SetSizer(self.boxSizer1) # new code
self.boxSizer1.Fit(self)
event.Skip()
Change this:
self.boxSizer1.Add(self.gridBagSizer1, 0, wx.ALL|wx.EXPAND)
to:
self.boxSizer1.Add(self.gridBagSizer1, 1, wx.ALL|wx.EXPAND)
Following is an alternative way of doing your layout with wxPython using sized_controls. I really like them as I now adays find it easier to layout screens with them instead of using a UI designer like Boa (which I liked a lot and have used in the past).
A few notes to the code, I used wxversion as I have a few wxPython versions installed on my machine, so needed to select one.
To find out more about:
sized controls - http://wiki.wxpython.org/SizedControls
WIT - http://wiki.wxpython.org/Widget%20Inspection%20Tool
#!/usr/bin/env python
# -*- coding: utf-8 -*-#
import wxversion
wxversion.select('2.9.5')
import wx
import wx.lib.sized_controls as SC
class AltFrame(SC.SizedFrame):
def __init__(self, *args, **kwds):
super(AltFrame, self).__init__(*args, **kwds)
paneCont = self.GetContentsPane()
paneControls = SC.SizedPanel(paneCont)
paneControls.SetSizerType('grid', options={'cols': 2,})
paneControls.SetSizerProps(expand=True, proportion=1)
paneButtons = SC.SizedPanel(paneCont)
paneButtons.SetSizerType('grid', options={'cols': 3,})
paneButtons.SetSizerProps(align='center')
self.button1 = wx.Button(paneButtons, label='Clear',
name='button1')
self.button2 = wx.Button(paneButtons, label='Convert',
name='button2')
self.button3 = wx.Button(paneButtons, label='Now',
name='button3')
self.staticText1 = wx.StaticText(paneControls, label=u'2^10 * Hex',
name='staticText1',
style=wx.ALIGN_RIGHT)
self.textCtrl1 = wx.TextCtrl(paneControls, name='textCtrl1',
value='textCtrl1')
self.staticText2 = wx.StaticText(paneControls, label=u'Hex Time',
name='staticText2',
style=wx.ALIGN_RIGHT)
self.textCtrl2 = wx.TextCtrl(paneControls, name='textCtrl2',
value='textCtrl2')
self.staticText3 = wx.StaticText(paneControls,
label=u'Epoch Time', name='staticText3')
self.textCtrl3 = wx.TextCtrl(paneControls, name='textCtrl3')
self.staticText4 = wx.StaticText(paneControls,
label=u'Date And Time', name='staticText4')
self.textCtrl4 = wx.TextCtrl(paneControls, name='textCtrl4')
self.statusBar2 = wx.StatusBar(self, name='statusBar2')
self.statusBar2.SetStatusText(u'Current status...')
self.statusBar2.SetToolTipString(u'statusBar2')
self.SetStatusBar(self.statusBar2)
if __name__ == "__main__":
import wx.lib.mixins.inspection as WIT
app = WIT.InspectableApp()
frame = AltFrame(None)
frame.Show()
app.MainLoop()

What is the difference between wx.lib.newevent.NewEvent() and wx.NewEventType()?

What is the difference between using wx.lib.newevent.NewEvent() and using wx.NewEventType() with wx.PyCommandEvent()? The code sample below uses the two different ways to create events to accomplish the same thing (passing data attached to an event). When should one be used instead of the other?
import wx
import wx.lib.newevent
MyEvent, EVT_MY_EVENT = wx.lib.newevent.NewEvent()
EVT_ANOTHER_EVENT_TYPE = wx.NewEventType()
EVT_ANOTHER_EVENT = wx.PyEventBinder(EVT_ANOTHER_EVENT_TYPE, 1)
class EventTest(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title, size=(210, 200))
wx.Button(self, 1, 'NewEvent', (50, 10), (110, -1))
wx.Button(self, 2, 'PyCommandEvent', (50, 60), (110, -1))
wx.Button(self, 3, 'Close', (50, 110), (110, -1))
self.Bind(wx.EVT_BUTTON, self.OnNewEvent, id=1)
self.Bind(wx.EVT_BUTTON, self.OnPyCommandEvent, id=2)
self.Bind(wx.EVT_BUTTON, self.OnClose, id=3)
self.Bind(EVT_MY_EVENT, self.NewEventHandler)
self.Bind(EVT_ANOTHER_EVENT, self.PyCommandEventHandler)
self.Centre()
self.ShowModal()
self.Destroy()
def OnNewEvent(self, event):
evt = MyEvent(SomeData=("NewEvent", 11, "aaa"))
wx.PostEvent(self, evt)
def OnPyCommandEvent(self, event):
evt = wx.PyCommandEvent(EVT_ANOTHER_EVENT_TYPE, wx.ID_ANY)
evt.SetClientData(("PyCommandEvent", 22, "bbb"))
wx.PostEvent(self, evt)
def NewEventHandler(self, evt=None):
print "NewEvent Data:", evt.SomeData
def PyCommandEventHandler(self, evt=None):
print "PyCommandEvent Data:", evt.GetClientData()
def OnClose(self, event):
self.Close(True)
if __name__ == "__main__":
app = wx.App(0)
EventTest(None, -1, 'Event Test')
app.MainLoop()
A wx.lib.newevent.NewEvent() is just an easier wxpython way thats been added to make a wx.NewEventType().
if you have a look at the code in the module newevent you will see what it does.
"""Easy generation of new events classes and binder objects"""
__author__ = "Miki Tebeka <miki.tebeka#gmail.com>"
import wx
#---------------------------------------------------------------------------
def NewEvent():
"""Generate new (Event, Binder) tuple
e.g. MooEvent, EVT_MOO = NewEvent()
"""
evttype = wx.NewEventType()
class _Event(wx.PyEvent):
def __init__(self, **kw):
wx.PyEvent.__init__(self)
self.SetEventType(evttype)
self.__dict__.update(kw)
return _Event, wx.PyEventBinder(evttype)
def NewCommandEvent():
"""Generate new (CmdEvent, Binder) tuple
e.g. MooCmdEvent, EVT_MOO = NewCommandEvent()
"""
evttype = wx.NewEventType()
class _Event(wx.PyCommandEvent):
def __init__(self, id, **kw):
wx.PyCommandEvent.__init__(self, evttype, id)
self.__dict__.update(kw)
return _Event, wx.PyEventBinder(evttype, 1)

How to accept value from textctrl in wxpython

Here is a python pgm. It contains a text ctrl and a button. Please help me to modify so that on pressing the button I need the string user entered in textctrl to be stored to a variable.
#! /usr/bin/env python
#Boa:Frame:Frame1
import wx
def create(parent):
return Frame1(parent)
[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1PANEL1, wxID_FRAME1TEXT1,
] = [wx.NewId() for _init_ctrls in range(4)]
class Frame1(wx.Frame):
def _init_ctrls(self, prnt):
# generated method, don't edit
wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
pos=wx.Point(249, 224), size=wx.Size(683, 445),
style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
self.SetClientSize(wx.Size(683, 445))
self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
pos=wx.Point(0, 0), size=wx.Size(683, 445),
style=wx.TAB_TRAVERSAL)
self.text1 = wx.TextCtrl(id=wxID_FRAME1TEXT1, name=u'text1',
parent=self.panel1, pos=wx.Point(268, 139), size=wx.Size(103, 25),
style=0, value=u'enter')
self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label=u'click',
name='button1', parent=self.panel1, pos=wx.Point(279, 272),
size=wx.Size(85, 27), style=0)
self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
id=wxID_FRAME1BUTTON1)
def __init__(self, parent):
self._init_ctrls(parent)
def OnButton1Button(self, event):
event.Skip()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = create(None)
frame.Show()
app.MainLoop()
There's a method attached to a wxTextCtrl object called getValue, so in OnButton1Button(), you call
var = self.text1.GetValue()
And then do what you want with var afterwards.

How come ScrolledPanel in wxpython not working this way?

I don't know why the following code not working, please help me out:
import wx
import wx.lib.scrolledpanel as scrolled
class TaskFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, id = -1, title="ScrolledPanel", size = (500, 600))
MainPanel = wx.Panel(self)
NewPanel = scrolled.ScrolledPanel(parent = MainPanel, pos = (100, 100), size = (300, 200), id = -1, style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER, name="panel" )
self.Button = wx.Button(parent = NewPanel, id = -1, label="Log", pos=(500, 30), size=(50, 20))
NewPanel.SetupScrolling()
class TaskApp(wx.App):
def OnInit(self):
self.frame = TaskFrame()
self.frame.Show()
self.SetTopWindow(self.frame)
return True
def main():
App = TaskApp(redirect = False)
App.MainLoop()
if __name__ == "__main__":
main()
The Log button should be in the NewPanel, and the NewPanel should be able to scroll, but it's not, what's the problem?
Try using a sizer. You have to place an object larger than the ScrolledPanel inside it to activate the scrolling (as far as I know), so this should do what I think you're trying to do:
class TaskFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, id = -1, title="ScrolledPanel", size = (500, 600))
MainPanel = wx.Panel(self)
NewPanel = scrolled.ScrolledPanel(parent = MainPanel, pos = (100, 100), size = (300, 200), id = -1, style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER, name="panel" )
PanelSizer = wx.BoxSizer()
InsidePanel = wx.Panel(NewPanel)
self.Button = wx.Button(parent=InsidePanel, id = -1, label="Log", pos=(500, 30), size=(50, 20))
PanelSizer.Add(InsidePanel, proportion=1)
NewPanel.SetSizer(PanelSizer)
NewPanel.SetupScrolling()

Categories