如何动态替换wxpython中的菜单栏?

公园

我正在用wxpython制作像AmCap这样的相机查看器。现在,我正在制作一个菜单栏,以显示已连接的摄像机列表。(例如AMCAP设备菜单,见图1)。

这是我的代码:(get_all_devices返回已连接设备的列表,并且工作正常)

class MainFrame(wx.Frame):
    def __init__(self, parent, fid, title, size):
        wx.Frame.__init__(self, parent, fid, title, wx.DefaultPosition, size)

        self.devices = get_all_devices()
        # init menubar
        self.make_menubar()

        # set timer to check usb connectivity
        self.timer = wx.Timer(self)
        self.timer.Start(100) 
        self.Bind(wx.EVT_TIMER, self.check_device)

    def make_menubar(self):
        self.menubar = wx.MenuBar()
        self.devices_menu = wx.Menu()
        self.options_menu = wx.Menu()
        help_menu = wx.Menu()
        self.make_devices_menu()

        self.options_menu.Append(101, 'resolutions')
        self.menubar.Append(self.devices_menu, 'devices')
        self.menubar.Append(self.options_menu, 'options')
        self.menubar.Append(help_menu, 'help')

        self.SetMenuBar(self.menubar)

    def recreate_menubar(self):
        self.menubar.Destroy()
        self.make_menubar()
        self.Layout()
        self.Refresh()

    def make_devices_menu(self):
        for i in range(len(self.devices)):
            self.devices_menu.Append(CI.MENU_DEVICES + 1 + i, self.devices[i], kind=wx.ITEM_CHECK)
            self.Bind(wx.EVT_MENU, self.click_device_menu, id=CI.MENU_DEVICES + 1 +  i)

    def check_device(self, evt):
        cur_devices = get_all_devices()
        if set(self.devices) != set(cur_devices):
            self.devices = cur_devices
            self.recreate_menubar()

一开始确实可以,但是如果我尝试断开相机的连接并连接4至5次,它不会重新创建菜单栏。例如,假设有两个连接的摄像机,如图1所示。如果我断开第一个摄像机的连接,程序将如图2所示(仅剩一个摄像机)。然后,如果我再次连接摄像机,它将再次显示两个摄像机,如图1所示。这就是我的预期(图1-断开连接->图2-连接->图1)

但实际上,经过4〜5次后,它无法正常工作。它不会更新菜单栏。(图1-断开连接->图2-再次连接->图2!)

我做错了什么?wxpython中不允许吗?

如果您需要有关我的代码的更多信息,请告诉我。

任何提示将欢迎!提前致谢。

在此处输入图片说明

图。1

它

图2

编辑:

我发现,如果我在init的末尾添加如下代码

self.menubar.SetName(str(self.devices))
self.SetMenuBar(self.menubar)
print(self.menubar.GetName())
print(self.GetMenuBar().GetName())

它返回连接的摄像机的相同列表。因此,我认为wxpyhthon可以正确地制作和设置菜单栏。

VZ。

虽然重新创建整个菜单栏看起来有些多余(您可以仅重新创建设备菜单,或者甚至可以删除旧的相机项目并添加新的相机项目),但它仍然可以正常工作。

您是否已检查过您recreate_menubar()期望得到的呼叫?您可以确定显示一个消息框。如果它被称为,但不知何故不更新菜单栏,做的最好的事情是尝试重现该问题在SSCCE并打开一票wxTrac,以便它可以进行调试,希望固定的(请注明您的平台如果您这样做)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章