How to round the edges with QPixmap PyQt5
-
I'm trying to round the edges of the image in the field.
QPixmap
♪Ooh.
QLabel
This is done peacefullyborder-radius: Npx
And it doesn't work here.Don't tell me how I can round the picture.
QPixmap
?P.S. Needy
QPixmap
I made a comment in the code.from PyQt5.Qt import * import sys import mutagen
DEFAULT_STYLE = """
QProgressBar{
border: 2px solid grey;
border-radius: 5px;
text-align: center
}QProgressBar::chunk {
background-color: orange;
width: 10px;}
"""
sliderSheet = """QSlider::handle {
height: 10px;
background: yellow;
margin: 0 -4px;
}QSlider::add-page {
background: transparent;
}QSlider::sub-page {
background: yellow;
}"""
class Window(QWidget):
def init(self, parent = None):
QWidget.init(self, parent)self.setWindowIcon(QIcon("images/mp3-player.ico")) self.setWindowTitle("MP3-Player") self.setFixedSize(700,700) self.first_open_pixmap = QPixmap("images/start_picture.png") self.background_pixmap = QPixmap("images/background.jpg") self.background_pixmap = self.background_pixmap.scaled(1200,1200, Qt.KeepAspectRatio, Qt.FastTransformation) self.background_label = QLabel(self) self.background_label.hide() self.background_label.setPixmap(self.background_pixmap) self.first_open_label = QLabel(self) self.first_open_label.setPixmap(self.first_open_pixmap) self.central_label = QLabel(self) self.central_label.setGeometry(100,50,500,500) self.central_label.setStyleSheet("background-color: rgb(147,147,147); border-radius: 25px") self.central_label.setScaledContents(True) self.central_label.hide() self.slider = QSlider(self) self.slider.hide() self.start_progress_bar = QProgressBar(self) self.start_progress_bar.setGeometry(150,400,350,10) self.start_progress_bar.setStyleSheet(DEFAULT_STYLE) self.start_button = QPushButton(self) self.start_button.setIcon(QIcon('images/start.png')) self.start_button.setIconSize(QSize(70, 70)) self.start_button.hide() self.next_button = QPushButton(self) self.next_button.setIcon(QIcon('images/next.png')) self.next_button.setIconSize(QSize(90, 90)) self.next_button.hide() self.previous_button = QPushButton(self) self.previous_button.setIcon(QIcon('images/previous.png')) self.previous_button.setIconSize(QSize(60, 60)) self.previous_button.hide() self.package = QFileDialog(self) self.package_button = QPushButton(self) self.package_button.setIcon(QIcon('images/package.png')) self.package_button.setIconSize(QSize(60, 60)) self.package_button.hide() self.time = 0 self.new_view = False self.music_here = False self.start_stop = 0 self.value = 0 self.timer_duration = QTimer(self) self.start_timer = QTimer(self) self.start_button.clicked.connect(self.play_music) self.start_timer.timeout.connect(self.first_open_app) self.package_button.clicked.connect(self.open_package) self.next_button.clicked.connect(self.next_secs) self.previous_button.clicked.connect(self.prev_secs) self.timer_duration.timeout.connect(self.value_slider) self.first_open_app() def first_open_app(self): self.time+=1 self.setStyleSheet("background-color: rgb(94, 94, 94)") self.first_open_label.setGeometry(250,200,200,150) self.start_timer.start(5) self.start_progress_bar.setValue(self.time) self.start_progress_bar.setFormat("") if self.time == 100: self.start_timer.stop() self.prepare_main_app() def prepare_main_app(self): self.first_open_label.hide() self.start_progress_bar.hide() self.setStyleSheet("background-color: rgb(255,255,255)") self.new_view = True def paintEvent(self, event): if self.new_view == True: self.start_progress_bar.setGeometry(150 ,380 , 375,15 ) self.central_label.show() self.start_button.show() self.start_button.setGeometry(160,635 , 60,60) self.start_button.setStyleSheet("background-color: rgb(147,147,147); \ border-radius: 15px") self.next_button.show() self.next_button.setGeometry(90, 635 , 60, 60) self.next_button.setStyleSheet("background-color: rgb(147,147,147); \ border-radius: 15px") self.previous_button.show() self.previous_button.setGeometry(230, 635 , 60, 60) self.previous_button.setStyleSheet("background-color: rgb(147,147,147); \ border-radius: 15px") self.package_button.show() self.package_button.setGeometry(300, 635, 60, 60) self.package_button.setStyleSheet("background-color: rgb(97, 97, 97); \ border-radius: 20px;") self.slider.show() self.slider.setOrientation(Qt.Horizontal) self.slider.setGeometry(100,620, 500,5) self.slider.setStyleSheet(sliderSheet) self.background_label.show() self.background_label.setGeometry(0,0,700,700) def play_music(self): if self.music_here == True: self.start_stop+=1 if self.start_stop%2 == 0: self.start_button.setIcon(QIcon('images/start.png')) self.start_button.setIconSize(QSize(70, 70)) self.player.pause() self.timer_duration.stop() if self.start_stop%2 == 1: self.start_button.setIcon(QIcon('images/stop.png')) self.start_button.setIconSize(QSize(70, 70)) self.player.play() self.timer_duration.start(1000) def value_slider(self): self.slider.setRange(0, self.player.duration()//1000) self.current_value = self.slider.value() self.value+=1 self.slider.setValue(self.value) if self.slider.value() >= self.player.duration()//1000: self.value = 0 self.start_stop = 0 self.slider.setValue(0) self.player.setPosition(0) self.player.stop() self.timer_duration.stop() self.start_button.setIcon(QIcon('images/start.png')) self.start_button.setIconSize(QSize(70, 70)) def prev_secs(self): if self.slider.value() > 0: self.player.setPosition(self.player.position() + 10000) self.slider.setValue(self.slider.value() + 10) self.value+=10 def next_secs(self): if self.slider.value() > 10: self.player.setPosition(self.player.position() - 10000) self.slider.setValue(self.slider.value() - 10) self.value -= 10 if self.slider.value() <=10 and self.slider.value()> 0: self.player.setPosition(0) self.slider.setValue(0) self.value = 0 def open_package(self): self.name = QUrl(self.package.getOpenFileUrl()[0]) if self.name != QUrl(""): self.music_here = True self.content = QMediaContent(self.name) self.player = QMediaPlayer() self.player.setMedia(self.content) pixlist = self.saveMP3pix(self.name) self.saveMP3tags(self.name) try: pixmap = QPixmap(pixlist[0]) #<Речь идет об этой части кода self.central_label.setPixmap(pixmap) # except: pass def saveMP3pix(self, filename): filename = filename.toLocalFile() mp3 = mutagen.File(filename) allpix = [] for tag in mp3.items(): if tag[0][:4] == 'APIC': pic = tag[1] new_name = f'my_pic_{len(allpix)}.' + str(pic.mime.split('/')[1]) file = open(new_name, "wb") file.write(pic.data) file.close() allpix.append(new_name) return allpix def saveMP3tags(self, filename): filename = filename.toLocalFile() for i in range(1,len(filename)+1): if filename[-i] == "/": self.name = filename[-i+1:-4] self.directory_name = filename[:-i+1] break
if name == "main":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
-
As an option:
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QWidget):
def init(self):
super().init()pixmap = QtGui.QPixmap('lena.jpg') pixmap.fill(QtCore.Qt.transparent) w = pixmap.size().width() h = pixmap.size().height() clipPath = QtGui.QPainterPath() clipPath.addRoundedRect(QtCore.QRectF(0, 0, w, h), w//2, h//2) qp = QtGui.QPainter(pixmap) qp.setClipPath(clipPath) qp.drawPixmap(0, 0, QtGui.QPixmap('lena.jpg')) qp.end() self.label = QtWidgets.QLabel() self.label.setPixmap(pixmap) layout = QtWidgets.QHBoxLayout(self) layout.addWidget(self.label, alignment = QtCore.Qt.AlignCenter) lbl = QtWidgets.QLabel() lbl.setPixmap(QtGui.QPixmap('lena.jpg')) layout.addWidget(lbl, alignment = QtCore.Qt.AlignCenter)
if name == "main":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())