How to accept value from textctrl in wxpython - python

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.

Related

wxPython: ListBox not selectable/clickable when used with ComboCtrl

I am trying to couple wx.ListBox with wx.combo.Comboctrl. A sample code is below. For some reason, the items in the ListBox are not clickable/selectable. I wonder how I can make it work. Thanks!
EDIT: Missing code added
import wx, wx.combo
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, title="", size=(300, 100))
gbs = wx.GridBagSizer()
ComboCtrlBox = wx.combo.ComboCtrl(self)
ComboCtrlPopup = ListBoxComboPopup()
ComboCtrlBox.SetPopupControl(ComboCtrlPopup)
ComboCtrlPopup.ListBox.Append("Apple")
ComboCtrlPopup.ListBox.Append("Banana")
ComboCtrlPopup.ListBox.Append("Orange")
ComboCtrlPopup.ListBox.Bind(wx.EVT_LISTBOX, self.OnSelect) #ADDED
gbs.Add(ComboCtrlBox, pos = (0, 0), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 10)
gbs.AddGrowableCol(0)
self.SetSizer(gbs)
self.Layout()
def OnSelect(self, evt): #ADDED
print "HAHA"
class ListBoxComboPopup(wx.combo.ComboPopup):
def Init(self):
self.ItemList = []
def Create(self, parent):
self.ListBox = wx.ListBox(parent, -1, size = (-1, 20), choices = self.ItemList)
def GetControl(self):
return self.ListBox
def OnPopup(self):
pass
#-----------------------------------------------------------------------------#
if __name__ == '__main__':
APP = wx.App(False)
FRAME = MainFrame(None)
FRAME.Show()
APP.MainLoop()
You are missing a few things in your ListBoxComboPopup class needed to make it work well with the ComboCtrl. At a minimum you are missing some event binding and handler to catch selection events from the ListBox, and an implementation of the GetStringValue method which the combo will call to get the value to be displayed. Please see the ComboCtrl sample in the wxPython demo for more details and example code.

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

Python: Enable Radiobox based upon Checkbox Selection

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()

Python WX - Returning user input from wx Dialog

I'm new to Python and WX. I created a simple test dialog shown below that prompts the user with a combobox. I would like to capture the value from the combox in my main program. How do I call it from my main program?
This is how I was purposing to call it that displays the dialog but does not currently capture the value from the combobox:
import highlight
highlight.create(self).Show(True)
a = highlight.OnComboBox1Combobox(self)
print a
The name of the Dialog file is "highlight". Below is the code:
#Boa:Dialog:Dialog2
import wx
def create(parent):
return Dialog2(parent)
[wxID_DIALOG2, wxID_DIALOG2COMBOBOX1, wxID_DIALOG2STATICTEXT1,
] = [wx.NewId() for _init_ctrls in range(3)]
class Dialog2(wx.Dialog):
def _init_ctrls(self, prnt):
# generated method, don't edit
wx.Dialog.__init__(self, id=wxID_DIALOG2, name='', parent=prnt,
pos=wx.Point(264, 140), size=wx.Size(400, 485),
style=wx.DEFAULT_DIALOG_STYLE, title='Dialog2')
self.SetClientSize(wx.Size(384, 447))
self.comboBox1 = wx.ComboBox(choices=['test1', 'test2'],
id=wxID_DIALOG2COMBOBOX1, name='comboBox1', parent=self,
pos=wx.Point(120, 16), size=wx.Size(130, 21), style=0,
value=u'wining\n')
self.comboBox1.SetToolTipString(u'comboBox1')
self.comboBox1.SetLabel(u'wining\n')
self.comboBox1.Bind(wx.EVT_COMBOBOX, self.OnComboBox1Combobox,
id=wxID_DIALOG2COMBOBOX1)
self.staticText1 = wx.StaticText(id=wxID_DIALOG2STATICTEXT1,
label=u'test', name='staticText1', parent=self, pos=wx.Point(88,
16), size=wx.Size(19, 13), style=0)
def __init__(self, parent):
self._init_ctrls(parent)
##print get_selection
##print get_selection1
def OnComboBox1Combobox(self, event):
get_selection = self.comboBox1.GetValue()
return get_selection
There are lots of dialog examples out there. Here are a couple:
The Dialogs of wxPython (Part 1 of 2)
http://zetcode.com/wxpython/dialogs/
Basically, all you need to do is instantiate your dialog, show it and then before you close it, extract the value. The typical way to do it is something like this:
myDlg = MyDialog()
res = myDlg.ShowModal()
if res == wx.ID_OK:
value = myDlg.myCombobox.GetValue()
myDlg.Destroy()
Update: Here's a more full-fledged example:
import wx
########################################################################
class MyDialog(wx.Dialog):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Dialog.__init__(self, None, title="Dialog")
self.comboBox1 = wx.ComboBox(self,
choices=['test1', 'test2'],
value="")
okBtn = wx.Button(self, wx.ID_OK)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.comboBox1, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(okBtn, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(sizer)
########################################################################
class MainProgram(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Main Program")
panel = wx.Panel(self)
btn = wx.Button(panel, label="Open dialog")
btn.Bind(wx.EVT_BUTTON, self.onDialog)
self.Show()
#----------------------------------------------------------------------
def onDialog(self, event):
""""""
dlg = MyDialog()
res = dlg.ShowModal()
if res == wx.ID_OK:
print dlg.comboBox1.GetValue()
dlg.Destroy()
if __name__ == "__main__":
app = wx.App(False)
frame = MainProgram()
app.MainLoop()

How to use Refresh with a wx.ColourDialog in wxpython?

i am trying to Refresh() a panel which uses the wx.ColourDialog. Once I refresh the panel once, it is unable to refresh again. Try the following to see the problem in action.
By clicking the button, it will ask you what color you would like to change the rectangle to. Once you press OK, it should change the rectangles color. It will not work it will not change the rectangle.
import wx
xcolor_of_font_dia=(0,0,0)
class MyFrame(wx.Frame):
"""a frame with a panel"""
def __init__(self, parent=None, id=wx.ID_ANY, title=None):
global xcolor_of_font_dia
global dc
wx.Frame.__init__(self, parent, wx.ID_ANY, title)
self.panel = wx.Panel(self, size=(350, 200))
self.panel.Bind(wx.EVT_PAINT, self.on_paint)
self.button2 = wx.Button(self.panel, id=wx.ID_ANY, label='Button2',pos=(8, 38), size=(175, 28))
self.button2.Bind(wx.EVT_BUTTON, self.onColorDlg)
self.Fit()
def onColorDlg(self, event):
global xcolor_of_font_dia
global dc
"""
This is mostly from the wxPython Demo!
"""
dlg = wx.ColourDialog(self)
# Ensure the full colour dialog is displayed,
# not the abbreviated version.
dlg.GetColourData().SetChooseFull(True)
if dlg.ShowModal() == wx.ID_OK:
data = dlg.GetColourData()
print 'You selected: %s\n' % str(data.GetColour().Get())
xcolor_of_font_dia='#%02x%02x%02x' % data.GetColour().Get()
dlg.Destroy()
self.panel.Refresh()
def on_paint(self, event):
global xcolor_of_font_dia
global dc
dc = wx.PaintDC(self.panel)
dc.SetPen(wx.Pen(xcolor_of_font_dia, 1))
rect = wx.Rect(50, 50, 100, 100)
dc.DrawRoundedRectangleRect(rect, 8)
# test it ...
app = wx.PySimpleApp()
frame1 = MyFrame(title='rounded-rectangle & circle')
frame1.Center()
frame1.Show()
app.MainLoop()
I cleaned your code a bit. Basically your globals were producing some problems as you were creating (and deleting) different dc instances after every size event.
You should not use globals if it is not strictly necessary (rarely is).
This works:
import wx
class MyFrame(wx.Frame):
"""a frame with a panel"""
def __init__(self, parent=None, id=wx.ID_ANY, title=None):
wx.Frame.__init__(self, parent, wx.ID_ANY, title)
self.xcolor = (0, 0, 0)
self.panel = wx.Panel(self, size=(350, 200))
self.panel.Bind(wx.EVT_PAINT, self.on_paint)
self.button2 = wx.Button(self.panel, id=wx.ID_ANY, label='Button2',
pos=(8, 38), size=(175, 28))
self.button2.Bind(wx.EVT_BUTTON, self.onColorDlg)
self.Fit()
def onColorDlg(self, event):
"""
This is mostly from the wxPython Demo!
"""
dlg = wx.ColourDialog(None)
dlg.GetColourData().SetChooseFull(True)
if dlg.ShowModal() == wx.ID_OK:
data = dlg.GetColourData()
self.xcolor = data.GetColour().Get()
print 'You selected: %s\n' % str(self.xcolor)
dlg.Destroy()
self.panel.Refresh()
def on_paint(self, event):
dc = wx.PaintDC(self.panel)
dc.SetPen(wx.Pen(self.xcolor, 2))
rect = wx.Rect(50, 50, 100, 100)
dc.DrawRoundedRectangleRect(rect, 8)
# test it ...
app = wx.PySimpleApp()
frame1 = MyFrame(title='rounded-rectangle & circle')
frame1.Center()
frame1.Show()
app.MainLoop()

Categories