5
PyQt5系列教程(二十五)图片自适应控件大小
source link: https://xugaoxiang.com/2022/04/10/pyqt5-25-image-scale/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
软硬件环境
- Windows 10 64bit
- Anaconda3 with python 3.7
- PyQt5
一般用 QLabel
控件来存放图片,但是图片有大有小,比例也不一样,如何才能让它自适应控件大小呢?本文来实践一下。
使用工具 designer
,新建一个窗口,然后拖动一个 label
到窗口中,接着,空白处右键点击 布局 --> 栅格布局
保存成 ui
文件后,使用 pyuic
将其转化成 python
代码
pyuic5.bat -o ui.py project.ui
完整的代码如下
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtGui import QPixmap
from ui import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
pix = QPixmap('wechat.jpg')
# 自适应
self.label.setScaledContents(True)
self.label.setPixmap(pix)
if __name__ == '__main__':
app = QApplication(sys.argv)
windows = MainWindow()
windows.show()
sys.exit(app.exec_())
执行上述代码
https://github.com/xugaoxiang/learningPyQt5
PyQt5系列教程
更多PyQt5
教程,请移步
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK