[英]Change design with Pyside2 - Python3
我使用Python 3.7和Pyside2。
我想更改顏色,字體,背景顏色...但是我不能!
我導入QtGui進行設計,但是出現相同的錯誤'Window' object has no attribute 'setBrush'
from PySide2.QtGui import QColor, QBrush, QPainterPath, QFont
from PySide2.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox, QDesktopWidget
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Convertisseur de devises")
self.setGeometry(700,300,700,300)
self.setBrush(QColor(255, 0, 0, 127))
self.setButton()
self.center()
def setButton(self):
btn = QPushButton("Inverser Devises", self)
btn.move(550,135)
def center(self):
qRect = self.frameGeometry()
centerPoint = QDesktopWidget().availableGeometry().center()
qRect.moveCenter(centerPoint)
self.move(qRect.topLeft())
myapp = QApplication(sys.argv)
window = Window()
window.show()
myapp.exec_()
sys.exit()
舉個例子 :
謝謝您的幫助
無需更改Painter,只需使用樣式表即可。 Qt樣式表使用CSS語法,可以輕松地重用於多個小部件。 此處提供更多信息: https : //doc.qt.io/qt-5/stylesheet-syntax.html
例如,您可以替換
self.setBrush(QColor(255, 0, 0, 127))
與
self.setStyleSheet('background-color: rgb(0, 0, 127)')
將背景色更改為藍色。
為了使其可重用,盡管將樣式表放入單獨的文件中也很有意義。 將樣式表與Python文件放在同一文件夾中。
style.qss:
QWidget {
background-color: rgb(0, 0, 127);
}
QPushButton {
border: none;
color: white;
}
然后更換
self.setBrush(QColor(255, 0, 0, 127))
與
# This gets the folder the Python file is in and creates the path for the stylesheet
stylesheet_path = os.path.join(os.path.dirname(__file__), 'style.qss')
with open(stylesheet_path, 'r') as f:
self.setStyleSheet(f.read())
並且由於您在父窗口小部件上設置了樣式,因此所有子窗口小部件(包括您的Button)也將具有相同的樣式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.