I
I
I3EKTOP2019-03-04 11:30:45
Python
I3EKTOP, 2019-03-04 11:30:45

How to wrap text on buttons in PyQt5?

Good afternoon everyone! I ran into such a problem: I have buttons, in which I want to display sentences from a different number of words (2-10), now this sentence is displayed in one line and the words are not wrapped. How to make automatic hyphenation, perhaps by space? Is it possible to automatically adjust the text size based on the amount of text? Tried to replace spaces with ]\n, but in this case it is displayed as text and doesn't wrap. I tried to use some CSS stuff, but none of them helped me, although maybe I used the wrong one. In general, how to implement it? Thank you very much in advance for your answer!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
bbkmzzzz, 2019-03-04
@I3EKTOP

2 options
1: Add a layout to the button, to the label layout, call setWordWrap(True) on the label (just a tip from stackoverflow)
2: Draw the label on top of the button
In any case, the label

A
Andrey_Dolg, 2019-03-05
@Andrey_Dolg

From the bad advice section.
Option number 1 do instead of button QLabel

class QLabel_alterada(QLabel):
    clicked=pyqtSignal()
    def __init__(self, parent=None):
        QLabel.__init__(self, parent)

    def mousePressEvent(self, ev):
        self.clicked.emit()

option number 2:
Mini perversion auto-generate an icon with text and send it to the button.

D
Dmitry Shitskov, 2019-03-04
@Zarom

Try using the first or second answer.
https://stackoverflow.com/questions/8969086/is-the...
Of course, a lot of text on a button is not a good design move. It's better to put all the text in a label next to the button.

W
WALL_E, 2019-09-26
@WALL_E

Use \n to wrap text! =D

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question