![](/style/images/good.png)
![](/style/images/bad.png)
PyQt5系列教程(二十三)自适应布局
source link: https://xugaoxiang.com/2022/03/21/pyqt5-23-auto-fit-windows/
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
这里说的布局自适应,指的是当窗口进行缩小和放大时,布局内的各个控件也能够等比例的缩放。下面还是举例操作一下
进入 designer
,新建一个 MainWindow
,拖入3个 Pushbutton
和1个 Lable
,将按钮放到一个垂直布局里,然后将垂直布局和 Label
一起放到水平布局里
![c2640e3ba996059c.png](https://image.xugaoxiang.com/imgs/2022/03/c2640e3ba996059c.png)
设置下 layoutStretch
,就是左边垂直布局和右边 Label
所占空间的比例
![179539c8a8788541.png](https://image.xugaoxiang.com/imgs/2022/03/179539c8a8788541.png)
来看下预览
![4bf0c0be9a155768.png](https://image.xugaoxiang.com/imgs/2022/03/4bf0c0be9a155768.png)
这时,最大化窗口,发现比例是不对的,也就是没有做到自适应布局
![98cf1c787e39f85b.png](https://image.xugaoxiang.com/imgs/2022/03/98cf1c787e39f85b.png)
解决方法,其实很简单,我们在空白位置,点击鼠标右键,选择 布局 --> 栅格布局,然后保存
![28f1cd69925c350c.png](https://image.xugaoxiang.com/imgs/2022/03/28f1cd69925c350c.png)
再来预览一下
![f1d8d8db725b09a0.png](https://image.xugaoxiang.com/imgs/2022/03/f1d8d8db725b09a0.png)
要想让每个控件都自适应,可以在属性 SizePolicy
中,将水平策略和垂直策略都设置成 Expanding
https://github.com/xugaoxiang/learningPyQt5
PyQt5系列教程
更多PyQt5
教程,请移步
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK