pyqt - 如何制作一个 textarea 来写入消息 - 有点像打印到控制台 [英] pyqt - how to make a textarea to write messages to - kinda like printing to a console

查看:82
本文介绍了pyqt - 如何制作一个 textarea 来写入消息 - 有点像打印到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 pyqt 相当陌生 - 我目前正在使用它来制作图形的可视化表示.我为此制作了一个自定义小部件,这很容易.但现在我在不得不使用内置功能时陷入困境.

I'm fairly new to pyqt - I'm currently using it to make a visual representation of a graph. I made a custom widget for this, which was fairly easy. But now I'm stuck when having to use built in functionality.

我想向我的应用程序添加一个视图"并能够向其打印文本(有点像使用 print("blablabla") 打印到控制台时发生的情况)

I want to add a 'view' to my application and be able to print text to it (kinda like what happens when you print to the console with print("blablabla") )

我尝试使用 pyqt api 来发现什么/如何但是..

I tried to use the pyqt api to discover what/how but..

http://pyqt.sourceforge.net/Docs/PyQt4/qtgui.html

它包含 41 个文本形式的类 + 其他内容,公平地说,我不知道该使用哪个类?

It contains 41 classes in the form of text + something else and to be fair I have NO clue to which one to use?

因此,如果有人能指出我是哪一个,并且您有时间了解如何将其用于我想要的目的,那将不胜感激 ^^

so if someone could point me out which one, and if you have time on how to use it for the purpose I want to, that would be much appreciated ^^

推荐答案

最简单的方法是使用 QTextEdit,可能通过 setReadOnly() 并使用 append()insertPlainText() 方法附加您的文本.对于类似的用例,我粗略地使用了以下内容:

The easiest way would be to use a QTextEdit, probably set it to read only through setReadOnly() and append your text with the append() or insertPlainText() method. I roughly used something like the following for a similar use case:

基本代码段:

...
logOutput = QTextEdit(parent)
logOutput.setReadOnly(True)
logOutput.setLineWrapMode(QTextEdit.NoWrap)

font = logOutput.font()
font.setFamily("Courier")
font.setPointSize(10)

theLayout.addWidget(logOutput)
...

要将任意颜色的文本附加到文本区域的末尾并自动滚动文本区域以使新文本始终可见,您可以使用类似

To append text in an arbitrary color to the end of the text area and to automatically scroll the text area so that the new text is always visible, you can use something like

自动滚动片段:

...
logOutput.moveCursor(QTextCursor.End)
logOutput.setCurrentFont(font)
logOutput.setTextColor(color)

logOutput.insertPlainText(text)

sb = logOutput.verticalScrollBar()
sb.setValue(sb.maximum())
...

这篇关于pyqt - 如何制作一个 textarea 来写入消息 - 有点像打印到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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