英文:
PyQt5 label won't wrap text
问题
I understand that you want a translation of the code and text, excluding the code parts. Here is the translation of the text:
"所以我试图创建一个文本换行的按钮,我已经查看了多篇帖子,希望能实现这个...
首先,我使用了来自这篇帖子的自定义PushButton,但标签不会换行。
这篇帖子有一个回答描述了两种制作带换行的标签的方法,但都对我不起作用。
我的代码看起来像这样:
如下所示,按钮位于网格中,分配给标签的文本根本不会换行。我该如何修复这个问题,而不让按钮扩展?我做错了什么吗?
英文:
So I'm trying to make a button with text that wraps, and I've looked into multiple posts in hope of achieving this...
Firstly, I've used the custom PushButton with a text label from this post, but the label won't text wrap.
This post has an answer describing two ways to make a label with word wrapping, and none of them work for me.
My code looks like this:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
container=QWidget()
self.buttonGridLayout=QGridLayout()
self.boardButtons=dict()
for x in range(5):
for y in range(5):
self.boardButtons[(x,y)]=RichTextButton(f"wow, this is such a very long string, i hope nothing bad happens")
self.boardButtons[(x,y)].setFixedSize(150,150)
self.buttonGridLayout.addWidget(self.boardButtons[(x,y)], y,x)
container.setLayout(self.buttonGridLayout)
self.setCentralWidget(container)
class RichTextButton(QPushButton):
def __init__(self, parent=None):
QPushButton.__init__(self, parent)
self.UnitText = QLabel(self)
self.UnitText.setTextInteractionFlags(Qt.NoTextInteraction)
self.UnitText.setAlignment(Qt.AlignCenter)
self.UnitText.setMouseTracking(False)
self.setLayout(QVBoxLayout())
self.layout().setContentsMargins(0,0,0,0)
self.layout().addWidget(self.UnitText)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
As you can see, the buttons are in a grid, and the text assigned to the labels don't wrap at all. How can I fix this without having the buttons expand? Am I doing something wrong?
答案1
得分: 1
你没有将文本分配给你在自定义按钮中创建的标签。
对于你的 RichTextButton
,签名只接受一个关键字参数 parent
,然后将其传递给超类构造函数。
但是当你实际在主窗口中定义 RichTextButton
实例时,你传递的是本应用于标签的文本。因此,你的自定义按钮将该文本传递给超类构造函数,它被分配为标准 QPushButton
的文本,而你的 QLabel
保持为空。
你可以扩展你的自定义按钮的签名,以接受按钮的文本,并确保 QLabel 的构造函数接收到该文本。
例如:
class RichTextButton(QPushButton):
def __init__(self, text, parent=None): # 修改签名以允许文本
QPushButton.__init__(self, parent)
self.UnitText = QLabel(text, self) # 将文本传递给标签
self.UnitText.setTextInteractionFlags(Qt.NoTextInteraction)
self.UnitText.setWordWrap(True) # 为标签设置换行
self.UnitText.setAlignment(Qt.AlignCenter)
self.UnitText.setMouseTracking(False)
self.setLayout(QVBoxLayout())
self.layout().setContentsMargins(0,0,0,0)
self.layout().addWidget(self.UnitText)
我还在 QLabel
上设置了 setWordWrap(True)
。
英文:
You are not assigning the text to the label you are creating in your custom button.
The signature for your RichTextButton
only accepts one keyword parameter, parent
and then passes it to the super constructor.
But when you actually define the RichTextButton
instance in your main window you are passing it the text that is meant for the label. So your custom button is passing that text to the super constructor and it is being assigned as if it was the text for a standard QPushButton
and your QLabel
remains empty.
What you can do is expand the signature for your custom button to accept the text for the button as well and then ensure that the constructor for the QLabel receives the text in it's constructor.
For example:
class RichTextButton(QPushButton):
def __init__(self, text, parent=None): # change signature to allow text
QPushButton.__init__(self, parent)
self.UnitText = QLabel(text, self) # pass the text for the label
self.UnitText.setTextInteractionFlags(Qt.NoTextInteraction)
self.UnitText.setWordWrap(True) # set word wrap for label
self.UnitText.setAlignment(Qt.AlignCenter)
self.UnitText.setMouseTracking(False)
self.setLayout(QVBoxLayout())
self.layout().setContentsMargins(0,0,0,0)
self.layout().addWidget(self.UnitText)
I also set setWordWrap(True)
on the QLabel
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论