Flatmenu中的密钥' 2'(keycode = 50)是否有任何快捷方式?

时间:2014-04-22 09:33:59

标签: wxpython

我正在尝试开发显示FlatMenu,工具栏和以下面板的应用程序。在工具栏中我放了一个textctrl。当我想要输入密钥' 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()

1 个答案:

答案 0 :(得分:0)

您发布的代码在我的系统上完全没有效果,因为您尝试在没有任何孩子的面板上使用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'和'键入'。

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 Stream\tCtrl+S", "Start Stream", wx.ITEM_NORMAL)
        f.AppendItem(item)

        item = FM.FlatMenuItem(f, wx.ID_ANY, "&Stop Stream\tCtrl+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()