سؤال

I have written the code below:

import sys
from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.central_widget = QtGui.QStackedWidget()
        self.setCentralWidget(self.central_widget)
        Login_Widget = LoginPage(self)
        self.central_widget.addWidget(Login_Widget)
        self.central_widget.setCurrentWidget(Login_Widget)
        self.setStyleSheet("background-color:#FFDA00;")

class LoginPage(QtGui.QWidget):
    def __init__(self, parent=None):
        super(LoginPage, self).__init__(parent)
        self.Username = QtGui.QLineEdit(self)  
        self.Password = QtGui.QLineEdit(self)
        self.Password.setEchoMode(QtGui.QLineEdit.Password)
        self.buttonLogin = QtGui.QPushButton('Login', self)
        self.cancelButton = QtGui.QPushButton('Cancel', self)
        loginLayout = QtGui.QFormLayout()
        loginLayout.addRow("Username", self.Username) 
        loginLayout.addRow("Password", self.Password)
        horizontallayout = QtGui.QHBoxLayout()
        horizontallayout.addWidget(self.buttonLogin)
        horizontallayout.addWidget(self.cancelButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addLayout(loginLayout)
        layout.addLayout(horizontallayout)
        self.setLayout(layout)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

With the code above, When I run the code, it sets everything orange. I wanted to set the background orange, the lineedit white and the buttons silvery-grey. How would I set different colors for individual widget items?? Also is there any way that I could set a colour for the Window bar (the bar containing the window-title, exit button, minimize button, and re-size button)

Any help will be much appreciated!

هل كانت مفيدة؟

المحلول

You can set different style properties for individual widgets.

Have a look at this link Qt Style Sheets Examples as it cover how to set different style properties for most of the widgets.

You can also save the stylesheeet as .qss file and save it externally.

css = '''

QMainWindow
{
background:orange;
}   

QLineEdit
{
background:white;
}

QPushButton
{
background:qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                          stop: 0 #FAFFFA, stop: 0.4 #F5F7F5,
                          stop: 0.5 #F0F2F0, stop: 1.0 #EDEDED); 
}
'''

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.central_widget = QtGui.QStackedWidget()
        self.setCentralWidget(self.central_widget)
        Login_Widget = LoginPage(self)
        self.central_widget.addWidget(Login_Widget)
        self.central_widget.setCurrentWidget(Login_Widget)

class LoginPage(QtGui.QWidget):
    def __init__(self, parent=None):
        super(LoginPage, self).__init__(parent)
        self.Username = QtGui.QLineEdit(self)  
        self.Password = QtGui.QLineEdit(self)
        self.Password.setEchoMode(QtGui.QLineEdit.Password)
        self.buttonLogin = QtGui.QPushButton('Login', self)
        self.cancelButton = QtGui.QPushButton('Cancel', self)
        loginLayout = QtGui.QFormLayout()
        loginLayout.addRow("Username", self.Username) 
        loginLayout.addRow("Password", self.Password)
        horizontallayout = QtGui.QHBoxLayout()
        horizontallayout.addWidget(self.buttonLogin)
        horizontallayout.addWidget(self.cancelButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addLayout(loginLayout)
        layout.addLayout(horizontallayout)
        self.setLayout(layout)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setStyleSheet(css)#<------------set your stylesheet
    window = MainWindow()
    window.show()
    app.exec_()

As for the window title bar, it is not possible to set color or any properties. Your best bet is to hide it and implement your own window title bar.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top