使用wx.toolbar在wxPython中单击后是否可以执行事件?

问题描述 投票:2回答:2

目前,我正在完成我的最终高中项目,但遇到了严重的问题。

我创建了一个wx.Toolbar,使用wx.AddTool添加每个选项,然后将一些功能绑定到它,它只对所有内容执行一次(开始时),并且在单击它后拒绝执行任何操作。 ...它比我想要的要早开始。

我将跳过一些代码,仅使用所需的代码。

self.frame_toolbar.AddTool(1, "one", Base64ToImg(image1), Base64ToImage(image1-disabled), "One", "First Thing")
self.frame_toolbar.AddTool(2, "two", Base64ToImg(image1), Base64ToImage(image1-disabled), "Two", "Second Thing")
self.frame_toolbar.AddTool(3, "three", Base64ToImg(image1), Base64ToImage(image1-disabled), "Three", "Third Thing")

self.frame_toolbar.Realize()

self.SetToolBar(self.frame_toolbar)

所以现在,我有了工具栏,上面有一些工具。现在:

self.Bind(wx.EVT_TOOL, self.onefunction(params), id=1)
self.Bind(wx.EVT_BUTTON, self.twofunction(params), id=2)
self.Bind(wx.EVT_MENU, self.threefunction(params), id=3)

也是

self.frame_toolbar.Bind(wx.EVT_TOOL, self.onefunction(params), id=1)
self.frame_toolbar.Bind(wx.EVT_BUTTON, self.twofunction(params), id=2)
self.frame_toolbar.Bind(wx.EVT_MENU, self.threefunction(params), id=3)

在工具栏加载后立即执行。当我单击按钮时是否可以使其执行ONLY

非常感谢您的帮助。R

python python-3.x wxpython
2个回答
2
投票

self.frame_toolbar.Bind(wx.EVT_TOOL, lambda evt:self.onefunction(params), id=1)

我认为可以解决您的问题


2
投票

您正在立即调用该函数,self.onefunction(params)。尝试删除括号-这会将其保留为函数,而不是函数的返回]

self.Bind(wx.EVT_TOOL, self.onefunction, id=1)
self.Bind(wx.EVT_BUTTON, self.twofunction, id=2)
self.Bind(wx.EVT_MENU, self.threefunction, id=3)

如果您需要传递参数,请检查乔兰的答案

© www.soinside.com 2019 - 2024. All rights reserved.