

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
-
10
二十五篇系列:游戏里的边缘崛起和市场天花板的产品模型验证 发布时间:2020-12-14 08:27:33 Tags:游戏的产品类型,
-
15
二十五篇系列:结婚系统的外延投射对留存和变现的负面影响 发布时间:2020-11-30 08:28:07 Tags:Monster Strike,
-
12
PyQt5 教程系列(七)控件 作者: haiyang 分类: Python 发布时间: 2019-08-03 08:...
-
10
PyQt5 教程系列(七)控件(II) 作者: haiyang 分类: Python 发布时间: 2019-08-...
-
11
PyQt5 教程系列(十)自定义控件 作者: haiyang 分类: Python 发布时间: 2019-08-...
-
6
响应式网页设计:rem、em设置网页字体大小自适应 「rem」是指根元素(root element,html)的字体大小,好开心的是,从遥远的 IE6 到版本帝 Chrome 他们都约好了,根元素默认的 font-size 都是 16px。这样一个新的单位兼容性如何呢? IE9+,F...
-
2
软硬件环境 Windows 10 64bit Anaconda3 with python 3.7 PyQt5 这里说的布局自适应,指的是当窗口进行缩小和放大时,布局内的各个控件也能够等比例的缩放。下面还是举例操作一下 进入
-
26
软硬件环境 Windows 10 64bit Anaconda3 with python 3.7 PyQt5 使用 Qt Designer 来设计界面,左侧是2个按钮,右侧是一个垂直布...
-
6
软硬件环境 Windows 10 64bit Anaconda3 with python 3.8 PyQt5 5.15 我们常见的界面上都有最小化、最大化按钮
-
4
GDI+下字体大小自适应方案初探 | 素履独行在某个瞬间,我忽然发觉,三体或是AI,本质上是非常相近的事物,甚至在面对任何未知领域的时候,人类总会不自觉地划分为降临派、拯救派和幸存派。姑且不论马斯克等人叫停 GPT-5 的真实动机如何,当大语言模型...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK