Answer the question
In order to leave comments, you need to log in
How to make a freeform button in PyQt5?
Good afternoon everyone. I'm learning PyQt5 from Prokhorenko's book, I wanted to make an oval-shaped button and ran into a problem. I rummaged through the Internet, found how to insert an image, but the button frame is still rectangular. The book describes how to create a free-form window using an image, but I can't use setBrush to set the button's background from the image. How to do it? I attached a code that I try to display an image as a background, but nothing comes out, I can’t understand why =(
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window. resize(200, 200)
btn = QtWidgets.QPushButton('x',window)
b_pal = btn.palette()
b_pal.setBrush(QtGui.QPalette.Normal, QtGui.QPalette.Button,
QtGui.QBrush(QtGui.QPixmap("img.jpg")))
btn.setPalette(b_pal)
btn.move(50,50)
window.show( )
sys.exit(app.exec_())
Answer the question
In order to leave comments, you need to log in
I haven't learned from books, and perhaps this approach will be slightly incorrect from the point of view of a full-fledged study of Pyqt. If I understand correctly, you need something like
an option to use qss styles. The button above is an example. Positioning through Qh/Qv layouts. Here is the code for such a button:
collate = QPushButton("Collate")
# Style sheet for button
collate.setStyleSheet("QPushButton {background-color: rgb(51,122,183); color: White; border-radius: 4px;}"
"QPushButton:pressed {background-color:rgb(31,101,163) ; }")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question