Сделать вокруг кнопки ободок
Есть приложение, проблема в том, что, я хочу сделать вокруг кнопки ободок, к примеру красного цвета (типа за кнопкой нарисовать прямоугольник немного больше кнопки), но я не понимаю какие команды в PyQt5 нужны чтобы рисовать прямоугольники.
Подскажите пожалуйста.
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QVBoxLayout,
QHBoxLayout, QPushButton, QMessageBox
from random import*
app = QApplication([])
main_win = QWidget()
main_win.setWindowTitle('Fast Clicker')
main_win.resize(800,500)
main_win.setStyleSheet("background-color:rgb(100,200, 255)")
button1 = QPushButton()
button2 = QPushButton()
button3 = QPushButton()
button4 = QPushButton()
button1.setStyleSheet("background-color: yellow")
button2.setStyleSheet("background-color: yellow")
button3.setStyleSheet("background-color: yellow")
button4.setStyleSheet("background-color: yellow")
button1.setFixedSize(100,125)
button2.setFixedSize(100,125)
button3.setFixedSize(100,125)
button4.setFixedSize(100,125)
line = QVBoxLayout()
lineH1 = QHBoxLayout()
lineH1.addWidget(button1, alignment = Qt.AlignCenter)
lineH1.addWidget(button2, alignment = Qt.AlignCenter)
lineH1.addWidget(button3, alignment = Qt.AlignCenter)
lineH1.addWidget(button4, alignment = Qt.AlignCenter)
main_win.setLayout(line)
line.addLayout(lineH1)
main_win.show()
app.exec()
Ответы (2 шт):
Автор решения: S. Nick
→ Ссылка
Чаще всего используют StyleSheet
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, \
QLabel, QVBoxLayout, QHBoxLayout, QPushButton, QMessageBox
from random import*
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Fast Clicker')
self.resize(800,500)
button1 = QPushButton()
button2 = QPushButton()
button3 = QPushButton()
button4 = QPushButton()
h_layout = QHBoxLayout()
h_layout.addWidget(button1)
h_layout.addWidget(button2)
h_layout.addWidget(button3)
h_layout.addWidget(button4)
main_layout = QVBoxLayout(self)
main_layout.addLayout(h_layout)
Stylesheet = '''
QWidget {
background-color:rgb(100,200, 255);
}
QPushButton {
background-color: #D98C00;
min-width: 96px;
max-width: 96px;
min-height: 96px;
max-height: 96px;
border-radius: 48px; /* круглый */
border: 2px solid #09009B;
}
QPushButton:hover:pressed {
background-color: red;
}
QPushButton:hover {
background-color: #0ff;
border: 2px solid #FFA6D5;
}
'''
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
app.setStyleSheet(Stylesheet)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
Автор решения: Александр
→ Ссылка
CSS стиль
import sys
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout
from PySide6.QtCore import QSize
custom_button = """
QPushButton#Button {
transition-duration: 0.1s;
background-color: white;
color: black;
border: 5px solid #4CAF50;
}
QPushButton#Button:hover {
background-color: lightgreen; /* Green */
color: black;
}
"""
class ExampleCustomButton(QWidget):
def __init__(self):
super().__init__()
self.resize(QSize(180, 120))
self.setObjectName("MainWindow")
box = QHBoxLayout()
self.setLayout(box)
self.button_print_message = QPushButton("Напечатать сообщение", objectName="Button")
self.button_print_message.clicked.connect(self.button_print_message_click_handler)
self.button_print_message.setStyleSheet(custom_button)
box.addWidget(self.button_print_message)
def button_print_message_click_handler(self):
print('Hello, World!')
if __name__ == '__main__':
app = QApplication(sys.argv)
ecb = ExampleCustomButton()
ecb.show()
sys.exit(app.exec_())
