Related
The following program has a memory leak (I verified this on a linux system, with the top command)
import wx
from random import randint
class MyPanel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self,parent)
self._parent = parent
self._nb_buttons = 0
self._main_sizer = wx.BoxSizer(wx.VERTICAL)
self._ctrl_sizer = wx.BoxSizer(wx.HORIZONTAL)
self._btns_sizer = wx.BoxSizer(wx.VERTICAL)
self._btn_tim = wx.Button(self,label="Start auto")
self._btn_tim.Bind(wx.EVT_BUTTON, self.OnStartStopEvent)
self._ctrl_sizer.Add(self._btn_tim, 0, wx.CENTER|wx.ALL, 5 )
self._main_sizer.Add(self._ctrl_sizer, 0, wx.CENTER )
self._main_sizer.Add(self._btns_sizer, 0, wx.CENTER|wx.ALL,10)
self.SetSizer(self._main_sizer)
self._timer1 = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self._timer1)
self._timer_running = False
def OnStartStopEvent(self,event):
if self._timer_running: # toggle state of Timer
self._timer1.Stop()
self._btn_tim.SetLabel("Start")
self._timer_running = False
else:
self._timer1.Start(200,False)
self._btn_tim.SetLabel("Stop")
self._timer_running = True
def AddWidget(self):
self._nb_buttons += 1
label = "Button %d" % self._nb_buttons
name = "button%d" % self._nb_buttons
new_button = wx.Button(self,label=label, name=name)
self._btns_sizer.Add( new_button, 0, wx.ALL, 5 )
self._parent._my_sizer.Layout()
self._parent.Fit()
def RemoveWidget(self):
if self._btns_sizer.GetChildren():
self._btns_sizer.Hide(self._nb_buttons -1 )
self._btns_sizer.Remove(self._nb_buttons -1 )
self._nb_buttons -= 1
self._parent._my_sizer.Layout()
self._parent.Fit()
def OnTimer(self,event):
n = randint(-3,3)
if self._nb_buttons < 2: n += randint(0,2)
if self._nb_buttons > 10: n -= randint(0,3)
while n > 0:
self.AddWidget()
n -= 1
while n < 0:
self.RemoveWidget()
n += 1
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,parent=None, title="Add remove buttons")
self._my_sizer = wx.BoxSizer(wx.VERTICAL)
panel1 = MyPanel(self)
self._my_sizer.Add(panel1, 1, wx.EXPAND)
self.SetSizer(self._my_sizer)
self.Fit()
self.Show()
def main():
app = wx.App(False)
frame1 = MyFrame()
app.MainLoop()
if __name__ == '__main__':
main()
The problem is that in RemoveWidget, the button is removed from the Sizer, but not from the parent wx.Panel.
What is the way to remove the wx.Button from the parent (wx.Panel in this case)?
When I searched for "remove wxPython widget", the answers I found only told how to remove from sizers. (In fact, my code is a variation from http://www.blog.pythonlibrary.org/2012/05/05/wxpython-adding-and-removing-widgets-dynamically/ ).
There is a RemoveChild method in the wx.Window class, but documentation says it is internal and should not be called by user code (https://wxpython.org/Phoenix/docs/html/wx.Window.html#wx.Window.RemoveChild)
I think I figured it out. I'm on windows 10, but I observed the same memory leak. Modifying the RemoveWidget method to this solved it on my computer.
See code comments for an explanation
Edited to reflect inputs from RobinDunn
def RemoveWidget(self):
if self._btns_sizer.GetChildren():
# GetItem returns a SizerItem, to get the actual button we have to call GetWindow
window = self._btns_sizer.GetItem(self._nb_buttons - 1).GetWindow()
# Calling Destroy removes widget from parent and sizer
window.Destroy()
self._nb_buttons -= 1
self._parent._my_sizer.Layout()
self._parent.Fit()
I'm a very beginner of Python Programming, and now I'm wondering why my widgets cannot show number repeatedly.
That is, I'd like to let the text show numbers from 1 to 9, but in the while loop, it only shows 9....
Any suggestions?
Here is my code(Python version: 2.6):
#!/user/bin/python
import wx
class Frame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self, parent, id,
'Show Number',
size = (200,150),
style=wx.MINIMIZE_BOX | wx.RESIZE_BORDER
| wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
self.initUI()
def initUI(self):
widgetPanel=wx.Panel(self, -1)
widgetPanel.SetBackgroundColour('white')
# Buttons for play the simulation
playButton = wx.Button(widgetPanel, -1, "Play", pos=(10,10), size=(30,30))
self.Bind(wx.EVT_BUTTON, self.play, playButton)
playButton.SetDefault()
# Time
self.timeText = wx.TextCtrl(widgetPanel, -1, "", pos=(10, 50),
size =(100,30), style=wx.TE_CENTER)
self.timeText.SetBackgroundColour('white')
self.timeText.SetFont(wx.Font(20, wx.DECORATIVE, wx.NORMAL, wx.NORMAL))
def play(self, event):
#self.timeText.SetValue("19:32")
self.show_num()
def show_num(self):
x = 0
while(x < 10):
self.timeText.SetValue(str(x))
x += 1
if __name__ == "__main__":
app = wx.App(False)
frame = Frame(parent=None,id=-1)
frame.Show()
app.MainLoop()
you need to give your app the chance to update .... the easiest and most correct way of doing this is to use timers instead of a loop ... I have included a minimal example
import wx
app = wx.App(redirect=False)
frame = wx.Frame(None,-1,"Counter")
btn = wx.Button(f,-1,"Click Me!")
timer = wx.Timer() # this will update the count
def onUpdateButton(evt):
next_int = int(btn.GetLabel())+1
btn.SetLabel(str(next_int))
if next_int > 10:
timer.Stop()
btn.Enable(True)
def onButtonClick(event):
btn.SetLabel("0")
btn.Enable(False)
timer.Start(1000) # 1 second updates
timer.Bind(wx.EVT_TIMER,onUpdateButton)
btn.Bind(wx.EVT_BUTTON,onButtonClick)
frame.Show()
app.MainLoop()
it is probably obvious to most users, however may be worth mentioning that instead of keeping a variable with the current count i am just using the label string
Like the other answer said, you need to give time to your app to update.
I have made a few additions to your code to make it work:
import wx
import time
import thread
class Frame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self, parent, id,
'Show Number',
size = (200,150),
style=wx.MINIMIZE_BOX | wx.RESIZE_BORDER
| wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
self.initUI()
def initUI(self):
widgetPanel=wx.Panel(self, -1)
widgetPanel.SetBackgroundColour('white')
# Buttons for play the simulation
playButton = wx.Button(widgetPanel, -1, "Play", pos=(10,10), size=(30,30))
self.Bind(wx.EVT_BUTTON, self.play, playButton)
playButton.SetDefault()
# Time
self.timeText = wx.TextCtrl(widgetPanel, -1, "", pos=(10, 50),
size =(100,30), style=wx.TE_CENTER)
self.timeText.SetBackgroundColour('white')
self.timeText.SetFont(wx.Font(20, wx.DECORATIVE, wx.NORMAL, wx.NORMAL))
def play(self, event):
#self.timeText.SetValue("19:32")
#self.show_num()
thread.start_new_thread(self.show_num,())
def show_num(self):
x = 0
while(x < 10):
self.timeText.SetValue(str(x))
time.sleep(1)
x += 1
if __name__ == "__main__":
app = wx.App(False)
frame = Frame(parent=None,id=-1)
frame.Show()
app.MainLoop()
Notice that time.sleep(1) pauses the while loop for 1 second so that the display can be updated. You can reduce or increase the pause time as you desire.
I experienced an update issue with:
self.timeText.SetValue(str(x))
time.sleep(1)
x += 1
I added a wx.YieldIfNeeded() before the sleep:
self.timeText.SetValue(str(x))
wx.YieldIfNeeded()
wx.sleep(1)
x += 1
This makes sure the GUI is updated and other events are handled.
I also would use the wx.Sleep(). This prevents using an other library...
I am trying to put together a gui in Python for a school project, but I am getting an error and don't really understand why.
self.prompt.addAnswer(i, self.ansControls[i].GetValue()) File
"C:\Python27\lib\site-packages\wx-3.0-msw\wx_core.py", line 16712, in
getattr
raise PyDeadObjectError(self.attrStr % self._name) wx._core.PyDeadObjectError: The C++ part of the TextCtrl object has
been deleted, attribute access no longer allowed.
I understand what the error means, The TextCtrl object no longer exists so I can't access it. I do not understand why the TextCtrl object does not exist anymore. Here is how the flow goes:
The frame appears with labels, TextCtrls, and buttons. The user enters in data and hits next. Everything goes smoothly. A different instance of the same PromptFrame class is then created, and the same thing happens. This time however, when the user hits next, I get the aforementioned error. Here is the code:
The service in the background running the show:
class AppService(object):
prompts = [Prompt_1, Prompt_2, Prompt_3, Prompt_4, Prompt_5, Prompt_6, Prompt_7,
Prompt_8, Prompt_9, Prompt_10, Prompt_11, Prompt_12, Prompt_13, Prompt_14,
Prompt_15, Prompt_16, Prompt_17, Prompt_18, Prompt_19]
skippedPromptIndices = []
def __init__(self):
print "Service Started"
PromptFrame(self, self.prompts[0], 0, len(self.prompts))
def doBack(self, curIndex, curPrompt):
if curIndex >= 0:
self.prompts[curIndex] = curPrompt
PromptFrame(self, self.prompts[curIndex - 1], curIndex - 1, len(self.prompts))
else:
posCurIndex = (curIndex * -1) - 1
self.prompts[posCurIndex] = curPrompt
backIndex = self.skippedPromptIndices.index(curIndex) - 1
nextPromptIndex = 0
if backIndex < 0:
nextPromptIndex = len(self.prompts) - 1
else:
nextPromptIndex = self.skippedPromptIndices[backIndex]
PromptFrame(self, self.prompts[(nextPromptIndex * -1) - 1], nextPromptIndex, len(self.prompts))
def doSkip(self, curIndex, curPrompt):
skipIndex = (curIndex + 1) * -1
if self.skippedPromptIndices.count(skipIndex) > 0:
self.skippedPromptIndices.remove(skipIndex)
self.skippedPromptIndices.append(skipIndex)
self.doNext(curIndex, curPrompt)
def doNext(self, curIndex, curPrompt):
if curIndex >= 0:
self.prompts[curIndex] = curPrompt
else:
self.prompts[(curIndex * -1) - 1] = curPrompt
if (curIndex >= 0 and curIndex < (len(self.prompts) - 1)):
PromptFrame(self, self.prompts[curIndex + 1], curIndex + 1, len(self.prompts))
elif len(self.skippedPromptIndices) > 0:
skipIndex = self.skippedPromptIndices.pop(0)
nextIndex = (skipIndex * -1) - 1
PromptFrame(self, self.prompts[nextIndex], skipIndex, len(self.prompts))
else:
dlg = wx.MessageDialog(self, "Done!", "Message", wx.OK)
dlg.ShowModal() # Shows it
dlg.Destroy() # finally destroy it when finished.
and here is the PromptFrame Class:
class PromptFrame(wx.Frame):
appService = None
prompt = None
promptIndex = 0
numPrompts = 0
ansControls = []
def __init__(self, appService, prompt=testPrompt, promptIndex=0, numPrompts=0):
print "New Prompt Frame!"
self.appService = appService
self.prompt = prompt
self.promptIndex = promptIndex
self.numPrompts = numPrompts
wx.Frame.__init__(self, None, wx.ID_ANY, title=prompt.title)
self.panel = wx.Panel(self, wx.ID_ANY)
self.panel.SetBackgroundColour('#2FC0E0') #0013E3, #66DFFA, #2FC0E0
self.Maximize()
self.CreateStatusBar() # A Statusbar in the bottom of the window
# Setting up the menu.
filemenu= wx.Menu()
menuAbout= filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
# Creating the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
# Creating fonts for the controls
titleFont = wx.Font(24, wx.DECORATIVE, wx.ITALIC, wx.BOLD)
qFont = wx.Font(12, wx.FONTFAMILY_DECORATIVE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
#Creating layout
vertSizer = wx.BoxSizer(wx.VERTICAL)
lblTitle = wx.StaticText(self.panel, wx.ID_ANY, prompt.title)
lblTitle.SetFont(titleFont)
vertSizer.Add(lblTitle, 0, wx.ALIGN_CENTER | wx.TOP, border=30)
vertSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY), 0, wx.EXPAND)
vertSizer.AddStretchSpacer(2)
for i in range(len(prompt.questions)):
if prompt.qTypes[i] == 0:
lbl = wx.StaticText(self.panel, wx.ID_ANY, prompt.questions[i], size=(200, -1))
lbl.SetFont(qFont)
lbl.Wrap(200)
ans = wx.TextCtrl(self.panel, wx.ID_ANY, size=(200,-1))
if not prompt.answers[i] == None:
ans.SetValue(prompt.answers[i])
self.ansControls.append(ans)
horizSizer = wx.BoxSizer(wx.HORIZONTAL)
horizSizer.Add((30, -1), 0)
horizSizer.Add(lbl, 0)
horizSizer.Add((20, -1), 0)
horizSizer.Add(self.ansControls[len(self.ansControls) - 1], 0)
vertSizer.Add(horizSizer, 0)
vertSizer.AddStretchSpacer(1)
print self.ansControls
vertSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY), 0, wx.EXPAND)
self.btnBack = wx.Button(self.panel, wx.ID_ANY, "Back")
self.btnSkip = wx.Button(self.panel, wx.ID_ANY, "Skip")
self.btnNext = wx.Button(self.panel, wx.ID_ANY, "Next")
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer.Add(self.btnBack, 0, wx.RIGHT, border=30)
btnSizer.Add(self.btnSkip, 0)
btnSizer.Add(self.btnNext, 0, wx.LEFT, border=30)
btnSizer.AddStretchSpacer(1)
vertSizer.AddStretchSpacer(2)
vertSizer.Add(btnSizer, 0, wx.ALIGN_CENTER)
vertSizer.AddStretchSpacer(2)
lblPage = wx.StaticText(self.panel, wx.ID_ANY, "Page: " + str(self.promptIndex) + "/" + str(self.numPrompts))
vertSizer.Add(lblPage, 0, wx.ALIGN_BOTTOM | wx.ALIGN_RIGHT | wx.ALL, border=20)
self.panel.SetSizer(vertSizer)
# Events.
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.Bind(wx.EVT_BUTTON, self.OnBack, self.btnBack)
self.Bind(wx.EVT_BUTTON, self.OnSkip, self.btnSkip)
self.Bind(wx.EVT_BUTTON, self.OnNext, self.btnNext)
self.Show()
def OnAbout(self,e):
# Create a message dialog box
aboutString = "This program was designed by students of Worcester Polytechnic Institute to aid water supply " \
"officials apply the World Health Organization's Water Safety Plans."
dlg = wx.MessageDialog(self, aboutString, "About Water Safety Plan Helper", wx.OK)
dlg.ShowModal() # Shows it
dlg.Destroy() # finally destroy it when finished.
def OnExit(self,e):
self.Close(True) # Close the frame.
def OnBack(self, e):
if (self.promptIndex > 0):
self.SaveAns()
self.appService.doBack(self.promptIndex, self.prompt)
self.Close(True)
else:
errorString = "There are no previous pages to go back to"
dlg = wx.MessageDialog(self, errorString, "Error", wx.OK)
dlg.ShowModal() # Shows it
dlg.Destroy() # finally destroy it when finished.
def OnSkip(self, e):
self.SaveAns()
self.appService.doSkip(self.promptIndex, self.prompt)
self.Close(True)
def OnNext(self, e):
self.SaveAns()
self.appService.doNext(self.promptIndex, self.prompt)
self.Close(True)
def SaveAns(self):
print self.ansControls
for i in range(len(self.ansControls)):
if self.prompt.qTypes[i] == 0:
self.prompt.addAnswer(i, self.ansControls[i].GetValue())
Thanks for your help guys!
EDIT: Here is my init.py file:
from MainFrame import MainFrame
import wx
app = wx.App(False)
frame = MainFrame(None, "My App")
app.MainLoop()
ansControls is currently defined as a class variable. Which means that any control defined in any window gets added to it.
You create a control in the first instance, it is added to the class, but the window belongs to the instance. So when you destroy the class, the instance gets destroyed, but the python object pointing to it still exists.
Then you open your second window, add more controls, and then hit the loop where you loop over them. The first ones in your loop will not have a valid C++ object below it anymore, and will fail.
Not sure why they were defined as class variables, but you either need to keep a pointer to the root window as well, delete class controls as the parent windows get deleted, or (simpler) just make ansControls an instance variable instead of a class variable...
does anyone of you have an example how to make the following possible:
I have a listctrl that displays > 600 items. Now I need to search in these items for a text the user inputs and update the list to only show the items containing this string.
So let us say the list contains "Hello", "Hi" and "Morning". The list displays all three items. Now the user types "h" into the textctrl, and the listctrl is narrowed down to "Hello" and "Hi". If the user instead types "o", and the list becomes "Hello" and "Morning".
Is this possible? Or is there any other convenient way to find an item in a listctrl? The build in "find as you type" is only of real use if you do exactly know what you search for - and in my case this will not really be the case...
Thanks, Woodpicker
The wxPython demo has a pretty good "type-ahead" filter built into it. Looking at the source code to Main.py they do it the "manual way", loop and rebuild the list. They are using a treeview but the ideas are sound:
def OnSearch(self, event=None):
value = self.filter.GetValue()
if not value:
self.RecreateTree()
return
wx.BeginBusyCursor()
for category, items in _treeList:
self.searchItems[category] = []
for childItem in items:
if SearchDemo(childItem, value):
self.searchItems[category].append(childItem)
wx.EndBusyCursor()
self.RecreateTree()
I like the ObjectListView wrapper better than the straight wx.ListCtrl. It includes the ability to filter items in the control as a feature of the widget. You can read about it here: http://objectlistview.sourceforge.net/python/features.html#filtering and here's the main page for the control: http://objectlistview.sourceforge.net/python/
Here is an example of filtering an UltimateListCtrl.
I know this is 2 years later but I've found other examples on stackoverflow really, really helpful.
I'm new to python/wxpython but hopefully it will be useful.
starting from http://www.blog.pythonlibrary.org/2011/11/02/wxpython-an-intro-to-the-ultimatelistctrl/
import wx
from wx.lib.agw import ultimatelistctrl as ULC
class ULC_Panel(wx.Panel):
""""""
def __init__(self, parent, col_headers=None, list_data=None, options=None, dlg=None,
selected_list=None):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.options = options
self.show_only_selected = False
self.filter_string = ""
hsizer = wx.BoxSizer(wx.HORIZONTAL)
okayButton = wx.Button(self, wx.ID_OK, "OK")
okayButton.SetToolTip(wx.ToolTip("Click to close this dialog and use the selections"))
self.Bind(wx.EVT_BUTTON, self.OnOkayCanButton, okayButton)
hsizer.Add(okayButton, 0, wx.ALL, 5)
canButton = wx.Button(self, wx.ID_CANCEL, "Cancel")
canButton.SetToolTip(wx.ToolTip("Click to close this dialog and cancel selections"))
self.Bind(wx.EVT_BUTTON, self.OnOkayCanButton, canButton)
hsizer.Add(canButton, 0, wx.ALL, 5)
cb_show_only = wx.CheckBox(self, -1, "Show only selected items?")
cb_show_only.SetValue(self.show_only_selected)
cb_show_only.SetToolTip(wx.ToolTip("Click to show only selected rows"))
self.Bind(wx.EVT_CHECKBOX, self.EvtShowOnly, cb_show_only)
hsizer.Add(cb_show_only, 0, wx.ALL, 5)
self.stext = wx.StaticText(self, -1, "Filter: ", style=wx.ALIGN_LEFT)
self.filtr = wx.TextCtrl(self, -1, "", style=wx.ALIGN_LEFT)
self.Bind(wx.EVT_TEXT, self.OnFiltr, self.filtr)
fsizer = wx.BoxSizer(wx.HORIZONTAL)
fsizer.Add(self.stext, 0, wx.ALL)
fsizer.Add(self.filtr, 1, wx.EXPAND)
font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
boldfont = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
boldfont.SetWeight(wx.BOLD)
boldfont.SetPointSize(12)
self.ultimateList = ULC.UltimateListCtrl(self, agwStyle = wx.LC_REPORT
| wx.LC_VRULES | ULC.ULC_HAS_VARIABLE_ROW_HEIGHT
| wx.LC_HRULES)
self.checkbox = [None] * len(list_data)
if selected_list != None:
self.selected = selected_list
else:
self.selected = [False] * len(list_data)
self.rows_max = len(list_data)
self.rows_current = -1
self.cols_max = len(col_headers)
self.cols_extra = 1
if options & ULC.ULC_MASK_CHECK:
self.cols_extra += 1
for i in xrange(self.cols_max+self.cols_extra):
info = ULC.UltimateListItem()
info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
info._image = []
info._format = 0
info._kind = 1
width = 150
if i >= self.cols_extra:
info._text = col_headers[i-self.cols_extra]
elif i == 0:
info._text = "Row"
width = 35
elif i == 1 and options & ULC.ULC_MASK_CHECK:
info._text = "Select"
width = 50
self.ultimateList.InsertColumnInfo(i, info)
self.ultimateList.SetColumnWidth(i, width)
self.list_data = list_data
pos = self.populate_table("")
if pos != None:
self.sz = self.ultimateList.GetItemRect(pos)
self.width = self.sz[2] + self.sz[3]
self.height = self.sz[1]
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(hsizer, 0, wx.EXPAND)
sizer.Add(fsizer, 0, wx.EXPAND)
sizer.Add(self.ultimateList, 1, flag=wx.EXPAND)
self.SetSizer(sizer)
sizer.Fit(self)
def EvtShowOnly(self, event):
cb = event.GetEventObject()
val = cb.GetValue()
self.show_only_selected = val
pos = self.populate_table(self.filter_string)
print "show_only_selected val= ", val
def EvtCheckBox(self, event):
cb = event.GetEventObject()
id = event.GetId()
val = cb.GetValue()
self.selected[id] = val
print "id, val= ", id, val
def OnOkayCanButton(self, event):
id = event.GetId()
dlg.EndModal(id)
def myGetNeedWH(self):
return (self.width, self.height)
def myGetSelectedState(self):
return self.selected
def populate_table(self, str_in):
busy = wx.BusyCursor()
if str_in:
str_low = str_in.lower()
if self.options & ULC.ULC_MASK_CHECK:
# if we have widgets in the row then we have to delete 1 row
# at a time (or else it leaves some of the widgets)
i = self.rows_current
#print "i, self.rows_max= ", i, self.rows_max
while i >= 0:
#print "i= ", i
self.ultimateList.DeleteItem(i)
i -= 1
else:
self.ultimateList.DeleteAllItems()
row = -1
for i in xrange(len(self.list_data)):
tlwr = self.list_data[i][0].lower()
if not str_in or tlwr.find(str_low) >= 0:
if self.show_only_selected and self.selected[i] == False:
continue
row += 1
for j in xrange(self.cols_max+self.cols_extra):
if j == 0:
pos = self.ultimateList.InsertStringItem(row, str(row))
elif j == 1 and self.options & ULC.ULC_MASK_CHECK:
self.checkbox[i] = wx.CheckBox(self.ultimateList, id= i)
self.checkbox[i].SetValue(self.selected[i])
self.checkbox[i].SetToolTip(wx.ToolTip("Click to select this row"))
self.Bind(wx.EVT_CHECKBOX, self.EvtCheckBox, self.checkbox[i])
self.ultimateList.SetItemWindow(pos, col=1, wnd=self.checkbox[i], expand=False)
else:
self.ultimateList.SetStringItem(row, j, self.list_data[i][j-self.cols_extra])
self.rows_current = row
return row
def OnFiltr(self, event):
str1 = event.GetString()
id = event.GetId()
#print "got txt_tval str[%s]= %s" % (id, str1)
self.filter_string = str1
pos = self.populate_table(str1)
event.Skip()
return
########################################################################
class FilterListDiag(wx.Dialog):
def __init__(self, parent, id, title, headers=None, data_table=None, options=None, selected_list=None):
wx.Dialog.__init__(self, parent, id, title="", style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
options_in = options
self.panel = ULC_Panel(self, col_headers=headers, list_data=data_table, options=options_in,
dlg=self, selected_list=selected_list)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.panel, 1, wx.EXPAND)
self.SetSizer(sizer)
(self.width, self.height) = self.panel.myGetNeedWH()
def myGetNeedWH(self):
return (self.width, self.height)
def myGetSelectedState(self):
return self.panel.myGetSelectedState()
class TestFrame(wx.Frame):
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="MvP UltimateListCtrl Demo", size=(850,600))
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = TestFrame()
col_headers = ['col0', 'col1', 'col2']
list_data = [
["Newsboys", "Go", "Rock"],
["Puffy", "Bring It!", "Pop"],
["Family Force 5", "III", "Pop"],
["Me2", "III", "Pop"],
["Duffy", "III", "Pop"],
["Fluffy", "III", "Pop"],
]
# sel_data passes in a list of which rows are already selected
sel_data = [
False,
False,
False,
False,
True,
False,
]
opt=ULC.ULC_MASK_CHECK # just reusing this to indicate I want a column of checkboxes.
dlg = FilterListDiag(frame, -1, "hi", headers=col_headers, data_table=list_data, options=opt, selected_list=sel_data)
(w, h) = dlg.myGetNeedWH()
print w,h
dlg.SetSizeWH(w, 300)
val = dlg.ShowModal()
selected = dlg.myGetSelectedState()
print "okay, can, val= ", wx.ID_OK, wx.ID_CANCEL, val
dlg.Destroy()
print 'selected=', selected
I have something different.
I created a dictionary and updated every time there is an entry in the list ctrl. Now, when I have a search box, which changes the list ctrl with each input from the keyboard and I clear and re-update my dictionary. This way, I always have the latest index for the values.
Code is as below:
class ViewUserDialog(wx.Dialog):
def __init__(self):
title = 'View Users Records'
super().__init__(parent=None, size=(750, 600), title=title)
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
self.list_sizer = wx.BoxSizer(wx.VERTICAL)
self.row_obj_dict = {}
.
.
.
self.list_ctrl_View_User.InsertColumn(0, "ID", width=150)
self.list_ctrl_View_User.InsertColumn(1, "User Name", width=150)
for index, users in original_user_frame.iterrows():
self.list_ctrl_View_User.InsertItem(indexes, user_id_value)
self.list_ctrl_View_User.SetItem(indexes, 1, users.NAME)
user_objects.append(users)
self.row_obj_dict[indexes] = users
indexes += 1
Now on searching, and selecting an item in lstctrl, you will still get your results:
def on_view(self, event):
selection = self.list_ctrl_View_User.GetFocusedItem()
self.list_ctrl_View_User.ClearAll()
if selection >= 0:
user = self.row_obj_dict[selection]
print(user)
self.list_ctrl_View_User.InsertColumn(0, "ID", width=150)
self.list_ctrl_View_User.InsertColumn(1, "User Name", width=150)
for index, users in original_user_frame.iterrows():
self.list_ctrl_View_User.InsertItem(indexes, user_id_value)
self.list_ctrl_View_User.SetItem(indexes, 1, users.NAME)
This will always give the currect selected item from the lstctrl and update the list with every input.
I'm working on an GUI program, and I use AppendText to update status in a multi-line text box(made of wx.TextCtrl). I noticed each time there's a new line written in this box, instead of smoothly adding this line to the end, the whole texts in the box just disappear(not in real, just visually) and I have to click the scroll button to check the newly updated/written status line. Why this happening? Should I add some styles? Hopefully you guys can help me out.
Here's my sample code:
import wx
import thread
import time
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, id = -1, title = "Testing", pos=(350, 110), size=(490,530), style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX)
panel = wx.Panel(self)
self.StartButton = wx.Button(parent = panel, id = -1, label = "Start", pos = (110, 17), size = (50, 20))
self.MultiLine = wx.TextCtrl(parent = panel, id = -1, pos = (38, 70), size = (410, 90), style = wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_AUTO_URL)
self.Bind(wx.EVT_BUTTON, self.OnStart, self.StartButton)
def OnStart(self, event):
self.StartButton.Disable()
thread.start_new_thread(self.LongRunning, ())
def LongRunning(self):
Counter = 1
while True:
self.MultiLine.AppendText("Hi," + str(Counter) + "\n")
Counter = Counter + 1
time.sleep(2)
class TestApp(wx.App):
def OnInit(self):
self.TestFrame = TestFrame()
self.TestFrame.Show()
self.SetTopWindow(self.TestFrame)
return True
def main():
App = TestApp(redirect = False)
App.MainLoop()
if __name__ == "__main__":
main()
try this:
self.logs = wx.TextCtrl(self, id=-1, value='', pos=wx.DefaultPosition,
size=(-1,300),
style= wx.TE_MULTILINE | wx.SUNKEN_BORDER)
self.logs.AppendText(text + "\n")
Try calling the Refresh() method on the textCtrl
Update:
A question has already been asked regarding this problem, here is the answer which pretty much solves it, -its not perfect but maybe you can improve on it...
Here is a thread from the wxpython mailing list regarding the problem which may also be of interest to you.