How to call function from other class with self? - python

One post right before mine, I found some Code I would like to use. There is an ComboPopup which has checkboxes in it. If one of These checkboxes is activated, I want to pass the selected text back to my class (i.e. MyForm). There is an StaticText called self.text. I want to Change the Label with the choosen Text of the ComboPopup.
I tried it with:
test = MyForm()
MyForm.OnUpdate(test,item.GetText())
as I thought that self.text is parent from MyForm(). But that doesn't work. No errors, but also no changes of the text.
What is self in this case? Is there a good way to find out what self is ? Like print the Name or anything :-)
My Code:
import wx
import wx.stc
from wx.lib.mixins.listctrl import CheckListCtrlMixin, ListCtrlAutoWidthMixin
class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin, ListCtrlAutoWidthMixin):
def __init__(self, parent):
wx.ListCtrl.__init__(self, parent, wx.ID_ANY, style=wx.LC_REPORT |
wx.SUNKEN_BORDER)
CheckListCtrlMixin.__init__(self)
ListCtrlAutoWidthMixin.__init__(self)
self.SetSize(-1, -1, -1, 50)
def OnCheckItem(self, index, flag):
item = self.GetItem(index)
if flag:
what = "checked"
else:
what = "unchecked"
print(f'{item.GetText()} - {what}')
test = MyForm()
MyForm.OnUpdate(test,item.GetText())
class ListViewComboPopup(wx.ComboPopup):
def __init__(self):
wx.ComboPopup.__init__(self)
self.lc = None
def AddItem(self, txt):
self.lc.InsertItem(0, txt)
def Init(self):
self.value = -1
self.curitem = -1
def Create(self, parent):
self.lc = CheckListCtrl(parent)
self.lc.InsertColumn(0, '', width=90)
return True
def GetControl(self):
return self.lc
def OnPopup(self):
wx.ComboPopup.OnPopup(self)
def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
return wx.ComboPopup.GetAdjustedSize(
self, minWidth, 110, maxHeight)
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Popup Menu")
self.panel = wx.Panel(self)
vsizer = wx.BoxSizer(wx.VERTICAL)
comboCtrl = wx.ComboCtrl(self.panel, wx.ID_ANY, "Select Text")
popupCtrl = ListViewComboPopup()
comboCtrl.SetPopupControl(popupCtrl)
popupCtrl.AddItem("Text One")
self.txt = wx.StaticText(self.panel,-1,style = wx.ALIGN_LEFT)
self.txt.SetLabel("Startup Text")
vsizer.Add(comboCtrl,1,wx.EXPAND)
vsizer.Add(self.txt,1,wx.EXPAND)
self.panel.SetSizer(vsizer)
def OnUpdate(self, txt):
self.txt.SetLabel(txt)
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm().Show()
app.MainLoop()

Your wx.Frame subclass instance does not have a parent. You explicitly create it without one:
wx.Frame.__init__(self, None, title="Popup Menu")
You create an instance of MyForm in your __name__ == '__main__' block:
frame = MyForm().Show()
# Note: your name 'frame' holds the return value of the method Show(), i.e. a boolean
# This probably should rather read:
# frame = MyForm()
# frame.Show()
This is the MyForm instance you show in your app.
What you do here:
test = MyForm()
is creating a new instance of MyFrame (that has nothing to do with the one your app shows). You then call onUpdate on that new instance of your MyForm class
MyForm.OnUpdate(test,item.GetText())
Since you never Show() that new instance, you can't see the effect of your operation. However, you probably don't want/need that new instance anyway.
You need your instance from the main block.
There is a parent argument on the CheckListCtrl initializer. This might contain a chain of objects which you probably can ascend until you reach your MyForm instance.
I can't tell for sure, since it is not visible where and how this is called in the ListViewComboPopup:
def Create(self, parent):
self.lc = CheckListCtrl(parent)
Do a print(self.Parent) in OnCheckItem to see what it contains and then add another .Parent to self.Parent until you hopefully end up on a <__main__.MyForm instance [...]>. This is where you want to call the onUpdate Method. That should look similar to this:
self.Parent.Parent.Parent.OnUpdate(item.GetText())
# the number of '.Parent' my vary, based on where in the chain you find your MyForm instance
Edit
As per the OP's comment, the parent attribute on wx objects is spelled with a capital P. The respective code snippets have been updated accordingly.

I don't know what wx library does but there is a way to check where .text is.
You want vars() mixed with pprint():
from pprint import pprint
pprint(vars(your_object))
pprint(your_object) # this is OK too
Suggestion 2
type(x).__name__
This gets you the class name of an instance. You could insert this line before self.text. And give self as argument instead of x.
Original: Link

Related

PyQt Get specific value in ListWidget

I am new to python and pyqt/pyside ...
i make customwidget class consist of 2 label (title & desc) which is example instance to add to Listwidget later ...
here is the complete clean code (pyside maya)
import PySide.QtCore as qc
import PySide.QtGui as qg
class CustomQWidget (qg.QWidget):
def __init__ (self, parent = None):
super(CustomQWidget, self).__init__(parent)
self.textQVBoxLayout = qg.QVBoxLayout()
self.titleLabel = qg.QLabel()
self.description = qg.QLabel()
self.textQVBoxLayout.addWidget(self.titleLabel)
self.textQVBoxLayout.addWidget(self.description)
self.setLayout(self.textQVBoxLayout)
def setTitle (self, text):
self.titleLabel.setText(text)
def setDescription (self, text):
self.description.setText(text)
class example_ui(qg.QDialog):
def __init__(self):
qg.QDialog.__init__(self)
self.myQListWidget = qg.QListWidget(self)
self.myQListWidget.currentItemChanged.connect(self.getTitleValue)
self.myQListWidget.setGeometry(qc.QRect(0,0,200,300))
# make instance customwidget item (just one)------
instance_1 = CustomQWidget()
instance_1.setTitle('First title')
instance_1.setDescription('this is a sample desc')
myQListWidgetItem = qg.QListWidgetItem(self.myQListWidget)
myQListWidgetItem.setSizeHint(instance_1.sizeHint())
self.myQListWidget.addItem(myQListWidgetItem)
self.myQListWidget.setItemWidget(myQListWidgetItem, instance_1)
def getTitleValue(self,val):
# i make assume something like below but didnt work
# print (self.myQListWidget.currentItem.titleLabel.text()
return 0
dialog = example_ui()
dialog.show()
now at getTitleValue function how do i get Title and desc value when i change selection ?
You should remember that the list items and corresponding widgets are not the same. Luckily, QListWidget tracks them and gives you access to the displayed widget if you provide the list item:
class example_ui(qg.QDialog):
def getTitleValue(self,val):
# parameter val is actually the same as self.myQListWidget.currentItem
selected_widget = self.myQListWidget.itemWidget(val)
print selected_widget.titleLabel.text()
return 0
Side note: I had to add a main loop in order for the app to be executed at all:
import sys # to give Qt access to parameters
# ... class definitions etc. ...
app = qg.QApplication(sys.argv)
dialog = example_ui()
dialog.show()
exec_status = app.exec_() # main loop

passing value into a different class

I have an array of data called msg.data[2] where i have used pubsub i now want to pass this data into the PlotCanvasExample Class
Here is where i call the class
self.pubsubText.SetLabel("This is the Contact Map for the chain "+msg.data[0]+" in the PDB file "+msg.data[1])
frame = self.GetParent()
sizer = wx.BoxSizer(wx.VERTICAL)
self.canvas = PlotCanvasExample(self,0, size=(100,100))
sizer.Add(self.canvas,1,wx.EXPAND,0)
Here is the class itself
class PlotCanvasExample(plot.PlotCanvas):
def __init__(self,parent,id,size):
plot.PlotCanvas.__init__(self,parent,id,style=wx.BORDER_NONE, size=(300,200))
self.data = [(1,2),(23,2)]
line = plot.PolyMarker(self.data)
gc = plot.PlotGraphics([line],"CM view", "x-axis","y axis")
self.Draw(gc, xAxis=(0,50), yAxis=(0,50))
How can i pass in the variable msg.data into this class
I am new to python so an explanation of how to do it would be nice so i can understand how to do it next time
you may add setData method to PlotCanvasExample class and use this method maybe?
def setData(data):
self.data = data
or if you need this parameter on object creation, you may add this parameter to init method of PlotCanvasExample.
class PlotCanvasExample(plot.PlotCanvas):
def __init__(self,parent,id,size, data):
class P...:
def __init__(self, parent, id, size, data):
self.data = data
...
Arguments which need to be passed to a class should be placed in the __init__ function.
Here is the Python documentation on Classes
the line of code def __init__(self,parent,id,size): is what you need to change to add the new parameter in.
When you create this class next time you can assign it in the self.canvas - PlotCanvasExample()
Example:
Class:
class PlotCanvasExample(plot.PlotCanvas):
def __init__(self,parent,id,size,data):
Code:
self.canvas = PlotCanvasExample(self,0, size=(100,100), msg.data[2])
If your new to Python I would suggest taking a look through the docs. Also take a look at PEP8 it will help when posting code on here.
:)
Try this code...
self.pubsubText.SetLabel("This is the Contact Map for the chain "+msg.data[0]+" in the PDB file "+msg.data[1])
frame = self.GetParent()
sizer = wx.BoxSizer(wx.VERTICAL)
self.canvas = PlotCanvasExample(self,0, size=(100,100), msg.data)
sizer.Add(self.canvas,1,wx.EXPAND,0)
class PlotCanvasExample(plot.PlotCanvas):
def __init__(self,parent,id,size, msgdata):
self.msgdata = msgdata
plot.PlotCanvas.__init__(self,parent,id,style=wx.BORDER_NONE, size=(300,200))
self.data = [(1,2),(23,2)]
line = plot.PolyMarker(self.data)
gc = plot.PlotGraphics([line],"CM view", "x-axis","y axis")
self.Draw(gc, xAxis=(0,50), yAxis=(0,50))
Hope this helps.
Andrew

Passing variable from one class instance to another using Python?

I'm having trouble passing a variable defined in one class instance to another class instance. I am relatively new to using classes, but it is my understanding that variables can be passed from one instance to another by simply defining as part of the class instance (such as in the following example). While I've used this model in the past I've never attempted to do this using a GUI framework such as wxPython.
class Foo(object):
def __init__(self, var):
self.var = var
class Bar(object):
def do_something(self, var):
print var*3
if __name__ == '__main__':
f = Foo(3)
b = Bar()
b.do_something(f.var)
The problem I'm having is that the wxPython instance seems to be predefined and will not accept any additional parameters (allowing me to only pass things like title, size, etc.) to the class instance.
The other issue I'm facing is I'm attempting to pass the variable three classes deep by calling a dialog window, and from the dialog I call a separate class designed to start a worker thread.
So my questions are:
How can I pass the variable from the first class instance to the third class instance?
How can I override the wxPython instance to allow for the definition of additional variables?
OR, Could it be possible to create a custom event handler to pass the necessary data?
To clarify...
I'm using Python and would like to think that I understand the fundamentals of programming using Classes and a GUI with frameworks such as Tkinter and wxPython (used in this project). I've written a main class/instance that gets some data from the user and I would like to be able to pass the information stored in self.main_instance_var and pass it along to a second class/instance (in this case a Progress Dialog window called from the first class.
When I attempted to use the above model in my Progress Dialog I got a very uninformative syntax error ('non-keyword arg after keyword arg'). Preventing me from further passing the variable from the Progress Dialog window on to the worker thread. If I had gotten an exception that would have been one thing but the syntax error I don't understand. Look below for a short example:
class ProgressDialog(wx.Dialog):
def __init__(self, parent, title, myVar): # Generates syntax error on this line
super(ProgressDialog, self).__init__(parent=parent,
title=title, size=(500, 110))
self.var = myVar
Basic Source (by request, sorry its so dirty):
import time
import os, sys, wx
from ftplib import FTP_TLS
from threading import Thread
from wx.lib.pubsub import Publisher
########################################################################
class FtpValues(object):
""" Returns a property attribute - called by FtpFileTransfer
Used to set values/variables for Host, USERID, PASSWD, FILE """
#----------------------------------------------------------------------
def __init__(self):
self.varList = None
#----------------------------------------------------------------------
def GetValues(self):
return self.varList
#----------------------------------------------------------------------
def SetValues(self, HOST, USERID, PASSWD, FILE):
self.varList = [HOST, USERID, PASSWD, FILE]
#----------------------------------------------------------------------
def DelValues(self):
del self.valList
Values = property(GetValues, SetValues, DelValues, "Set/Get FtpValues")
# http://docs.python.org/library/functions.html#property
########################################################################
class FtpFileTransfer(Thread):
"""Test Worker Thread Class."""
#----------------------------------------------------------------------
def __init__(self):
"""Init Worker Thread Class."""
Thread.__init__(self)
self.StartTransfer() # start the thread
#----------------------------------------------------------------------
def StartTransfer(self): # was named run - started automatically
"""Run Worker Thread.""" # when called by the start method
# This is the code executing in the new thread.
HOST, USERID, PASSWD, FILE = FtpValues.Values
BLOCKSIZE = 57344
try:
ftp = FTP_TLS(HOST)
ftp.login(USERID, PASSWD)
ftp.prot_p()
ftp.voidcmd("TYPE I")
f = open(FILE, 'rb')
datasock, esize = ftp.ntransfercmd(
'STOR %s' % os.path.basename(FILE))
size = os.stat(FILE)[6]
bytes_so_far = 0
while 1:
buf = f.read(BLOCKSIZE)
if not buf:
break
datasock.sendall(buf)
bytes_so_far += len(buf)
msg = [bytes_so_far, size]
Publisher().sendMessage("update", msg)
except: raise
finally:
try:
datasock.close()
f.close()
ftp.voidresp()
ftp.quit()
print 'Complete...'
except: pass
wx.CallAfter(Publisher().sendMessage, "update", "Database Transfer Complete!")
########################################################################
class ProgressDialog(wx.Dialog):
def __init__(self, parent, title):
super(ProgressDialog, self).__init__(parent=parent,
title=title, size=(500, 110))
self.displayLbl = wx.StaticText(self, -1, 'Verifying Database Files... ', (20, 20)) #Preparing for Transfer...
self.gauge = wx.Gauge(self, -1, 100, (20, 45), (370, 24))
self.btn = btn = wx.Button(self, -1, 'Cancel', (400, 45), (-1, 25))
btn.Bind(wx.EVT_BUTTON, self.OnClose)
# listens for response from worker thread
Publisher().subscribe(self.updateDisplay, "update")
FtpFileTransfer()#.StartTransfer(HOST, USERID, PASSWD, FILE) #Start the FTP Worker Thread
#self.OnStart()
#----------------------------------------------------------------------
def run(self):
FtpFileTransfer(HOST, USERID, PASSWD, FILE)
#----------------------------------------------------------------------
def OnClose(self, event):
""" Place Holder """
if self.btn.GetLabel() == 'Finish':
# Do Something!
pass
return None
#----------------------------------------------------------------------
def updateDisplay(self, msg):
""" Receives data from thread and updates the display """
if isinstance(msg.data, list):
bytes_so_far, size = msg.data
k = 100 * bytes_so_far / size
self.displayLbl.SetLabel("Sent %d of %d bytes %.1f%%" % (bytes_so_far, size, 100 * bytes_so_far / size))
self.gauge.SetValue(k)
else:
self.displayLbl.SetLabel("%s" % msg.data)
#self.btn.Enable()
self.btn.SetLabel('Finish')
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
self.displayLbl = wx.StaticText(panel, label="Amount of time since thread started goes here")
self.btn = btn = wx.Button(panel, label="Start Thread")
self.gauge = wx.Gauge(panel, -1, 100, size=(370, 24))
btn.Bind(wx.EVT_BUTTON, self.onButton)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.displayLbl, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(self.gauge, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
self.VarData()
# create a pubsub receiver
Publisher().subscribe(self.updateDisplay, "update")
#----------------------------------------------------------------------
def onButton(self, event):
"""
Runs the thread
"""
chgdep = ProgressDialog(None, title='File Transfer. . .')
chgdep.ShowModal()
#chgdep.Destroy()
#----------------------------------------------------------------------
def updateDisplay(self, msg):
"""
Receives data from thread and updates the display
"""
if isinstance(msg.data, list):
bytes_so_far, size = msg.data
k = 100 * bytes_so_far / size
self.displayLbl.SetLabel("Sent %d of %d bytes %.1f%%" % (bytes_so_far, size, 100 * bytes_so_far / size))
self.gauge.SetValue(k)
else:
self.displayLbl.SetLabel("%s" % msg.data)
self.btn.Enable()
#----------------------------------------------------------------------
def VarData(self):
HOST = '127.0.0.1'
USERID = 'test'
PASSWD = 'P#ssw0rd'
FILE = r'F:\Programming\temp\Test.zip'
varList = [HOST, USERID, PASSWD, FILE]
FtpValues.Values = HOST, USERID, PASSWD, FILE
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()
Personally, I like using wx.lib.pubsub to pass information between classes. I do it all the time in my applications. You can read about it here: http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/
If you need to post data from a thread, you will need to use a thread-safe method like wx.CallAfter, wx.CallLater or wx.PostEvent. You can combine these with pubsub by calling your pubsub publisher inside one of the thread-safe methods. I show how to do just that here: http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
There's also a good article on threads and wxPython on their wiki: http://wiki.wxpython.org/LongRunningTasks
The approaches suggested in the question are the preferred way of doing things in Python. Statements like b.do_something(f.var) and __init__(self, parent, title, myVar) are perfectly good ways to pass information between classes.
It's common when starting out, but my guess here is that you've taken a small syntax error somewhere and are thinking it's implying that you're taking the wrong general approach. Instead, your general approach looks fine, but you just need to figure out what's causing the specific error.
Comments on other answers:
1) Set/get functions work well too, but Python prefers the properties approach. (Personally, I still use set/get methods out of habit, but it's not as Pythonic.)
2) pubsub is great, but it's not a general tool for "passing information between classes", e.g., one wouldn't want to use pubsub for i = int("2"). Pubsub is more for cases where one has, for example, two wxFrames that need to communicate a bit of information. There's a reason it's a part of wxPython and not Python.
In OOP if you want to pass data into and out of an object you should define a set/get function in that class so that you can get data from that object as well as set data in that object. So in your case each instance of your object would call the respective get/set function to pass the data back and forth between your objects.
You may have worked through this months ago, but I just ran into the very same issue with a wxPython dialog, and got it to work by using informing a global within the function:
elusive_variable="" # declare outside class & function
class MyForm(wx.Frame):
def onOpenFile(self, event):
global elusive_variable
# ...other stuff here
elusive_variable="Oh hi Mark"
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
print elusive_variable # variable is liberated!!!
There might be some more enlightned way but this works...
Heres an example:
class One:
param1 = ['a', 'b']
class Two:
i = One()
print i.param1
Save the above code in .py file and run it . You should see the output
I guess this can be a simple way to exchange variables from one class to another

Is it is possible to "set" a list to a ComboBox, wxpython?

Hi i know its possible to do this with lists however is it possible to do this with Comboboxes? Is there anything similar to the set function?
I have tried using set with a Combo box but i receive the following error:
AttributeError: 'ComboBox' object has no attribute 'Set'
Thanks.
Well, you can call SetItems(myList) to put a list into a ComboBox, overwriting what's already in it.
EDIT: The most common method to create a list in a combobox's list is like this:
myList = ["dog", "cat", "hamster"]
cbo = wx.ComboBox(self, choices=myList)
But since ComboBox inherits from ItemContainer, you can also do it like this complete example:
import wx
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Test")
panel = wx.Panel(self)
myList = ["dog", "cat", "hamster"]
cbo = wx.ComboBox(panel)
cbo.SetItems(myList)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
http://www.wxpython.org/docs/api/wx.ComboBox-class.html
__init__(parent, id, value, pos, size, choices, style, validator, name)
combobox = wx.ComboBox(self, choices=myList)
I believe your are asking for a method to add new items "at runtime"? ie after the form is created? See the code below if so ;-)
def UpdateCitiesCombo(self):
self.cmbCities.Clear()
pc = PostalCode()
if self.txtPostalCode.Value:
cities = pc.GetFromCode(int(self.txtPostalCode.Value))
for city in cities:
self.cmbCities.Append(city[2])
items = self.cmbCities.GetItems()
index = -1
try:
if self.customer.city != "":
index = items.index(self.customer.city)
else:
index = 0
self.cmbCities.SetSelection(index)
except ValueError:
self.cmbCities.SetValue(self.customer.city)
In essence what you should not is the Clear() and Append() methods of the ComboBox and the fact that this function is called from an event somewhere. Hope it is what you are looking for.

wxPython ListCtrl Column Ignores Specific Fields

I'm rewriting this post to clarify some things and provide a full class definition for the Virtual List I'm having trouble with. The class is defined like so:
from wx import ListCtrl, LC_REPORT, LC_VIRTUAL, LC_HRULES, LC_VRULES, \
EVT_LIST_COL_CLICK, EVT_LIST_CACHE_HINT, EVT_LIST_COL_RIGHT_CLICK, \
ImageList, IMAGE_LIST_SMALL, Menu, MenuItem, NewId, ITEM_CHECK, Frame, \
EVT_MENU
class VirtualList(ListCtrl):
def __init__(self, parent, datasource = None,
style = LC_REPORT | LC_VIRTUAL | LC_HRULES | LC_VRULES):
ListCtrl.__init__(self, parent, style = style)
self.columns = []
self.il = ImageList(16, 16)
self.Bind(EVT_LIST_CACHE_HINT, self.CheckCache)
self.Bind(EVT_LIST_COL_CLICK, self.OnSort)
if datasource is not None:
self.datasource = datasource
self.Bind(EVT_LIST_COL_RIGHT_CLICK, self.ShowAvailableColumns)
self.datasource.list = self
self.Populate()
def SetDatasource(self, datasource):
self.datasource = datasource
def CheckCache(self, event):
self.datasource.UpdateCache(event.GetCacheFrom(), event.GetCacheTo())
def OnGetItemText(self, item, col):
return self.datasource.GetItem(item, self.columns[col])
def OnGetItemImage(self, item):
return self.datasource.GetImg(item)
def OnSort(self, event):
self.datasource.SortByColumn(self.columns[event.Column])
self.Refresh()
def UpdateCount(self):
self.SetItemCount(self.datasource.GetCount())
def Populate(self):
self.UpdateCount()
self.datasource.MakeImgList(self.il)
self.SetImageList(self.il, IMAGE_LIST_SMALL)
self.ShowColumns()
def ShowColumns(self):
for col, (text, visible) in enumerate(self.datasource.GetColumnHeaders()):
if visible:
self.columns.append(text)
self.InsertColumn(col, text, width = -2)
def Filter(self, filter):
self.datasource.Filter(filter)
self.UpdateCount()
self.Refresh()
def ShowAvailableColumns(self, evt):
colMenu = Menu()
self.id2item = {}
for idx, (text, visible) in enumerate(self.datasource.columns):
id = NewId()
self.id2item[id] = (idx, visible, text)
item = MenuItem(colMenu, id, text, kind = ITEM_CHECK)
colMenu.AppendItem(item)
EVT_MENU(colMenu, id, self.ColumnToggle)
item.Check(visible)
Frame(self, -1).PopupMenu(colMenu)
colMenu.Destroy()
def ColumnToggle(self, evt):
toggled = self.id2item[evt.GetId()]
if toggled[1]:
idx = self.columns.index(toggled[2])
self.datasource.columns[toggled[0]] = (self.datasource.columns[toggled[0]][0], False)
self.DeleteColumn(idx)
self.columns.pop(idx)
else:
self.datasource.columns[toggled[0]] = (self.datasource.columns[toggled[0]][0], True)
idx = self.datasource.GetColumnHeaders().index((toggled[2], True))
self.columns.insert(idx, toggled[2])
self.InsertColumn(idx, toggled[2], width = -2)
self.datasource.SaveColumns()
I've added functions that allow for Column Toggling which facilitate my description of the issue I'm encountering. On the 3rd instance of this class in my application the Column at Index 1 will not display String values. Integer values are displayed properly. If I add print statements to my OnGetItemText method the values show up in my console properly. This behavior is not present in the first two instances of this class, and my class does not contain any type checking code with respect to value display.
It was suggested by someone on the wxPython users' group that I create a standalone sample that demonstrates this issue if I can. I'm working on that, but have not yet had time to create a sample that does not rely on database access. Any suggestions or advice would be most appreciated. I'm tearing my hair out on this one.
Are you building on the wxPython demo code for virtual list controls? There are a couple of bookkeeping things you need to do, like set the ItemCount property.
One comment about your OnGetItemText method: Since there's no other return statement, it will return None if data is None, so your test has no effect.
How about return data or "" instead?
There's a problem with the native object in Windows. If GetImg returns None instead of -1 the list has a problem with column 1 for some reason. That from Robin over on the Google Group post for this issue.

Categories

Resources