您好,登錄后才能下訂單哦!
這篇文章主要介紹python3+PyQt5如何自定義窗口部件,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
以下類似于css:
StyleSheet = """ QComboBox { color: darkblue; } QLineEdit { color: darkgreen; } QLineEdit[mandatory="true"] { #mandatory="true"時(shí),QLineEdit的樣式會(huì)變化 background-color: rgb(255, 255, 127); color: darkblue; }
如果在選擇器的前面加上一個(gè)句點(diǎn),比如.QLineEdit,則選擇器就會(huì)只應(yīng)用于指定的類,而不會(huì)應(yīng)用于這個(gè)類的子類。如果要求選擇器僅用于某一特定窗口部件,則可以對(duì)該窗口部件調(diào)用setObjectName(),然后用該名字作為選擇器的一部分。比如,如果有一個(gè)按鈕,其對(duì)象名字是“findButton”,則應(yīng)用于這個(gè)按鈕的選擇器就應(yīng)該是QpushButton#findButton。有些窗口部件會(huì)有一些子控件。例如QComboBox會(huì)有一個(gè)箭頭子控件,用戶通過點(diǎn)擊這個(gè)箭頭來看到下拉列表。子控件可以指定為選擇器的一部分–例如,QComboBox::drop-down。偽狀態(tài)可以用一個(gè)冒號(hào)指定–例如,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_()
運(yùn)行結(jié)果:
以上是“python3+PyQt5如何自定義窗口部件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。