Styling QLineEdit and QPushButton with PyQt Style Sheets

Posted by


PyQt is a set of Python bindings for the Qt application framework developed by Riverbank Computing. It allows Python developers to create desktop applications with Qt GUI components. One of the key features of PyQt is the ability to style widgets using style sheets.

In this tutorial, we will look at how to style the QLineEdit and QPushButton widgets using style sheets in PyQt.

  1. Setting up PyQt:
    First, you need to install PyQt on your system. You can do this using pip by running the following command:
pip install pyqt5

After installing PyQt, you can create a new Python script and import the required modules:

from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton
  1. Styling the QLineEdit widget:
    The QLineEdit widget is used to get input from the user. You can style the QLineEdit widget using style sheets to change its appearance. Here is an example of how to create a styled QLineEdit widget:
app = QApplication([])
window = QWidget()
line_edit = QLineEdit()
line_edit.setStyleSheet("QLineEdit { background-color: #f0f0f0; color: #333; border: 1px solid #ccc; padding: 5px; }")

In this example, we have set the background color to a light grey (#f0f0f0), the text color to dark grey (#333), and the border to a 1px solid grey (#ccc). We have also added some padding to the widget to make it look nicer.

  1. Styling the QPushButton widget:
    The QPushButton widget is used to create a clickable button. You can style the QPushButton widget using style sheets to change its appearance. Here is an example of how to create a styled QPushButton widget:
button = QPushButton("Click me")
button.setStyleSheet("QPushButton { background-color: #007bff; color: white; border-radius: 5px; padding: 10px; }")

In this example, we have set the background color to a blue color (#007bff), the text color to white, and added a border radius to give the button rounded corners. We have also added some padding to the button to make it look nicer.

  1. Putting it all together:
    Now that we have created and styled the QLineEdit and QPushButton widgets, we can add them to the window and show it:
window.setLayout(QVBoxLayout())
window.layout().addWidget(line_edit)
window.layout().addWidget(button)
window.show()

This will create a window with a styled QLineEdit widget and a styled QPushButton widget. You can experiment with different style sheet properties to customize the appearance of the widgets further.

In this tutorial, we have looked at how to style the QLineEdit and QPushButton widgets using style sheets in PyQt. PyQt style sheets offer a powerful way to customize the appearance of widgets and create visually appealing desktop applications. Experiment with different style properties to create a unique look for your PyQt applications.

0 0 votes
Article Rating

Leave a Reply

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@somethingamongthebytes9228
7 hours ago

Thank you so much for sharing your knowledge! Really appreciate it! 🎉

@soniosonio
7 hours ago

good video

2
0
Would love your thoughts, please comment.x
()
x