PyQt 中的第 3 方 Python 模块 [英] 3rd Party Python Modules in PyQt

查看:48
本文介绍了PyQt 中的第 3 方 Python 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定一种用于开发可以与串行端口连接的桌面应用程序的技术.我研究了 python,看起来基于控制台的应用程序并不困难 - http://pyserial.sourceforge.net/ ,我还研究了 PyQt,它可以实现我项目的 GUI 部分.

I am trying to decide on a technology for developing a desktop application that can interface with the serial port. I have looked into python and it looks like a console based app would not be difficult - http://pyserial.sourceforge.net/ , and I have also looked at PyQt, which would fulfill the GUI portion of my project.

但是是否可以在 PyQt 中包含像 PySerial 这样的 3rd 方模块?

But is it possible to include 3rd party modules like PySerial in PyQt?

推荐答案

PyQt 和 pyserial 配合得很好(事实上,我在我的一个应用程序中使用它们.)pyserial 网站上的示例大多是简单的控制台 -像示例一样,但没有理由不能从任何 pyserial 对象中获取数据并在 PyQt 中使用它们.例如,您可以将通过串行端口接收到的数据推送到 QByteArray 或 NumPy 数组或任何您喜欢的类似数组中.

PyQt and pyserial play nice together (in fact, I'm using them in one of my apps.) The examples on the pyserial website are mostly simple console-like examples, but there's no reason you cannot take data from any of the pyserial objects and use them in PyQt. You could, for instance, take data that you received over a serial port and push it into a QByteArray or NumPy array or anything similar as fits your fancy.

几个警告:如果你使用 Serial.readline(),它会阻塞直到它得到一个换行符 (\n).这可能对您的 GUI 不利.如果您必须使用 readline() 而不是 read(),我建议将与 pyserial 相关的活动放在单独的线程中.你如何做到这一点取决于你,但我建议使用 Qt 的内置线程.然后,您可以在线程内部将数据转换为 Qt 类型等.有关pyqt 中的线程的更多信息.

A couple of caveats: if you use Serial.readline(), it is blocking until it gets a newline (\n). This could be bad for your GUI. If you must use readline() instead of read(), I recommend putting your pyserial related activities in a separate thread. How you do this is up to you, but I'd recommend using Qt's built in threading. You can then do data conversions to Qt types and such inside the thread. More info on threading in pyqt.

编辑:差点忘了.如果有人关心,您也可以使用 PyQt 编写控制台应用程序.只需使用 QCoreApplication 而不是 QApplication.不太确定为什么会在 python 中这样做,但是,除非您真的很喜欢 Qt 的数据类型……在这种情况下,您可以使用 PyQt 和 pyserial 来编写纯控制台应用程序:D

Edit: almost forgot. If anyone cares, you can use PyQt to write console apps too. Just use QCoreApplication instead of QApplication. Not quite sure why one would do that in python, however, unless you were really fond of Qt's data types... in this case you could use PyQt and pyserial to write a pure console app :D

这篇关于PyQt 中的第 3 方 Python 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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