PyQt是與Python相結合的跨平台GUI庫,可用於多種操作系統。由於PyQt具有豐富的組件內容和完善的文檔支持,它的使用越來越廣泛。並且,PyQt還提供了很多漂亮的GUI界面模板,可以讓應用程序具有更加美觀和專業的外觀。本文將會介紹PyQt漂亮GUI界面模板的一些方面。
一、布局管理器
PyQt的布局管理器可以幫助設計師管理圖形用戶界面的各個部分,並確保它們在不同環境下的合適位置,不會出現遮擋現象。一些重要的布局管理器包括:
1. Box布局
Box布局可以使子部件在行或列中對齊,並支持控制邊距和間距。
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QHBoxLayout, QLabel, QWidget
import sys
class MyApp(QWidget):
def __init__(self):
super().__init__()
vbox = QVBoxLayout()
hbox = QHBoxLayout()
label1 = QLabel('Label 1')
label2 = QLabel('Label 2')
label3 = QLabel('Label 3')
hbox.addWidget(label1)
hbox.addWidget(label2)
vbox.addLayout(hbox)
vbox.addWidget(label3)
self.setLayout(vbox)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
2. Grid布局
Grid布局可以使子部件在網格中對齊,並支持控制行和列的大小。
from PyQt5.QtWidgets import QApplication, QGridLayout, QLabel, QWidget
import sys
class MyApp(QWidget):
def __init__(self):
super().__init__()
grid = QGridLayout()
label1 = QLabel('Label 1')
label2 = QLabel('Label 2')
label3 = QLabel('Label 3')
grid.addWidget(label1, 0, 0)
grid.addWidget(label2, 0, 1)
grid.addWidget(label3, 1, 0, 1, 2)
self.setLayout(grid)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
二、自定義樣式
PyQt漂亮GUI界面模板還提供了一種自定義樣式的方式,可以使設計師更好地控制用戶界面的各個元素外觀效果。
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit
from PyQt5.QtGui import QPalette, QColor
import sys
class MyApp(QWidget):
def __init__(self):
super().__init__()
vbox = QVBoxLayout()
line_edit = QLineEdit(self)
line_edit.setPlaceholderText('Search')
palette = QPalette()
palette.setColor(QPalette.Base, QColor('#2a2a2a'))
palette.setColor(QPalette.Text, QColor('#b1b1b1'))
palette.setColor(QPalette.PlaceholderText, QColor('#808080'))
line_edit.setPalette(palette)
vbox.addWidget(line_edit)
self.setLayout(vbox)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
app.exec_()
三、主題
PyQt漂亮GUI界面模板還提供了一些主題,可以使您的應用程序具有時尚的外觀和感覺。具體主題請參考PyQt官方文檔。
from PyQt5.QtWidgets import QApplication, QWidget, QStackedWidget, QVBoxLayout, QHBoxLayout, QComboBox
from PyQt5.QtGui import QIcon
import sys
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Theme Selector')
vbox = QVBoxLayout()
hbox = QHBoxLayout()
label = QLabel('Select Theme:')
combo_box = QComboBox()
for i in range(1, 6):
combo_box.addItem(QIcon(f'./themes/{i}.png'), f'Theme {i}')
hbox.addWidget(label)
hbox.addWidget(combo_box)
stacked_widget = QStackedWidget()
for i in range(1, 6):
widget = QWidget()
widget.setStyleSheet(f"background-image : url('./themes/{i}.png'); background-repeat: no-repeat; background-position: center;")
stacked_widget.addWidget(widget)
vbox.addLayout(hbox)
vbox.addWidget(stacked_widget)
self.setLayout(vbox)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
app.exec_()
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/293656.html