pyqt 在Widgets中显示图片和文字

思路非常简单:<p>创建window,设置窗口大小,创建label1,导入图片,创建label2,导入文字,show,结束!</p>

import sys
from PyQt5 import QtWidgets,QtGui
#定义窗口函数window
def window():
    #我事实上不太明白干嘛要这一句话,只是pyqt窗口的建立都必须调用QApplication方法
    app=QtWidgets.QApplication(sys.argv)
    #新建一个窗口,名字叫做w
    w=QtWidgets.QWidget()
    #定义w的大小
    w.setGeometry(100,100,300,200)
    #给w一个Title
    w.setWindowTitle('lesson 2')
    #在窗口w中,新建一个lable,名字叫做l1
    l1=QtWidgets.QLabel(w)
    #调用QtGui.QPixmap方法,打开一个图片,存放在变量png中
    png=QtGui.QPixmap('/home/capture/Pictures/Selection_026.png')
    # 在l1里面,调用setPixmap命令,建立一个图像存放框,并将之前的图像png存放在这个框框里。
    l1.setPixmap(png)

    #在窗口w中,新建另一个label,名字叫做l2
    l2=QtWidgets.QLabel(w)
    #用open方法打开一个文本文件,并且调用read命令,将其内容读入到file_text中
    file=open('/home/capture/eric6_test/auto_k2_all/test1.log')
    file_text=file.read()
    #调用setText命令,在l2中显示刚才的内容
    l2.setText(file_text)

    #调整l1和l2的位置
    l1.move(100,20)
    l2.move(140,120)
    #显示整个窗口
    w.show()
    #退出整个app
    app.exit(app.exec_())
#调用window这个函数
window()


不过,这样写的目的是什么,弄一个函数,来生成一个图像,没有参数可以输入?还不如不用函数呢。所以,我改了一下。

import sys
from PyQt5 import QtWidgets,QtGui
#定义窗口函数window
def window(png,file_text):
    #新建一个窗口,名字叫做w
    w=QtWidgets.QWidget()
    #定义w的大小
    w.setGeometry(100,100,300,200)
    #给w一个Title
    w.setWindowTitle('lesson 2')
    #在窗口w中,新建一个lable,名字叫做l1
    l1=QtWidgets.QLabel(w)
    #调用QtGui.QPixmap方法,打开一个图片,存放在变量png中

    # 在l1里面,调用setPixmap命令,建立一个图像存放框,并将之前的图像png存放在这个框框里。
    l1.setPixmap(png)

    #在窗口w中,新建另一个label,名字叫做l2
    l2=QtWidgets.QLabel(w)
    #用open方法打开一个文本文件,并且调用read命令,将其内容读入到file_text中

    #调用setText命令,在l2中显示刚才的内容
    l2.setText(file_text)

    #调整l1和l2的位置
    l1.move(100,20)
    l2.move(140,120)
    #显示整个窗口
    w.show()
    #退出函数,很奇怪,没有这个还不行
    sys.exit(app.exec_())
#调用window这个函数
if __name__ == '__main__':
    #我事实上不太明白干嘛要这一句话,只是pyqt窗口的建立都必须调用QApplication方法
    app = QtWidgets.QApplication(sys.argv)
    #图片和文件,各打开一个
    Png=QtGui.QPixmap('/home/capture/Pictures/Selection_026.png')
    File = open('/home/capture/eric6_test/auto_k2_all/test1.log')
    File_text = File.read()
    #把图片和文本传递给函数window
    window(Png,File_text)
    #退出
    app.exit(app.exec_())




  • 6
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在PyQt的TextBrowser显示图片并设置文字围绕,可以使用QTextDocument和QTextImageFormat类。具体步骤如下: 1. 创建一个QTextDocument对象,并设置其默认字体、字体大小等属性。 2. 创建一个QTextImageFormat对象,并设置其图片路径、大小、对齐方式等属性。 3. 使用QTextCursor对象将图片插入到文档。 4. 使用QTextCharFormat对象设置文本样式,包括文字对齐方式、行距等。 5. 最后,将QTextDocument对象设置为TextBrowser的文档对象。 下面是一个示例代码: ``` from PyQt5.QtWidgets import QTextBrowser, QApplication from PyQt5.QtGui import QTextDocument, QTextImageFormat, QTextCursor, QTextCharFormat from PyQt5.QtCore import Qt app = QApplication([]) text_browser = QTextBrowser() # Create a document and set default font and size document = QTextDocument() font = QTextCharFormat() font.setFontFamily("Arial") font.setFontPointSize(12) document.setDefaultFont(font) # Create an image format and set properties image_format = QTextImageFormat() image_format.setWidth(200) image_format.setHeight(200) image_format.setName("path/to/image.png") image_format.setAlignment(Qt.AlignRight) # Insert image using a cursor cursor = QTextCursor(document) cursor.insertImage(image_format) # Set text alignment and line spacing char_format = QTextCharFormat() char_format.setAlignment(Qt.AlignJustify) char_format.setLineHeight(150, QTextCharFormat.ProportionalHeight) cursor.setCharFormat(char_format) # Set the document as the browser's document text_browser.setDocument(document) text_browser.show() app.exec_() ``` 在上面的示例,我们创建了一个200x200像素的图片,并将其设置为右对齐。然后,我们使用QTextCursor对象将图片插入到文档,并使用QTextCharFormat对象设置了文本样式,包括文字对齐方式和行距。 最后,将QTextDocument对象设置为TextBrowser的文档对象,即可在TextBrowser显示带有文字围绕的图片
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值