日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python做一个窗口样式_python3+PyQt5 自定义窗口部件--使用窗口部件样式表的方法...

發(fā)布時間:2024/7/23 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python做一个窗口样式_python3+PyQt5 自定义窗口部件--使用窗口部件样式表的方法... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文借用HTML的css語法,將樣式表應(yīng)用到窗口部件。這里只是個簡單的例子,實際上樣式表的語法很豐富。

以下類似于css:

StyleSheet = """

QComboBox { color: darkblue; }

QLineEdit { color: darkgreen; }

QLineEdit[mandatory="true"] { #mandatory="true"時,QLineEdit的樣式會變化

background-color: rgb(255, 255, 127);

color: darkblue;

}

如果在選擇器的前面加上一個句點,比如.QLineEdit,則選擇器就會只應(yīng)用于指定的類,而不會應(yīng)用于這個類的子類。如果要求選擇器僅用于某一特定窗口部件,則可以對該窗口部件調(diào)用setObjectName(),然后用該名字作為選擇器的一部分。比如,如果有一個按鈕,其對象名字是“findButton”,則應(yīng)用于這個按鈕的選擇器就應(yīng)該是QpushButton#findButton。有些窗口部件會有一些子控件。例如QComboBox會有一個箭頭子控件,用戶通過點擊這個箭頭來看到下拉列表。子控件可以指定為選擇器的一部分–例如,QComboBox::drop-down。偽狀態(tài)可以用一個冒號指定–例如,QCheckBox::checked.

#!/usr/bin/env python3

import sys

from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog,

QDialogButtonBox, QGridLayout, QLabel, QLineEdit, QVBoxLayout)

class ContactDlg(QDialog):

StyleSheet = """

QComboBox { color: darkblue; }

QLineEdit { color: darkgreen; }

QLineEdit[mandatory="true"] {

background-color: rgb(255, 255, 127);

color: darkblue;

}

"""

def __init__(self, parent=None):

super(ContactDlg, self).__init__(parent)

forenameLabel = QLabel("&Forename:")

self.forenameEdit = QLineEdit()

forenameLabel.setBuddy(self.forenameEdit)

surnameLabel = QLabel("&Surname:")

self.surnameEdit = QLineEdit()

surnameLabel.setBuddy(self.surnameEdit)

categoryLabel = QLabel("&Category:")

self.categoryComboBox = QComboBox()

categoryLabel.setBuddy(self.categoryComboBox)

self.categoryComboBox.addItems(["Business", "Domestic",

"Personal"])

companyLabel = QLabel("C&ompany:")

self.companyEdit = QLineEdit()

companyLabel.setBuddy(self.companyEdit)

addressLabel = QLabel("A&ddress:")

self.addressEdit = QLineEdit()

addressLabel.setBuddy(self.addressEdit)

phoneLabel = QLabel("&Phone:")

self.phoneEdit = QLineEdit()

phoneLabel.setBuddy(self.phoneEdit)

mobileLabel = QLabel("&Mobile:")

self.mobileEdit = QLineEdit()

mobileLabel.setBuddy(self.mobileEdit)

faxLabel = QLabel("Fa&x:")

self.faxEdit = QLineEdit()

faxLabel.setBuddy(self.faxEdit)

emailLabel = QLabel("&Email:")

self.emailEdit = QLineEdit()

emailLabel.setBuddy(self.emailEdit)

self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|

QDialogButtonBox.Cancel)

addButton = self.buttonBox.button(QDialogButtonBox.Ok)

addButton.setText("&Add")

addButton.setEnabled(False)

grid = QGridLayout()

grid.addWidget(forenameLabel, 0, 0)

grid.addWidget(self.forenameEdit, 0, 1)

grid.addWidget(surnameLabel, 0, 2)

grid.addWidget(self.surnameEdit, 0, 3)

grid.addWidget(categoryLabel, 1, 0)

grid.addWidget(self.categoryComboBox, 1, 1)

grid.addWidget(companyLabel, 1, 2)

grid.addWidget(self.companyEdit, 1, 3)

grid.addWidget(addressLabel, 2, 0)

grid.addWidget(self.addressEdit, 2, 1, 1, 3)

grid.addWidget(phoneLabel, 3, 0)

grid.addWidget(self.phoneEdit, 3, 1)

grid.addWidget(mobileLabel, 3, 2)

grid.addWidget(self.mobileEdit, 3, 3)

grid.addWidget(faxLabel, 4, 0)

grid.addWidget(self.faxEdit, 4, 1)

grid.addWidget(emailLabel, 4, 2)

grid.addWidget(self.emailEdit, 4, 3)

layout = QVBoxLayout()

layout.addLayout(grid)

layout.addWidget(self.buttonBox)

self.setLayout(layout)

self.lineedits = (self.forenameEdit, self.surnameEdit,

self.companyEdit, self.phoneEdit, self.emailEdit)

for lineEdit in self.lineedits:

lineEdit.setProperty("mandatory", True)

lineEdit.textEdited.connect(self.updateUi)

self.categoryComboBox.activated.connect(self.updateUi)

self.buttonBox.accepted.connect(self.accept)

self.buttonBox.rejected.connect(self.reject)

self.setStyleSheet(ContactDlg.StyleSheet)

self.setWindowTitle("Add Contact")

def updateUi(self):

mandatory = bool(self.companyEdit.property("mandatory"))

if self.categoryComboBox.currentText() == "Business":

if not mandatory:

self.companyEdit.setProperty("mandatory", True)

elif mandatory:

self.companyEdit.setProperty("mandatory", False)

if (mandatory !=

bool(self.companyEdit.property("mandatory"))):

self.setStyleSheet(ContactDlg.StyleSheet)

enable = True

for lineEdit in self.lineedits:

if (bool(lineEdit.property("mandatory")) and

not lineEdit.text()):

enable = False

break

self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable)

if __name__ == "__main__":

app = QApplication(sys.argv)

form = ContactDlg()

form.show()

app.exec_()

運行結(jié)果:

以上這篇python3+PyQt5 自定義窗口部件--使用窗口部件樣式表的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的python做一个窗口样式_python3+PyQt5 自定义窗口部件--使用窗口部件样式表的方法...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。