尝试使用wxpython在菜单栏中设置菜单项的功能

时间:2017-11-08 07:37:14

标签: python-3.x wxpython

这是我的代码,用于在功能上设置菜单栏中的菜单项。但我不知道为什么,但它只是不工作,我的意思是当我在功能上设置打开它工作,但当我设置其他它没有,也没有它采取历史菜单和帮助菜单。目前我已经评论了历史记录和帮助代码但是当我删除逗号时它会给出错误。 (我对python的关注不是很好,我刚刚开始,所以需要一些帮助,以便找到我错的地方。)

import wx

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MenuFrame(None, title="Menus and MenuBars")
        self.SetTopWindow(self.frame)
        self.frame.Show()

        return True

ID_READ_ONLY = wx.NewId()

class MenuFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(MenuFrame, self).__init__(*args, **kwargs)

        # Attributes
        self.panel = wx.Panel(self)
        self.txtctrl = wx.TextCtrl(self.panel,
                                   style=wx.TE_MULTILINE)

        # Layout
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.txtctrl, 1, wx.EXPAND)
        self.panel.SetSizer(sizer)
        self.CreateStatusBar() # For output display

        # Setup the Menu
        menub = wx.MenuBar()

        # File Menu
        filem = wx.Menu()
        filem.Append(wx.ID_NEW, "New\tCtrl+N")
        filem.Append(wx.ID_OPEN, "Open\tCtrl+O")
        filem.Append(wx.ID_SAVE, "Save\tCtrl+S")
        filem.Append(wx.ID_SAVEAS, "Save_As\tCtrl+Shift+S")
        menub.Append(filem, "&File")

        # Edit Menu
        editm = wx.Menu()
        editm.Append(wx.ID_UNDO, "Undo\tCtrl+Z")
        editm.Append(wx.ID_REDO, "Redo\tCtrl+Shift+Z")
        editm.Append(wx.ID_COPY, "Copy\tCtrl+C")
        editm.Append(wx.ID_CUT, "Cut\tCtrl+X")
        editm.Append(wx.ID_PASTE, "Paste\tCtrl+V")
        editm.Append(wx.ID_SELECTALL, "SelectAll\tCtrl+A")
        editm.AppendSeparator()
        editm.Append(ID_READ_ONLY, "Read Only",
                     kind=wx.ITEM_CHECK)
        menub.Append(editm, "E&dit")

        """# History Menu
        historym = wx.Menu()
        historym.Append(wx.ID_RECENT, "Recent\tCtrl+N")
        menub.Append(historym, "&History")

        # Help Menu
        helpm = wx.Menu()
        helpm.Append(wx.ID_HINT, "Hint")
        helpm.Append(wx.ID_ABOUT, "About")
        menub.Append(helpm, "&Help")"""

        self.SetMenuBar(menub)

        # Event Handlers
        self.Bind(wx.EVT_MENU, self.OnMenu)

    def OnMenu(self, event):
        """Handle menu clicks"""
        evt_id = event.GetId()
        actions = { wx.ID_COPY  : self.txtctrl.Copy,
                    wx.ID_CUT   : self.txtctrl.Cut,
                    wx.ID_PASTE : self.txtctrl.Paste }
        action = actions.get(evt_id, None)
        if action:
            action()
        elif evt_id == ID_READ_ONLY:
            # Toggle enabled state
            self.txtctrl.Enable(not self.txtctrl.Enabled)
        elif evt_id == wx.ID_OPEN:
            dlg = wx.FileDialog(self, "Open File", style=wx.FD_OPEN)
            if dlg.ShowModal() == wx.ID_OK:
                fname = dlg.GetPath()
                handle = open(fname, 'r')
                self.txtctrl.SetValue(handle.read())
                handle.close()
        else:
            event.Skip()


if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()

1 个答案:

答案 0 :(得分:0)

historyhelp菜单的问题在于,您使用的ID不存在,即ID_RECENTID_HINT,因此请使用其他内容或分配你自己的Id 未分配selectall功能 请注意,cutcopy函数要求您选择了某些内容 您尚未为SaveSaveas函数编写任何代码 undoredo功能似乎无法在标准wx.TextCtrl中发挥作用,因此您可以使用StyledTextCtrl代替。 希望下面的代码能让您感动一点 (这是针对wxpython Phoenix和Linux上的python3)

import wx
import wx.stc
class MyApp(wx.App):
    def OnInit(self):
        self.frame = MenuFrame(None, title="Menus and MenuBars")
        self.SetTopWindow(self.frame)
        self.frame.Show()

        return True

ID_READ_ONLY = wx.NewId()

class MenuFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(MenuFrame, self).__init__(*args, **kwargs)

        # Attributes
        self.panel = wx.Panel(self)
        self.txtctrl = wx.stc.StyledTextCtrl(self.panel,
                                   style=wx.TE_MULTILINE)

        # Layout
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.txtctrl, 1, wx.EXPAND)
        self.panel.SetSizer(sizer)
        self.CreateStatusBar() # For output display

        # Setup the Menu
        menub = wx.MenuBar()

        # File Menu
        filem = wx.Menu()
        filem.Append(wx.ID_NEW, "New\tCtrl+N")
        filem.Append(wx.ID_OPEN, "Open\tCtrl+O")
        filem.Append(wx.ID_SAVE, "Save\tCtrl+S")
        filem.Append(wx.ID_SAVEAS, "Save_As\tCtrl+Shift+S")
        menub.Append(filem, "&File")

        # Edit Menu
        editm = wx.Menu()
        editm.Append(wx.ID_UNDO, "Undo\tCtrl+Z")
        editm.Append(wx.ID_REDO, "Redo\tCtrl+Shift+Z")
        editm.Append(wx.ID_COPY, "Copy\tCtrl+C")
        editm.Append(wx.ID_CUT, "Cut\tCtrl+X")
        editm.Append(wx.ID_PASTE, "Paste\tCtrl+V")
        editm.Append(wx.ID_SELECTALL, "SelectAll\tCtrl+A")
        editm.AppendSeparator()
        editm.Append(ID_READ_ONLY, "Read Only",
                     kind=wx.ITEM_CHECK)
        menub.Append(editm, "E&dit")

        # History Menu
        historym = wx.Menu()
        historym.Append(wx.ID_PREVIEW, "Recent\tCtrl+N")
        menub.Append(historym, "&History")

        # Help Menu
        helpm = wx.Menu()
        helpm.Append(wx.ID_HELP_INDEX, "Hint")
        helpm.Append(wx.ID_ABOUT, "About")
        menub.Append(helpm, "&Help")

        self.SetMenuBar(menub)

        # Event Handlers
        self.Bind(wx.EVT_MENU, self.OnMenu)

    def OnMenu(self, event):
        """Handle menu clicks"""
        evt_id = event.GetId()
        actions = { wx.ID_COPY  : self.txtctrl.Copy,
                    wx.ID_CUT   : self.txtctrl.Cut,
                    wx.ID_PASTE : self.txtctrl.Paste,
                    wx.ID_UNDO : self.txtctrl.Undo,
                    wx.ID_REDO : self.txtctrl.Redo,
                    wx.ID_SELECTALL : self.txtctrl.SelectAll}
        action = actions.get(evt_id, None)
        if action:
            action()
        elif evt_id == ID_READ_ONLY:
            # Toggle enabled state
            self.txtctrl.Enable(not self.txtctrl.Enabled)
        elif evt_id == wx.ID_OPEN:
            dlg = wx.FileDialog(self, "Open File", style=wx.FD_OPEN)
            if dlg.ShowModal() == wx.ID_OK:
                fname = dlg.GetPath()
                handle = open(fname, 'r')
                self.txtctrl.SetValue(handle.read())
                handle.close()
        else:
            event.Skip()


if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()