RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
python3+PyQt5如何自定义窗口部件-创新互联

这篇文章主要介绍python3+PyQt5如何自定义窗口部件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

阿克塞哈萨克族自治网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。创新互联公司自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

以下类似于css:

 StyleSheet = """
QComboBox { color: darkblue; }
QLineEdit { color: darkgreen; }
QLineEdit[mandatory="true"] { #mandatory="true"时,QLineEdit的样式会变化
 background-color: rgb(255, 255, 127);
 color: darkblue;
}

如果在选择器的前面加上一个句点,比如.QLineEdit,则选择器就会只应用于指定的类,而不会应用于这个类的子类。如果要求选择器仅用于某一特定窗口部件,则可以对该窗口部件调用setObjectName(),然后用该名字作为选择器的一部分。比如,如果有一个按钮,其对象名字是“findButton”,则应用于这个按钮的选择器就应该是QpushButton#findButton。有些窗口部件会有一些子控件。例如QComboBox会有一个箭头子控件,用户通过点击这个箭头来看到下拉列表。子控件可以指定为选择器的一部分–例如,QComboBox::drop-down。伪状态可以用一个冒号指定–例如,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_()

运行结果:

python3+PyQt5如何自定义窗口部件

以上是“python3+PyQt5如何自定义窗口部件”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前名称:python3+PyQt5如何自定义窗口部件-创新互联
链接URL:http://ncwzjz.com/article/decdso.html