以编程方式调用 wxPython EVT_BUTTON 事件 [英] Invoking a wxPython EVT_BUTTON event programmatically

查看:29
本文介绍了以编程方式调用 wxPython EVT_BUTTON 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了关于这个主题的另一个问题,但我不能完全让信息对我有用,所以我想我会给出我的细节 - 我怀疑我只是目光短浅.

I've seen another question regarding this topic, but I couldn't quite get the information to work for me, so I thought I'd give my specifics - I suspect I'm just being short-sighted.

我正在尝试从测试框架中练习我的 GUI,这涉及在测试脚本中手动调用事件(在本例中为按下按钮).到目前为止,除了其他不相关的胡言乱语之外,我还有:

I'm trying to exercise my GUI from a test framework, which involves manually invoking an event (in this case a button press) within a test script. So far, in addition to other irrelevant guff, I have:

# In GUI class:

self.button_1 = wx.Button(self, id=wx.ID_ANY, label="Button 1")
self.button_1.Bind(wx.EVT_BUTTON, self.button_1)

# In GUI Test class:

event = wx.PyCommandEvent(X, Y)
wx.PostEvent(get_gui_instance(), event)

我的问题是我不知道 XY 应该是什么(假设其余的没问题).非常感谢任何帮助.

My problem is that I don't know what X and Y should be (assuming that the rest is ok). Any help is greatly appreciated.

推荐答案

原来是因为我re- 从 GUI 测试中将我的 GUI 设置为在工作线程中运行,我可以直接与它通信.我应该早点意识到这一点,但结果是我不需要费心将事件从 GUI 测试发布到 GUI,因为它们在同一进程中运行.

So it turns out that because I've re-jigged my GUI to run in a worker thread from GUI Test, I can communicate with it directly. I should have realised this earlier, but nonetheless the result is that I don't need to bother with posting events from GUI Test to GUI since they're running in the same process.

相反,我现在可以直接调用事件的效果.例如,我可以调用 on_button_press(),绕过按钮的实际点击,这通常会在 wxPython 中触发事件.这使我能够模拟用户交互并测试工作流程和输入范围,这正是我想要做的.

Instead, I can now call the effects of events directly. For example, I can call on_button_press(), bypassing the actual clicking of the button, which would normally fire off the event in wxPython. This allows me to simulate user interaction and test workflows and input ranges, which is exactly what I wanted to do.

当然,这只是因为我在与测试套件相同的进程中运行我的 GUI.否则,发布事件似乎是前进的方向,在回答我自己的问题时,似乎自定义事件是跨进程调用按钮按下的方式.这意味着在 GUI 中使用某种测试代理"来处理那些特定于测试的事件.

Of course, this only works because I'm running my GUI in the same process as the test suite. Posting events seems to be the way forward otherwise, and in answer to my own question, it seems custom events are the way to invoke button presses cross-process. This implies the use of some sort of "test agent" within the GUI to handle those events that are specific for testing.

这篇关于以编程方式调用 wxPython EVT_BUTTON 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆