Pyqt5自適應(yīng)布局實(shí)例
在pyqt5中要做到自適應(yīng)布局,必須應(yīng)用Layout類
下面列出類似于 html 中 float 功能的布局方法:
實(shí)現(xiàn)原理: PyQt5中的布局中,stretch 屬性類似于一個(gè)可自適應(yīng)的空白布局。會(huì)盡可能將周圍的組件布局向外擠壓
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
基本上,大的布局通過(guò)上面的方法即可實(shí)現(xiàn),細(xì)節(jié)可能還需要調(diào)整 margin 或者 spacing
以上這篇Pyqt5自適應(yīng)布局實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Tornado Application的實(shí)現(xiàn)
本文主要介紹了Tornado Application的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05pytorch masked_fill報(bào)錯(cuò)的解決
今天小編就為大家分享一篇pytorch masked_fill報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02python實(shí)現(xiàn)將一維列表轉(zhuǎn)換為多維列表(numpy+reshape)
今天小編就為大家分享一篇python實(shí)現(xiàn)將一維列表轉(zhuǎn)換為多維列表(numpy+reshape),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11詳解Python如何精確控制asyncio并發(fā)運(yùn)行多個(gè)任務(wù)
這篇文章主要為大家詳細(xì)介紹了Python如何精確控制asyncio并發(fā)運(yùn)行多個(gè)任務(wù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10python實(shí)現(xiàn)模擬按鍵,自動(dòng)翻頁(yè)看u17漫畫
這篇文章主要介紹了python實(shí)現(xiàn)模擬按鍵,自動(dòng)翻頁(yè)看u17漫畫,十分簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-03-03