如何使Wxpython NewWindow出现在Top中

时间:2014-01-28 13:40:20

标签: python wxpython

当按下右键时,弹出菜单为'编辑'或'删除'listctrl项目 如果用户选择了“编辑”,则创建一个新窗口进行编辑 但新窗口出现在旧主窗口的底部, 我知道这可能因为我使用wx.CallAfter(self.PopupMenu,MyPopupMenu(self,item),self.position) 但如果我不使用wx.Callafter,
当我选择一个列表项时, 按鼠标右键,选择“删除”菜单,
将出现错误对话框“无法从列表项XXX中检索信息”。  如何使新窗口顶部和活动?  谢谢


import wx


DATA = {
0 : (u"Test2", "123456", ),
1 : (u"Test", "123456",),
2 : (u"doe", "156789", ),
3 : (u"John", "13455", )
}


class MyApp(wx.App):

    def OnInit(self):
        frame = MyFrame(None)
        frame.Show()
        self.SetTopWindow(frame)
        return True


class MyFrame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent, wx.NewId(), size=(500, -1))
        wx.Frame.CenterOnScreen(self)
        self.panel = MyPanel(self)


class MyPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.list = MyListCtrl(self,3)
        self.add_button = wx.Button(self, label="add")
        h_sizer = wx.BoxSizer(wx.HORIZONTAL)
        h_sizer.Add(self.add_button, proportion=1, flag=wx.ALL, border=5)
        v_sizer = wx.BoxSizer(wx.VERTICAL)
        v_sizer.Add(h_sizer, proportion=0, flag=wx.EXPAND)
        v_sizer.Add(self.list, proportion=1, flag=wx.EXPAND, border=5)
        self.SetSizer(v_sizer)
        self.add_button.Bind(wx.EVT_BUTTON,self.onAdd)

    def onAdd(self, event):
        self.new_w = NewWindow(self)
        self.new_w.Show()


class MyPopupMenu(wx.Menu):

    def __init__(self,parent, item):
        super(MyPopupMenu,self).__init__()
        self.parent = parent
        self.item = item
        menuEdit = wx.MenuItem(self,wx.NewId(), 'edit %s' % item[0])
        self.AppendItem(menuEdit)
        self.Bind(wx.EVT_MENU, self.onEdit, menuEdit)
        menuDel = wx.MenuItem(self,wx.NewId(), 'delete %s' % item[0])
        self.AppendItem(menuDel)
        self.Bind(wx.EVT_MENU, self.OnDelete, menuDel)

    def onEdit(self,e):
        self.parent.parent.edit_w = NewWindow(self.parent.parent)
        self.parent.parent.edit_w.Show()

    def OnDelete(self,e):
        self.parent.DeleteAllItems()

class MyListCtrl(wx.ListCtrl):
    def __init__(self, parent, columns):
        wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT)
        self.parent = parent
        self.R_MOUSE = 0
        self.InsertColumn(0, "name")
        self.InsertColumn(1, "phone")
        self.InsertColumn(2, "address")
        self.SetColumnWidth(0, 200)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelect)
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
        self.itemDataMap = {}
        self.refresh_list()


    def refresh_list(self):
        self.DeleteAllItems()
        for entry in DATA.items():
            self.Append(entry[1])
            self.SetItemData(entry[0],entry[0])

    def OnRightDown(self, event):
        self.R_MOUSE = 1
        self.position = event.GetPosition()
        event.Skip()

    def OnSelect(self, event):
        index = event.GetIndex()
        item = []
        for i in range(3):
            item.append(self.GetItem(itemId=index, col=i).GetText())
        self.SetItemState(index, 0, wx.LIST_STATE_SELECTED)
        self.SetItemBackgroundColour(index, wx.Colour(255,255,0))
        if self.R_MOUSE:
            self.R_MOUSE = 0
            self.PopupMenu(MyPopupMenu(self, item), self.position)


class NewWindow(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent, size=(400,250), style=wx.DEFAULT_FRAME_STYLE)
        self.parent = parent
        self.CenterOnParent()
        panel = wx.Panel(self)


    def onClick(self,event):
        self.Close()


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

1 个答案:

答案 0 :(得分:1)

您可以使用 wx.STAY_ON_TOP

标志

试试这个:

class NewWindow(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, size=(400,250), style=wx.DEFAULT_FRAME_STYLE| wx.STAY_ON_TOP)