为 python gui 实现宏记录器?

发布于 2024-10-18 16:33:46 字数 300 浏览 5 评论 0原文

我想知道如何为 python gui(可能是 PyQt,但理想情况下是不可知的)实现宏记录器。与 Excel 中的类似,但它不是获取 VB 宏,而是创建 Python 代码。之前我为 Tkinter 做了一些事情,其中​​所有回调都通过记录操作的单个类。不幸的是,我的班级进行日志记录有点难看,我正在寻找一个更好的班级。虽然这确实将 GUI 与代码的其余部分很好地分开了,但就通常的信号/插槽接线而言,它似乎很不寻常。有更好的办法吗?

目的是用户可以在图形界面中完成数据分析过程,查看其决策的效果。稍后,只需稍加修改即可将记录的过程应用于其他数据,并且无需启动 GUI。

I'm wondering how to go about implementing a macro recorder for a python gui (probably PyQt, but ideally agnostic). Something much like in Excel but instead of getting VB macros, it would create python code. Previously I made something for Tkinter where all callbacks pass through a single class that logged actions. Unfortunately my class doing the logging was a bit ugly and I'm looking for a nicer one. While this did make a nice separation of the gui from the rest of the code, it seems to be unusual in terms of the usual signals/slots wiring. Is there a better way?

The intention is that a user can work their way through a data analysis procedure in a graphical interface, seeing the effect of their decisions. Later the recorded procedure could be applied to other data with minor modification and without needing the start up the gui.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

め七分饶幸 2024-10-25 16:33:46

您可以应用命令设计模式:当用户执行操作时,生成表示所需更改的命令。然后,您实现某种命令管道来执行命令本身,很可能只是调用您已有的方法。执行命令后,您可以将它们序列化或按照您想要的方式记下它们,并在需要重新执行过程时加载这一系列命令。

You could apply the command design pattern: when your user executes an action, generate a command that represents the changes required. You then implement some sort of command pipeline that executes the commands themselves, most likely just calling the methods you already have. Once the commands are executed, you can serialize them or take note of them the way you want and load the series of commands when you need to re-execute the procedure.

小嗷兮 2024-10-25 16:33:46

从高层次思考,这就是我要做的:

开发一个装饰器函数,用它来装饰每个事件处理函数。

该装饰器函数将在统一的数据结构中记录所调用的函数及其参数(以及可能的返回值) - 在此数据结构上注意将 Widget 和 Control 实例标记为特殊类型的对象。这是因为在其他运行中这些小部件不会是相同的实例 - 啊,您甚至无法序列化工具包小部件实例,无论是 Qt 还是其他。

当需要播放宏时,您可以用实际运行对象的实例替换代表小部件的对象来填补空白,然后只需使用剩余参数调用原始函数即可。

在具有传递给事件处理函数的专门“事件”参数的工具包中,您还必须负责该事件的序列化和反序列化。

我希望这能有所帮助。我可以为此提出一些概念验证代码(尽管我今天很想使用 tkinter - 必须阅读很多内容才能提出 Qt4 示例)。

Thinking in high level, this is what I'd do:

Develop a decorator function, with which I'd decorate every event-handling functions.

This decorator functions would take note of thee function called, and its parameters (and possibly returning values) in a unified data-structure - taking care, on this data structure, to mark Widget and Control instances as a special type of object. That is because in other runs these widgets won't be the same instances - ah, you can't even serialize a toolkit widget instances, be it Qt or otherwise.

When the time comes to play a macro, you fill-in the gaps replacing the widget-representating object with the instances of the actually running objects, and simply call the original functions with the remaining parameters.

In toolkits that have an specialized "event" parameter that is passed down to event-handling functions, you will have to take care of serializing and de-serializing this event as well.

I hope this can help. I could come up with some proof of concept code for that (although I am in a mood to use tkinter today - would have to read a lot to come up with a Qt4 example).

后eg是否自 2024-10-25 16:33:46

您正在寻找的示例位于 mayavi2。出于您的目的,mayavi2 的“脚本记录”功能将生成一个 Python 脚本,然后可以针对其他情况进行简单修改。我听说它效果很好。

An example of what you're looking for is in mayavi2. For your purposes, mayavi2's "script record" functionality will generate a Python script that can then be trivially modified for other cases. I hear that it works pretty well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文