04_wxPython之【菜单】

菜单

# -*- coding: utf-8 -*-
import wx

'''
菜单事件
'''

class MyApp(wx.App):

    def OnInit(self):
        frame = wx.Frame(parent=None, title='菜单事件')
        menu_bar = wx.MenuBar()  # 生成一个菜单条
        menuFile = wx.Menu()  # 生成一个菜单的下拉列表
        menu_open = menuFile.Append(-1, 'open')  # 在菜单列表中,增加一个条目
        menu_save = menuFile.Append(-1, 'save')  # 在菜单列表中,增加一个条目
        menu_exit = menuFile.Append(-1, 'exit')  # 在菜单列表中,增加一个条目

        menuEdit = wx.Menu()  # 生成一个菜单的下拉列表
        menu_copy = menuEdit.Append(-1, '复制')  # 在菜单列表中,增加一个条目
        menu_del = menuEdit.Append(-1, '删除')  # 在菜单列表中,增加一个条目
        menu_cut = menuEdit.Append(-1, '剪切')  # 在菜单列表中,增加一个条目

        menu_bar.Append(menuFile, "&file")  # 将菜单增加到菜单列表中
        menu_bar.Append(menuEdit, "&edit")  # 将菜单增加到菜单列表中

        frame.SetMenuBar(menu_bar)  # 将菜单条,增加到 frame 中

        self.Bind(wx.EVT_MENU, self.open_event, menu_open)  # 绑定一个事件,到设置好的menu上

        frame.Show()
        frame.Center()
        return True

    def open_event(self, event):  # 事件函数
        print('点击了open菜单')

app = MyApp()
app.MainLoop()
2225162-87ed4dad94eb4e2a.png
image.png

红色框:MenuBar
绿色框:Menu
蓝色框:menu item

右键菜单

# -*- coding: utf-8 -*-
import wx

'''
右键菜单
'''

class MyApp(wx.App):

    def OnInit(self):
        frame = wx.Frame(parent=None, title='右键菜单')
        panel = wx.Panel(frame, -1)  # 生成一个面板

        menuEdit = wx.Menu()  # 生成一个菜单的下拉列表
        menu_copy = menuEdit.Append(-1, '复制')  # 在菜单列表中,增加一个条目
        menu_del = menuEdit.Append(-1, '删除')  # 在菜单列表中,增加一个条目
        menu_cut = menuEdit.Append(-1, '剪切')  # 在菜单列表中,增加一个条目

        self.Bind(wx.EVT_RIGHT_DOWN, self.right_click)  # 绑定一个右键点击事件

        # 设置成员变量
        self.menuEdit = menuEdit
        self.panel = panel
        frame.Show()
        frame.Center()
        return True

    def right_click(self, event):  # 右键点击事件
        # event.x  和  event.y  代表点击的坐标点
        print('右键点击了,X坐标是:', event.x, 'Y坐标是:', event.y)
        pos = (event.GetX(),event.GetY())
        self.panel.PopupMenu(self.menuEdit,pos)

app = MyApp()
app.MainLoop()
2225162-6807619dddf92652.png
image.png

猜你喜欢

转载自blog.csdn.net/weixin_33744141/article/details/90936307