如何在 PyQt 中发送 UI 事件? [英] How to send UI events in PyQt?
问题描述
我想在我的程序中自动化 UI 操作:它可以显示菜单、打开另一个表单等.我在我的 python 应用程序中使用 .py
文件和 .ui
文件.我该怎么做?
I want to automate UI actions in my program: it can be show a menu, open another form, etc.
I'm using .py
files and .ui
files in my python app.
How can i do this?
推荐答案
几乎所有的 GUI 框架都有一个事件的概念,它通常由用户操作(按钮单击、窗口拖动等)生成并发送到您的处理程序.
Almost all GUI frameworks have a notion of an event, which are typically generated by user actions (button clicking, window dragging, etc.) and send to your handlers.
但事件通常也可以直接发送到事件循环.就您而言,我假设您使用的是 Digia 的 PyQt 和 Qt 工具包.
But event can usually also be sent directly to the event loop. In your case, I'm assuming you're using PyQt and Qt toolkit from Digia.
QCoreApplication.sendEvent
的 PyQt 文档a> 看起来是个不错的起点:
The PyQt docs for QCoreApplication.sendEvent
look like a good place to start:
bool QCoreApplication.sendEvent(QObject 接收器,QEvent 事件)
使用notify() 函数将事件事件直接发送到接收者接收者.返回从事件处理程序返回的值.
Sends event event directly to receiver receiver, using the notify() function. Returns the value that was returned from the event handler.
事件发送后不会删除事件.正常的做法是在栈上创建事件,例如:
The event is not deleted when the event has been sent. The normal approach is to create the event on the stack, for example:
QMouseEvent event(QEvent.MouseButtonPress, pos, 0, 0, 0);
QApplication.sendEvent(mainWindow, &event);
另见 postEvent() 和 notify().
See also postEvent() and notify().
不要忽略对 postEvent
和 notify
的另见.
Don't ignore the see-alsos to postEvent
and notify
.
更多信息也将在 Digia 的 postEvent Qt 文档
这篇关于如何在 PyQt 中发送 UI 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!