03
Май
2020

Как сделать круглый label?

Хочу, чтобы QLabel (countLabel) был круглым а не квадратным.

import PyQt5
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QMainWindow, QAction, qApp
from PyQt5.QtGui import QIcon

HEIGHT = 750
WIDTH = 1300
class Menu(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.menubar = parent.menuBar()
        self.file = self.menubar.addMenu('&Опции')


        self.exitAction = QAction(QIcon('icons/cross-exit.ico'), '&Exit', self)
        self.exitAction.setShortcut('Ctrl+Q')
        self.exitAction.setStatusTip('Exit application')
        self.exitAction.triggered.connect(parent.close)

        self.file.addAction(self.exitAction)

    def addMenu(self, actionName, text, command, icon, shortcut=None, statusTip=None) :
        self.actionName = QAction(QIcon(icon), text, self)
        if shortcut:
            self.actionName.setShortcut(shortcut)
        if statusTip:
            self.actionName.setStatusTip(statusTip)
        self.actionName.triggered.connect(command)

        self.file.insertAction(self.exitAction, self.actionName)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.q = QDesktopWidget().availableGeometry()
        self.r_w, self.r_h = self.q.width(), self.q.height()
        self.setGeometry(self.r_w/2-WIDTH/2, self.r_h/2-HEIGHT/2, WIDTH, HEIGHT)
        self.w = self.size().width()
        self.h = self.size().height()
        print(self.w, self.h)
        self.setFixedSize(1300, 750)
        self.setWindowTitle('Lings')
        self.setWindowIcon(QIcon('icons/Lings.ico'))
        self.label = QtWidgets.QLabel("sdsd", self)
        self.labelAnswer = QtWidgets.QLabel(self)
        self.labelEdit = QtWidgets.QLineEdit(self)
        self.countLabel = QtWidgets.QLabel(self)
        self.l_w, self.l_h = 500, 100
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.countLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.labelEdit.setAlignment(QtCore.Qt.AlignCenter)
        self.labelAnswer.resize(self.l_w, self.l_h)
        self.countLabel.resize(50, 50)
        self.labelEdit.resize(self.l_w, self.l_h)
        self.labelAnswer.move(self.w/2-self.l_w/2, 250)
        self.countLabel.move(self.w/2-self.l_w/2+550, 275)
        self.labelEdit.move(self.w/2-self.l_w/2, 400)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.resize(self.l_w, self.l_h)
        self.label.move(self.w/2-self.l_w/2, 100)
        self.label.setFont(QtGui.QFont('Verdana', 24))
        self.labelAnswer.setFont(QtGui.QFont('Verdana', 24))
        self.labelEdit.setFont(QtGui.QFont('Verdana', 24))
        self.label.setStyleSheet("background-color: #E0FFFF;")
        self.labelAnswer.setStyleSheet("background-color: #E0FFFF")
        self.labelEdit.setStyleSheet("background-color: #7FFFD4; border: none")
        self.countLabel.setStyleSheet("background-color: red;")

        self.menu = Menu(self)
        self.menu.addMenu('action', 'New', lambda: SecondWindow(self), 'icons/test.ico', shortcut="Ctrl+D", statusTip="Creating")
        self.menu.addMenu('Eng', 'С английского на русский', lambda: SecondWindow(self), 'icons/eng-rus.ico', shortcut="Ctrl+D", statusTip="Creating")
        self.menu.addMenu('Rus', 'С русского на английский', lambda: SecondWindow(self), 'icons/rus-eng.ico', shortcut="Ctrl+D", statusTip="Creating")

        self.show()

class SecondWindow(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent, QtCore.Qt.Window)
        self.initUI()

    def initUI(self):

        self.q = QDesktopWidget().availableGeometry()
        self.r_w, self.r_h = self.q.width(), self.q.height()
        self.setGeometry(self.r_w/2-WIDTH/2, self.r_h/2-HEIGHT/2, WIDTH, HEIGHT)
        self.setFixedSize(1300, 800)
        self.setWindowTitle('Lings2')
        self.setWindowIcon(QIcon('icons/Lings.ico'))
        self.mainLayout = QtWidgets.QVBoxLayout()

        self.buttons = []
        for i in range(5):
            but = QtWidgets.QPushButton('button {}'.format(i), self)
            self.mainLayout.addWidget(but)
            self.buttons.append(but)
        self.setLayout(self.mainLayout)


        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    styleFile="styles/styles.qss"
    with open(styleFile,"r") as sf:
        app.setStyleSheet(sf.read())

    mainWindow = MainWindow()
    sys.exit(app.exec_())```

Источник: https://ru.stackoverflow.com/questions/1119691/%D0%9A%D0%B0%D0%BA-%D1%81%D0%B4%D0%B5%D0%BB%D0%B0%D1%82%D1%8C-%D0%BA%D1%80%D1%83%D0%B3%D0%BB%D1%8B%D0%B9-label

Тебе может это понравится...

Добавить комментарий