平面菜单中是否有与键"2"(键码= 50)关联的快捷方式?



我正在尝试开发显示FlatMenu、工具栏和以下面板的应用程序。在工具栏上我放了一个文本控件。当我试图输入键"2"时,它没有被键入。我试着调试代码,它在日志中打印密钥代码,但在textctrl中它没有被键入。除了‘2’,我可以输入所有其他键。

代码

import wx
import os
import sys
import webbrowser
import wx.lib.scrolledpanel as scrolled
import wx.lib.agw.flatmenu as FM
wx.Log.SetLogLevel(0)

class MainPanel(wx.Panel):
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent,style=wx.TAB_TRAVERSAL)                       
class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "FlatMenu Demo")
        self.toolbar = self.CreateToolBar()
        self.textctrl = wx.TextCtrl( self.toolbar, wx.ID_ANY, value='',size=(100, -1))
        self.textctrl.Bind(wx.EVT_KEY_UP, self.HandleKeyPress)
        self.createToolbarItem(0,1,"connect", "new_icons/connect.png", self.stopLiveUpdate)
        self.toolbar.DoInsertTool(1,2, '', wx.Bitmap('new_icons/install_app.png'))
        self.toolbar.DoInsertTool(2,3, '', wx.Bitmap('new_icons/uninstall.png'))
        self.toolbar.InsertControl(3,self.textctrl)
        self.toolbar.Realize()
        self.CreateMenu()
        self.panel = MainPanel(self)
        self.PanelSizer = wx.BoxSizer(wx.VERTICAL)
        self.PanelSizer.Add( self.menubar, 0,wx.ALL | wx.ALIGN_LEFT | wx.EXPAND )
        self.PanelSizer.Add( self.toolbar, 0, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND )
        self.PanelSizer.Add( self.panel, 1, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND)
        self.SetSizer(self.PanelSizer)
        self.Layout()
    def HandleKeyPress(self,e):
        keycode = e.GetKeyCode()
        print keycode
        if keycode==50:
            print 'entered'
            e.Skip()            
        else:
            print 'not entered'
            return  
        e.Skip()
    def CreateMenu(self):
        self.menubar = FM.FlatMenuBar(self,-1)
        file = FM.FlatMenu()
        conn = FM.FlatMenu()
        help = FM.FlatMenu()
        app  = FM.FlatMenu()
        log = FM.FlatMenu()
        file.Append(1,"&Start Stream")
        file.Append(2,"&Stop stream")
        type = FM.FlatMenu()
        self.type1 = FM.FlatMenuItem(type, 3, "Type1", wx.ITEM_CHECK)
        type.AppendItem(self.type1)
        self.type2 = FM.FlatMenuItem(type, 4, "Type2", wx.ITEM_CHECK)
        type.AppendItem(self.type2)
        file.AppendMenu(5, "Emulator &Tuner-Type",type)
        self.quit = file.Append(6, "&Exit tAlt+F4", "Quit the Application")
        conn.Append(7,"&Connect")
        conn.Append(8,"&Disconnect")
        conn.Append(9,"&Power Off")
        conn.Append(10,"&Shut Down")
        app.Append(11,"&Install App")
        app.Append(12,"&Transfer Files")
        app.Append(13,"&Run App")
        app.Append(14,"&Kill App")
        app.Append(15,"&Update App")
        app.Append(16,"&Uninstall App")
        log.Append(17,"&Save Log")
        log.Append(18,"&Clear Log")
        log.Append(19,"&Refresh Log")
        help.Append(20,"&Home")
        help.Append(21,"&Google")
        help.Append(22,"&User Manual     tF1")
        help.Append( 23, "&About")
        self.menubar.Append(file, "&Start")
        self.menubar.Append(conn, "&Connection")
        self.menubar.Append(app, "&App")
        self.menubar.Append(log, "&Log")
        self.menubar.Append(help, "&Help")
        self.Bind(FM.EVT_FLAT_MENU_SELECTED, self.OnQuit, id=5)
        self.Bind(FM.EVT_FLAT_MENU_SELECTED,self.launchGoogle,id=21)
    def OnQuit(self,e=None):
        self.Destroy()
    def launchGoogle(self,event=None):
        webbrowser.open('http://google.com')
    def startLiveUpdate(self, event):
        self.createToolbarItem(0,1,"connect", "new_icons/connect.png", self.stopLiveUpdate)
    def stopLiveUpdate(self, event):
        self.createToolbarItem(0,1,"disconnect", "new_icons/disconnected.png", self.startLiveUpdate)

    def createToolbarItem(self,pos,id,label,imageName,method=None):
        self.toolbar.RemoveTool(id)
        self.pos = self.toolbar.GetToolsCount()
        self.toolbar.DoInsertTool(pos,id, label, wx.Bitmap(imageName))
        self.toolbar.Realize()
        self.Bind(wx.EVT_TOOL, method, id=id)

app = wx.App(0)
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()

您发布的代码在我的系统上根本无法正常工作,这是因为您试图在没有任何子级的面板上使用sizer。我对代码进行了一些修改,使其看起来更好(Windows 7,wxPython 2.9.4),然后将问题追溯到如何设置FlatMenu。你知道有一个FlatMenu.py演示吗?

https://github.com/crankycoder/wxPython-2.9.2.4/blob/master/wxPython/demo/agw/FlatMenu.py

从中得到一些想法,我只在FlatMenu中添加了几个条目,它适用于数字2,所以我假设你使用了一个特殊的ID(通常建议使用wx.ID_ANY,而不是硬编码的ID,因为有些ID有特殊效果)。请检查注释掉的代码,看看你将如何继续修改它以使其工作。

另一个可能最终导致问题的大问题是,你过度使用了"file"one_answers"type"等保留字。

import wxversion
import wx
import os
import sys
import webbrowser
import wx.lib.scrolledpanel as scrolled
import wx.lib.agw.flatmenu as FM
wx.Log.SetLogLevel(0)

class MainPanel(wx.Panel):
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent,style=wx.TAB_TRAVERSAL)                       
class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "FlatMenu Demo")
        self.panel = MainPanel(self)
        self.PanelSizer = wx.BoxSizer(wx.VERTICAL)
        #self.toolbar = self.CreateToolBar(style=wx.TB_FLAT|wx.WANTS_CHARS)
        self.toolbar = wx.ToolBar(self.panel, style=wx.TB_FLAT)
        self.textctrl = wx.TextCtrl( self.toolbar, wx.ID_ANY, value='',size=(100, -1), style=wx.WANTS_CHARS)
        self.textctrl.Bind(wx.EVT_CHAR, self.HandleKeyPress)
        self.toolbar.DoInsertTool(0,1,"connect", wx.Bitmap("gear_blue.png"))
        self.toolbar.DoInsertTool(1,2, '', wx.Bitmap('gear_blue.png'))
        self.toolbar.DoInsertTool(2,3, '', wx.Bitmap('tools.png'))
        t = self.toolbar.InsertControl(3,self.textctrl)
        self.toolbar.Realize()
        self.CreateMenu()
        self.PanelSizer.Add( self.menubar, 0, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND )
        self.PanelSizer.Add( self.toolbar, 0, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND )
        #self.PanelSizer.Add( self.panel, 1, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND)
        self.panel.SetSizer(self.PanelSizer)
        self.panel.Layout()
    def HandleKeyPress(self,e):
        keycode = e.GetKeyCode()
        print keycode
        if keycode==50:
            print 'entered'
            #return
            #e.Skip()            
        else:
            print 'not entered'
            #return  
        e.Skip()
    def CreateMenu(self):
        self.menubar = FM.FlatMenuBar(self.panel,-1)
        f = FM.FlatMenu()
        conn = FM.FlatMenu()
        h = FM.FlatMenu()
        app  = FM.FlatMenu()
        log = FM.FlatMenu()

        item = FM.FlatMenuItem(f, wx.ID_ANY, "&Start StreamtCtrl+S", "Start Stream", wx.ITEM_NORMAL)
        f.AppendItem(item)
        item = FM.FlatMenuItem(f, wx.ID_ANY, "&Stop StreamtCtrl+S", "Stop Stream", wx.ITEM_NORMAL)
        f.AppendItem(item)
        """
        #f.Append(2,"&Stop stream")
        t = FM.FlatMenu()
        self.type1 = FM.FlatMenuItem(t, 3, "Type1", wx.ITEM_CHECK)
        t.AppendItem(self.type1)
        self.type2 = FM.FlatMenuItem(t, 4, "Type2", wx.ITEM_CHECK)
        t.AppendItem(self.type2)
        f.AppendMenu(5, "Emulator &Tuner-Type",t)
        self.quit = f.Append(6, "&Exit tAlt+F4", "Quit the Application")
        conn.Append(7,"&Connect")
        conn.Append(8,"&Disconnect")
        conn.Append(9,"&Power Off")
        conn.Append(10,"&Shut Down")
        app.Append(11,"&Install App")
        app.Append(12,"&Transfer Files")
        app.Append(13,"&Run App")
        app.Append(14,"&Kill App")
        app.Append(15,"&Update App")
        app.Append(16,"&Uninstall App")
        log.Append(17,"&Save Log")
        log.Append(18,"&Clear Log")
        log.Append(19,"&Refresh Log")
        h.Append(20,"&Home")
        h.Append(21,"&Google")
        h.Append(22,"&User Manual     tF1")
        h.Append( 23, "&About")
        """
        self.menubar.Append(f, "&Start")
        self.menubar.Append(conn, "&Connection")
        self.menubar.Append(app, "&App")
        self.menubar.Append(log, "&Log")
        self.menubar.Append(h, "&Help")
        #self.Bind(FM.EVT_FLAT_MENU_SELECTED, self.OnQuit, id=5)
        #self.Bind(FM.EVT_FLAT_MENU_SELECTED,self.launchGoogle,id=21)
    def OnQuit(self,e=None):
        self.Destroy()
    def launchGoogle(self,event=None):
        webbrowser.open('http://google.com')
    def startLiveUpdate(self, event):
        self.createToolbarItem(0,1,"connect", "gear_blue.png", self.stopLiveUpdate)
    def stopLiveUpdate(self, event):
        self.createToolbarItem(0,1,"disconnect", "tools.png", self.startLiveUpdate)

    def createToolbarItem(self,pos,id,label,imageName,method=None):
        self.toolbar.RemoveTool(id)
        self.pos = self.toolbar.GetToolsCount()
        self.toolbar.DoInsertTool(pos,id, label, wx.Bitmap(imageName))
        self.toolbar.Realize()
        self.Bind(wx.EVT_TOOL, method, id=id)

app = wx.App(0)
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()

最新更新