WxPython using Listbox and other UserInput with a Button - python

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

Related

Wx.stc.StyledTextCtrl ScrollBar

In my project, I use wx.stc.StyledTextCtrl(). I bind the events of key down and key up. When I want to add a letter into the TextCtrl, I don’t skip the event, in some reasons, I use the method AddText() in order to add a text. When the text is long and the ScrollBar (of the width of the screen) is opened I want that ScrollBar will be at the position where I can see the letter that is added (will move automatically as it should be). currently the ScrollBar always stays at the left side of the screen.
I'm searching for a function that can do that.
when the letters are over the width of the TextCtrl (over pos 300) the ScrollBar is still doesn't move. I want that it will be like the messageTxt in the right side of the frame.
Here is a basic code that present my problem:
import wx
import wx.stc
def on_key_down(event):
pass
def on_key_up(event):
key_code = event.GetKeyCode()
messageTxt.AddText(chr(key_code))
app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = wx.stc.StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(300, 300),
style=wx.TE_MULTILINE, name="File")
messageTxt.Bind(wx.EVT_KEY_DOWN, on_key_down)
messageTxt.Bind(wx.EVT_KEY_UP, on_key_up)
messageTxt2 = wx.stc.StyledTextCtrl(frame, id=wx.ID_ANY, pos=(320, 0), size=(150, 150),
style=wx.TE_MULTILINE, name="File")
app.SetTopWindow(frame)
app.MainLoop()
Clearly another event is ocurring after the key event, which is being missed.
Use event.Skip() in the functions bound to a key event.
Skip(self, skip=True)
This method can be used inside an event handler to control whether further event handlers bound to this event will be called after the current one returns.
Without Skip (or equivalently if Skip(false) is used), the event will not be processed any more. If Skip(true) is called, the event processing system continues searching for a further handler function for this event, even though it has been processed already in the current handler.
In general, it is recommended to skip all non-command events to allow the default handling to take place. The command events are, however, normally not skipped as usually a single command such as a button click or menu item selection must only be processed by one handler.
import wx
import wx.stc
def on_key_down(event):
event.Skip()
pass
def on_key_up(event):
key_code = event.GetKeyCode()
messageTxt.AddText(chr(key_code))
event.Skip()
app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = wx.stc.StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(300, 300),
style=wx.TE_MULTILINE, name="File")
messageTxt.Bind(wx.EVT_KEY_DOWN, on_key_down)
messageTxt.Bind(wx.EVT_KEY_UP, on_key_up)
messageTxt2 = wx.stc.StyledTextCtrl(frame, id=wx.ID_ANY, pos=(320, 0), size=(150, 150),
style=wx.TE_MULTILINE, name="File")
app.SetTopWindow(frame)
app.MainLoop()
I've added a different answer because the first issue is still valid, just not for your question, as I interpret it.
I assume this is a mock-up of your problem, if it isn't, let me know and I'll delete it.
The "server updates" are simulated by a timer, the character is added, then we simply move the cursor 1 character to the right.
import wx
import wx.stc
server_sends=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','T','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','T']
class MyFrame(wx.Frame):
def __init__(self, parent, id=wx.ID_ANY, title=""):
super(MyFrame, self).__init__(parent, id, title)
self.SetSize((500,500))
self.panel = wx.Panel(self, -1 , size=(500,500))
self.messageTxt = wx.stc.StyledTextCtrl(self.panel, id=wx.ID_ANY, pos=(0, 0), size=(300, 300),
style=wx.TE_MULTILINE, name="File")
self.messageTxt2 = wx.stc.StyledTextCtrl(self.panel, id=wx.ID_ANY, pos=(320, 0), size=(150, 150), style=wx.TE_MULTILINE, name="File")
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.timer.Start(500)
self.cnt = 0
self.Show()
def OnTimer(self, event):
print (server_sends[self.cnt])
self.Server_Update(server_sends[self.cnt])
self.cnt += 1
if self.cnt > len(server_sends) - 1 :
self.timer.Stop()
def Server_Update(self,char):
self.messageTxt.AddText(char)
self.messageTxt.CharRight()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame(None,title="The Main Frame")
app.MainLoop()

wxPython checkbox true or false

So let's say that I have a checkbox in wxPython:
cb1 = wx.CheckBox(panelWX, label='TIME', pos=(20, 20))
cb1.SetValue(False)
Is there a simple way I could check to see whether it has changed to true?
Like this maybe?
if cb1.SetValue == True:
And from that point append something from the action of it being true?
Like so:
selectionSEM1.append('Time')
you just need to use GetValue() method. look at this example from wxpython wiki:
#!/usr/bin/python
# checkbox.py
import wx
class MyCheckBox(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(250, 170))
panel = wx.Panel(self, -1)
self.cb = wx.CheckBox(panel, -1, 'Show Title', (10, 10))
self.cb.SetValue(True)
wx.EVT_CHECKBOX(self, self.cb.GetId(), self.ShowTitle)
self.Show()
self.Centre()
def ShowTitle(self, event):
if self.cb.GetValue():#here you check if it is true or not
self.SetTitle('checkbox.py')
else: self.SetTitle('')
app = wx.App(0)
MyCheckBox(None, -1, 'checkbox.py')
app.MainLoop()
You want to use Events.
def do_something(event):
box = event.GetEventObject()
setting = box.GetValue()
if setting:
selectionSEM1.append('Time')
event.Skip()
cb1.Bind(wx.EVT_CHECKBOX, do_something, cb1)

How to refresh listctrl simultaneously when using setstringitem function

I'm so troubled by this problem:
I've create a ListCtrl object, a TextCtrl object, and a button. First I fill some data into the ListCtrl object, when I press the button, it will append some strings into TextCtrl object and use SetStringItem to modify ListCtrl object.
As you can see in the button function, I've added time.sleep(2) in each loop. When I've got is when the button is pressed, TextCtrl will be refreshed every time the strings is inserted, but ListCtrl just freeze until the LOOP IS FINISHED, then it will display the correct strings.
I want to know how to refresh the ListCtrl object as soon as SetStringItem is called.
Any help is deeply appreciated.
Here is the code:
import wx
import sys
import time
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, size=(450, 450))
self.panel = wx.Panel(self)
self.dl = wx.ListCtrl(self,-1,size=(300,100),style=wx.LC_REPORT)
self.dl.InsertColumn(0, 'File')
self.dl.InsertColumn(1, 'Progress')
self.dl.InsertColumn(2, 'State')
for row in range(3):
labels = [l+str(row) for l in "FILE PERCENT STATE".split()]
# sys.maxint inserts at the end of the list
index = self.dl.InsertStringItem(sys.maxint, labels[0])
self.dl.SetStringItem(index, 1, labels[1])
self.dl.SetStringItem(index, 2, labels[2])
self.Show(True)
button2 = wx.Button(self, label=u"test", pos=(15, 200), size=(60, 25))
self.Bind(wx.EVT_BUTTON, self.test, button2)
self.text = wx.TextCtrl(self, -1, pos=(80, 200), size=(200, 175), style=wx.TE_MULTILINE)
def test(self,event):
for i in range(3):
self.dl.SetStringItem(i,1,"HELLO")
self.text.AppendText("HELLO")
time.sleep(2)
app = wx.App()
Frame(None)
app.MainLoop()
The problem is that time.sleep blocks your GUI, what you will need to do to get the effect that you are trying for is:
On your button press add the first item & start a 2 second wx.Timer with an event handler/
In the event handler add the next string or if there are no more to add cancel the timer.
I've changed my code to this, and it works, thanks steve
import wx
import sys
import time
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, size=(450, 450))
self.panel = wx.Panel(self)
self.dl = wx.ListCtrl(self,-1,size=(300,100),style=wx.LC_REPORT)
self.dl.InsertColumn(0, 'File')
self.dl.InsertColumn(1, 'Progress')
self.dl.InsertColumn(2, 'State')
for row in range(3):
labels = [l+str(row) for l in "FILE PERCENT STATE".split()]
# sys.maxint inserts at the end of the list
index = self.dl.InsertStringItem(sys.maxint, labels[0])
self.dl.SetStringItem(index, 1, labels[1])
self.dl.SetStringItem(index, 2, labels[2])
self.Show(True)
self.timer = wx.Timer(self,-1)
#self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.Bind(wx.EVT_TIMER, self.test1, self.timer)
button2 = wx.Button(self, label=u"test", pos=(15, 200), size=(60, 25))
self.Bind(wx.EVT_BUTTON, self.test, button2)
self.text = wx.TextCtrl(self, -1, pos=(80, 200), size=(200, 175), style=wx.TE_MULTILINE)
self.z=0
def test(self,event):
self.timer.Start(3000)
def test1(self,event):
for i in range(1):
self.dl.SetStringItem(self.z,1,"HELLO")
self.text.AppendText("HELLO")
self.z+=1
if self.z >2 :
self.timer.Stop()
app = wx.App()
Frame(None)
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()

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.

Categories