Wxpython 在菜单 event.GetLabel() 中将下划线字符加倍
Wxpython doubling up underscore characters in Menu event.GetLabel()
我加载了一个包含文件名的菜单,用户可以从中进行选择。
如果文件名包含下划线字符 (_),则
event.GetEventObject().GetLabel(event.GetId())
returns 任何下划线字符 (_) 加倍的值。
所以 a_file.txt
的文件名变成 a__file.txt
我可以通过使用
来解决这个问题
event.GetEventObject().MenuItems[event.GetId()].GetLabel()
但我不仅不知道使用它是否有任何影响,而且
我不是特别想搜索 1000 行代码
对于这个奇怪问题的实例。
有没有人解释这种行为以及如何避免这种行为?
下面的演示代码说明了问题和解决方法。
测试是针对普通文件名,带空格的文件名和文件名
带下划线。
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()
编辑:
这似乎是 wxpython 2.8(在 Linux,可能是其他平台上)中的一个错误,因为在 Windows 上使用 wxpython 3.0 时问题不会自行显现。信息由@pss 提供,他为我测试了代码。
结果是我确实浏览了我的代码并使用了上面详述的解决方法。
除了我对问题的原始编辑之外,故障似乎是一个非常古老的故障,正如在 trac.wxwidgets.org 票号 #338、#9062 和 #9055 中所讨论的,并且与下划线 (_ ) 字符用作 GTK+ 的转义字符。这种形式的错误也可能存在于 Windows 上,但 Windows 上的错误字符不会是下划线而是符号 (&),即 Windows 下的文件名,例如 a&filename.txt 可以作为 filename.txt 返回,因为它会完全删除 & 符号(我不是 100% 确定这一点,请参阅代码)
http://trac.wxwidgets.org/changeset/9055/svn-wx
因此,@pss 在 Windows 平台上进行了测试,问题可能不是一个已修复的简单错误,而是 wxpython/wxwidgets 和正在处理的图形平台之间的问题的结果用过。
如果您遇到此问题,一种解决方法在原始问题中有详细说明并且有效:
event.GetEventObject().MenuItems[event.GetId()].GetLabel()
或者如代码块中所述更冗长:
id_selected = event.GetId()
obj = event.GetEventObject()
print "Label returned:", obj.MenuItems[id_selected].GetLabel()
我加载了一个包含文件名的菜单,用户可以从中进行选择。
如果文件名包含下划线字符 (_),则
event.GetEventObject().GetLabel(event.GetId())
returns 任何下划线字符 (_) 加倍的值。
所以 a_file.txt
的文件名变成 a__file.txt
我可以通过使用
event.GetEventObject().MenuItems[event.GetId()].GetLabel()
但我不仅不知道使用它是否有任何影响,而且
我不是特别想搜索 1000 行代码
对于这个奇怪问题的实例。
有没有人解释这种行为以及如何避免这种行为?
下面的演示代码说明了问题和解决方法。
测试是针对普通文件名,带空格的文件名和文件名
带下划线。
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()
编辑:
这似乎是 wxpython 2.8(在 Linux,可能是其他平台上)中的一个错误,因为在 Windows 上使用 wxpython 3.0 时问题不会自行显现。信息由@pss 提供,他为我测试了代码。
结果是我确实浏览了我的代码并使用了上面详述的解决方法。
除了我对问题的原始编辑之外,故障似乎是一个非常古老的故障,正如在 trac.wxwidgets.org 票号 #338、#9062 和 #9055 中所讨论的,并且与下划线 (_ ) 字符用作 GTK+ 的转义字符。这种形式的错误也可能存在于 Windows 上,但 Windows 上的错误字符不会是下划线而是符号 (&),即 Windows 下的文件名,例如 a&filename.txt 可以作为 filename.txt 返回,因为它会完全删除 & 符号(我不是 100% 确定这一点,请参阅代码)
http://trac.wxwidgets.org/changeset/9055/svn-wx
因此,@pss 在 Windows 平台上进行了测试,问题可能不是一个已修复的简单错误,而是 wxpython/wxwidgets 和正在处理的图形平台之间的问题的结果用过。
如果您遇到此问题,一种解决方法在原始问题中有详细说明并且有效:
event.GetEventObject().MenuItems[event.GetId()].GetLabel()
或者如代码块中所述更冗长:
id_selected = event.GetId()
obj = event.GetEventObject()
print "Label returned:", obj.MenuItems[id_selected].GetLabel()