1

在我的 python 应用程序中,我在对象列表视图中显示了对象。我想使用上下文菜单在列表中上下移动对象。

有什么方法可以识别鼠标右键点击的对象(这不一定是选中的对象)?

import wx
from ObjectListView import (ObjectListView, ColumnDefn)

class MainFrame(wx.Frame):
    MIN_SIZE = (500, 400)
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(None, *args, **kwargs)
        self.panel = MainPanel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel)
        self.SetSizer(sizer)
        self.Center()
        self.SetMinSize(self.MIN_SIZE)
        self.Show()
        widgets = [Widget('a'), Widget('b'),Widget('c')]
        self.panel.olv_widgets.SetObjects(widgets)

    def on_olv_widgets_right_down(self,event):
        self.PopupMenu(UpDownMenu(self), event.GetPosition())
        print(event.GetPosition())

    def on_cell_select(self, event):
        print('on_cell_select')

    def move_item_down(self, event):
        print('move_item_down')

    def move_item_up(self, event):
        print('move_item_up')

class MainPanel(wx.Panel):
    LIST_SIZE = (2000, 2000)
    def __init__(self, parent, *args, **kwargs):
        super(MainPanel, self).__init__(parent, *args, **kwargs)

        self.olv_widgets = ObjectListView(self, wx.ID_ANY,
                                            style=wx.LC_REPORT|wx.SUNKEN_BORDER,
                                            size=self.LIST_SIZE)
        self.olv_widgets.SetColumns(
                [ColumnDefn('Widget', 'left', parent.MIN_SIZE[0],
                 'description', isSpaceFilling=True)])

        self.olv_widgets.Bind(wx.EVT_RIGHT_DOWN, parent.on_olv_widgets_right_down)
        self.olv_widgets.Bind(wx.EVT_LIST_ITEM_SELECTED, parent.on_cell_select)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.olv_widgets, proportion=1, flag=wx.EXPAND)
        self.SetSizer(sizer)
        sizer.Fit(self)


class UpDownMenu(wx.Menu):
    def __init__(self, parent):
        super(UpDownMenu, self).__init__()
        up_menu_item = wx.MenuItem(self, wx.NewId(), 'Move item up')
        self.Append(up_menu_item)
        self.Bind(wx.EVT_MENU, parent.move_item_up, up_menu_item)
        down_menu_item = wx.MenuItem(self, wx.NewId(), 'Move item down')
        self.Append(down_menu_item)
        self.Bind(wx.EVT_MENU, parent.move_item_down, down_menu_item)


class Widget(object):
    def __init__(self, description):
        self.description = description

if __name__ == '__main__':
    wx_app = wx.App()
    MainFrame()
    wx_app.MainLoop()
4

1 回答 1

1

您需要将ObjectListView实例绑定到wx.EVT_LIST_ITEM_RIGHT_CLICK. 我重新排列了你的代码来做一个例子:

import wx
from ObjectListView import ObjectListView, ColumnDefn

class MainFrame(wx.Frame):
    MIN_SIZE = (500, 400)
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(None, *args, **kwargs)
        self.panel = MainPanel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel)
        self.SetSizer(sizer)
        self.Center()
        self.SetMinSize(self.MIN_SIZE)
        self.Show()
        widgets = [Widget('a'), Widget('b'),Widget('c')]
        self.panel.olv_widgets.SetObjects(widgets)

    def on_cell_select(self, event):
        print('on_cell_select')


class MainPanel(wx.Panel):
    LIST_SIZE = (2000, 2000)
    def __init__(self, parent, *args, **kwargs):
        super(MainPanel, self).__init__(parent, *args, **kwargs)

        self.olv_widgets = ObjectListView(self, wx.ID_ANY,
                                            style=wx.LC_REPORT|wx.SUNKEN_BORDER,
                                            size=self.LIST_SIZE)
        self.olv_widgets.SetColumns(
                [ColumnDefn('Widget', 'left', parent.MIN_SIZE[0],
                 'description', isSpaceFilling=True)])
        self.olv_widgets.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.on_right_click)

        self.olv_widgets.Bind(wx.EVT_LIST_ITEM_SELECTED, parent.on_cell_select)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.olv_widgets, proportion=1, flag=wx.EXPAND)
        self.SetSizer(sizer)
        sizer.Fit(self)

    def on_right_click(self,event):
        self.list_item_clicked = self.olv_widgets.GetSelectedObject()
        print(self.list_item_clicked.description)
        self.PopupMenu(UpDownMenu(self), self.olv_widgets.GetPosition())

    def move_item_down(self, event):
        print('move_item_down')

    def move_item_up(self, event):
        print('move_item_up')



class UpDownMenu(wx.Menu):
    def __init__(self, parent):
        super(UpDownMenu, self).__init__()
        up_menu_item = wx.MenuItem(self, wx.NewId(), 'Move item up')
        self.Append(up_menu_item)
        self.Bind(wx.EVT_MENU, parent.move_item_up, up_menu_item)
        down_menu_item = wx.MenuItem(self, wx.NewId(), 'Move item down')
        self.Append(down_menu_item)
        self.Bind(wx.EVT_MENU, parent.move_item_down, down_menu_item)


class Widget(object):
    def __init__(self, description):
        self.description = description

if __name__ == '__main__':
    wx_app = wx.App()
    MainFrame()
    wx_app.MainLoop()
于 2019-06-04T15:44:19.093 回答