简体   繁体   English

Wxpython将Menu event.GetLabel()中的下划线字符加倍

[英]Wxpython doubling up underscore characters in Menu event.GetLabel()

I load a menu with filenames from which the user will be able to choose from. 我加载了带有文件名的菜单,用户可以从中进行选择。
If the file name contains an underscore character (_), the 如果文件名包含下划线字符(_),则

event.GetEventObject().GetLabel(event.GetId())

returns a value where any underscore character (_) is doubled up. 返回一个值,其中任何下划线字符(_)都会加倍。
So a file name of a_file.txt becomes a__file.txt 因此,文件名a_file.txt变为a__file.txt
I can get around the problem by using 我可以通过使用解决问题

event.GetEventObject().MenuItems[event.GetId()].GetLabel()

but not only do I not know if there are any repercussions of using this but I don't particularly want to trawl through 1000's of lines of code hunting for instances of this strange issue. 但是,我不仅不知道使用此功能是否有任何影响,而且我不想特别针对此奇怪问题的实例遍历1000行代码搜寻。
Does anyone have an explanation for this behaviour and how to avoid it? 有人对此行为有解释,如何避免?

The demonstration code below illustrates the problem and the work around. 下面的演示代码说明了问题和解决方法。
The tests are for a normal file name, a file name with spaces and a file name with an 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 a list of things via another function or just a declaration
        self.list_of_things = ["afilename.txt", "another filename.txt", "problem_filename.txt"]
        list_used = wx.Menu()
        thing_count = 0

        for 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 to see the doubling of underscore')

        self.menubar.Append(menu1, "&File")
        self.SetMenuBar(self.menubar)
    # Create bindings for the Thing list
        i_count = 0
        for i 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 get the label in another way"
        print "Label returned:", obj.MenuItems[id_selected].GetLabel()
        print "From the range:"
        for i 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
        for i 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: 编辑:
It would seem that this is a bug in wxpython 2.8 (on Linux, perhaps other platforms) as the problem does not manifest itself using wxpython 3.0 on Windows. 看来这是wxpython 2.8(在Linux,也许是其他平台上)中的错误,因为在Windows上使用wxpython 3.0并不能解决问题。 Information courtesy of @pss who tested the code for me. 信息由@pss提供,他为我测试了代码。
The result being that I have indeed trawled through my code and used the work-around as detailed above. 结果是我确实已经遍历了我的代码并使用了如上所述的变通方法。

Further to my original edit to the question, the fault seems to be a very old one as discussed on trac.wxwidgets.org ticket numbers #338, #9062, and #9055 and is related to the underscore (_) character being used as the escape character for GTK+. 继我对问题的原始编辑之后,该错误似乎是一个非常古老的错误,如在trac.wxwidgets.org票证编号#338,#9062和#9055上所讨论的,并且与下划线(_)字符有关GTK +的转义字符。 This form of error might exist on Windows as well but the errant character on Windows would not be the underscore but an ampersand (&) ie under Windows a file name such as a&filename.txt could be returned as afilename.txt as it would drop the ampersand completely (I'm not 100% sure of this see the code) 这种错误形式也可能在Windows上存在,但Windows上的错误字符不是下划线,而是与号(&),即在Windows下,文件名(例如a&filename.txt)可以作为afilename.txt返回,因为它将删除完全与符号(我不确定百分百看代码)
http://trac.wxwidgets.org/changeset/9055/svn-wx http://trac.wxwidgets.org/changeset/9055/svn-wx

So as @pss tested on the Windows platform, the issue may not be a simple bug that has been fixed but the result of a issue between wxpython/wxwidgets and the graphical platform that is being used. 因此,正如在Windows平台上测试@pss一样,该问题可能不是已修复的简单错误,而是wxpython / wxwidgets与所使用的图形平台之间的问题的结果。
One work-around should you come across this problem was detailed in the original question and works: 原始问题和工作中详细介绍了一种应解决此问题的解决方法:

event.GetEventObject().MenuItems[event.GetId()].GetLabel()

or as stated in the code block more long-windedly: 或者更长时间地在代码块中声明:

id_selected = event.GetId()
obj = event.GetEventObject()
print "Label returned:", obj.MenuItems[id_selected].GetLabel()     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM