python - Wxpython doubling up underscore characters in Menu event.GetLabel() -
i load menu filenames user able choose from.
if file name contains underscore character (_),
event.geteventobject().getlabel(event.getid())
returns value underscore character (_) doubled up.
file name of a_file.txt
becomes a__file.txt
can around problem using
event.geteventobject().menuitems[event.getid()].getlabel()
but not not know if there repercussions of using don't particularly want trawl through 1000's of lines of code hunting instances of strange issue.
have explanation behaviour , how avoid it?
the demonstration code below illustrates problem , work around.
tests normal file name, file name spaces , file name underscore.
import wx class menuproblem(wx.frame): def __init__(self, *args, **kwds): self.frame=wx.frame.__init__(self, *args, **kwds) self.menubar = wx.menubar() # self.statusbar = wx.statusbar(self-1) self.createstatusbar() self.setstatustext("demonstration of wxpython") menu1 = wx.menu() menu_item_1 = menu1.append(wx.id_open, "&file") menu_item_2 = menu1.append(wx.id_exit, "&exit...") #build list of things via function or declaration self.list_of_things = ["afilename.txt", "another filename.txt", "problem_filename.txt"] list_used = wx.menu() thing_count = 0 thing in self.list_of_things: t1 = wx.menuitem(list_used, thing_count, thing) list_used.appenditem(t1) thing_count +=1 thing_end = wx.menuitem(list_used,199,'end of list') list_used.appenditem(thing_end) menu1.appendmenu(wx.id_file,'&problem demo',list_used) menu1.sethelpstring(wx.id_file, 'click problem_filename.txt see doubling of underscore') self.menubar.append(menu1, "&file") self.setmenubar(self.menubar) # create bindings thing list i_count = 0 in self.list_of_things: self.bind(wx.evt_menu, self.onthingopen, id=i_count) i_count = i_count + 1 self.bind(wx.evt_menu, self.onthingend, id=199) self.bind(wx.evt_menu, self.onclose, id=wx.id_exit) self.show(true) def onthingopen(self, event): id_selected = event.getid() obj = event.geteventobject() print "option :", id_selected print "label returned:", obj.getlabel(id_selected) print "now label in way" print "label returned:", obj.menuitems[id_selected].getlabel() print "from range:" in range(obj.menuitemcount): print "\t\t", obj.menuitems[i].getlabel() print "."*50 def onthingend(self, event): id_selected = event.getid() obj = event.geteventobject() print "option :", id_selected print "label returned :",obj.getlabel(id_selected) print "enabled", obj.isenabled(id_selected) print obj.menuitemcount in range(obj.menuitemcount): print obj.menuitems[i].getlabel() def onclose(self, event): self.close() if __name__ == '__main__': app = wx.app() mc=menuproblem(parent=none, id=-1) app.mainloop()
edit:
seem bug in wxpython 2.8 (on linux, perhaps other platforms) problem not manifest using wxpython 3.0 on windows. information courtesy of @pss tested code me.
result being have indeed trawled through code , used work-around detailed above.
further original edit question, fault seems old 1 discussed on trac.wxwidgets.org ticket numbers #338, #9062, , #9055 , related underscore (_) character being used escape character gtk+. form of error might exist on windows errant character on windows not underscore ampersand (&) i.e. under windows file name such a&filename.txt returned afilename.txt drop ampersand (i'm not 100% sure of see code)
http://trac.wxwidgets.org/changeset/9055/svn-wx
so @pss tested on windows platform, issue may not simple bug has been fixed result of issue between wxpython/wxwidgets , graphical platform being used.
1 work-around should come across problem detailed in original question , works:
event.geteventobject().menuitems[event.getid()].getlabel()
or stated in code block more long-windedly:
id_selected = event.getid() obj = event.geteventobject() print "label returned:", obj.menuitems[id_selected].getlabel()
Comments
Post a Comment