Pyqt5自適應布局實例
更新時間:2019年12月13日 18:33:24 作者:fengtangzheng
今天小編就為大家分享一篇Pyqt5自適應布局實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
在pyqt5中要做到自適應布局,必須應用Layout類
下面列出類似于 html 中 float 功能的布局方法:
實現(xiàn)原理: PyQt5中的布局中,stretch 屬性類似于一個可自適應的空白布局。會盡可能將周圍的組件布局向外擠壓
from PyQt5.QtWidgets import QVBoxLayout, QHBoxLayout
# 垂直居中
def vcenter_layout(*widgets):
vbox = QVBoxLayout()
vbox.addStretch()
for widget in widgets:
vbox.addWidget(widget)
vbox.addStretch()
return vbox
# 水平居中
def hcenter_layout(*widgets):
hbox = QHBoxLayout()
hbox.addStretch()
for widget in widgets:
hbox.addWidget(widget)
hbox.addStretch()
return hbox
# 垂直水平居中
def center_layout(widget):
hbox = QHBoxLayout()
hbox.addStretch()
hbox.addWidget(widget)
hbox.addStretch()
vbox = QVBoxLayout()
vbox.addStretch()
vbox.addLayout(hbox)
vbox.addStretch()
return vbox
# 居左
def left_layout(*widgets):
hbox = QHBoxLayout()
for widget in widgets:
hbox.addWidget(widget)
hbox.addStretch()
return hbox
# 居右
def right_layout(*widgets):
hbox = QHBoxLayout()
hbox.addStretch()
for widget in widgets:
hbox.addWidget(widget)
return hbox
# 向上靠齊
def top_layout(*widgets):
vbox = QVBoxLayout()
for widget in widgets:
vbox.addWidget(widget)
vbox.addStretch()
return vbox
# 向下靠齊
def bottom_layout(*widgets):
vbox = QVBoxLayout()
vbox.addStretch()
for widget in widgets:
vbox.addWidget(widget)
return vbox
# 正常垂直分布
def v_layout(*widgets):
vbox = QVBoxLayout()
for widget in widgets:
vbox.addWidget(widget)
return vbox
# 正常垂直分布
def h_layout(*widgets):
vbox = QHBoxLayout()
for widget in widgets:
vbox.addWidget(widget)
return vbox
基本上,大的布局通過上面的方法即可實現(xiàn),細節(jié)可能還需要調整 margin 或者 spacing
以上這篇Pyqt5自適應布局實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python GUI庫圖形界面開發(fā)之PyQt5窗口布局控件QStackedWidget詳細使用方法
- python GUI庫圖形界面開發(fā)之PyQt5動態(tài)(可拖動控件大小)布局控件QSplitter詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5布局控件QVBoxLayout詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5表單布局控件QFormLayout詳細使用方法與實例
- Python深度學習實戰(zhàn)PyQt5布局管理項目示例詳解
- PyQt5的相對布局管理的實現(xiàn)
- Pyqt5中5種布局的實現(xiàn)示例
相關文章
python中三種輸出格式總結(%,format,f-string)
在Python語言編程中,我們會與字符串打交道,那務必會輸出字符串來查看字符串的內容,下面這篇文章主要給大家介紹了關于python中三種輸出格式的相關資料,三種格式分別是%,format,f-string,需要的朋友可以參考下2022-03-03

