为什么标签没有完全显示?

2022-01-12 00:00:00 python pyqt pyqt5 qlabel

问题描述

我正在学习如何使用 PyQt5,但遇到了我的第一个标签"未在我的屏幕上完全显示的问题.

I am learning how to use PyQt5 and I came across this issue where "my first label" does not complete display on my screen.

运行代码后显示:

代码:

from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui  import *
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys

QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True) #enable highdpi scaling
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True) #use highdpi icons

def window():
  app = QApplication(sys.argv)
  win = QMainWindow()
  win = QMainWindow()
  win.setGeometry(200, 200, 400, 400)
  win.setWindowTitle("Tech with Aeijan")
  label = QtWidgets.QLabel(win)
  label.setText("my first label!")
  label.move(50,50)

  win.show()
  sys.exit(app.exec_())

window()


解决方案

QLabel 根据(可能的)父布局管理器来适配它的内容,但是你没有使用任何,所以它不知道如何正确显示自己或调整它的大小来做到这一点.

QLabel adapts its contents based on the (possible) parent layout manager, but you didn't use any, so it doesn't know how to correctly display itself or adapt its size to do that.

最简单的解决方案是调用label.adjustSize(),这将导致标签自身调整大小,以便能够显示其内容.

The simplest solution is to call label.adjustSize(), which will cause the label to resize itself so that it will be able to display its contents.

不过,这不是一个好主意:您正在尝试为小部件使用固定位置(由于很多原因,这通常被认为是一件坏事);结果将是,如果标签文本太大并且用户调整窗口大小,文本将不会完全可见,标签也不知道如何调整大小或最终包装其内容以确保所有显示其文本.

That wouldn't be a very good idea, though: you are trying to use a fixed position for a widget (which is normally considered a bad thing to do, for plenty of reasons); the result will be that if the label text is too big and the user resizes the window, the text won't be completely visible as it should be, nor the label would know how to resize or eventually wrap its contents to do ensure that all its text is shown.

更好的方法是使用布局管理器,但这是为更简单的小部件(如 QWidget 或 QDialog)保留的解决方案;QMainWindow 不能那样工作,并且它需要设置一个中央小部件以确保其内容正确显示和管理.

The better approach is to use a layout manager, but that is a solution reserved for simpler widgets (like a QWidget or a QDialog); a QMainWindow doesn't work like that, and it requires a central widget to be set to ensure that its contents are correctly displayed and managed.

在您的情况下,您可以简单地使用 self.setCentralWidget(label),但这会阻止您将任何其他小部件添加到您的窗口.

In your case, you could simply use self.setCentralWidget(label), but that would prevent you to add any other widget to your window.

应该使用容器"小部件,并且该小部件将设置为主窗口的中心;然后您可以为该小部件设置布局并为其添加标签:

A "container" widget should be used instead, and that widget would be set as the central one for the main window; then you can set a layout for that widget and add the label to it:

def window():
    app = QApplication(sys.argv)
    win = QMainWindow()

    central = QWidget()
    win.setCentralWidget(central)

    layout = QVBoxLayout()
    central.setLayout(layout)
    # alternatively, the above is the same as this:
    # layout = QVBoxLayout(central)

    label = QtWidgets.QLabel(win)
    label.setText("my first label!")
    layout.addWidget(label)

    # ...

相关文章