<bdo id='JB7i6'></bdo><ul id='JB7i6'></ul>
  1. <i id='JB7i6'><tr id='JB7i6'><dt id='JB7i6'><q id='JB7i6'><span id='JB7i6'><b id='JB7i6'><form id='JB7i6'><ins id='JB7i6'></ins><ul id='JB7i6'></ul><sub id='JB7i6'></sub></form><legend id='JB7i6'></legend><bdo id='JB7i6'><pre id='JB7i6'><center id='JB7i6'></center></pre></bdo></b><th id='JB7i6'></th></span></q></dt></tr></i><div id='JB7i6'><tfoot id='JB7i6'></tfoot><dl id='JB7i6'><fieldset id='JB7i6'></fieldset></dl></div>

    1. <legend id='JB7i6'><style id='JB7i6'><dir id='JB7i6'><q id='JB7i6'></q></dir></style></legend>

    2. <small id='JB7i6'></small><noframes id='JB7i6'>

    3. <tfoot id='JB7i6'></tfoot>

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

      时间:2023-08-05

      <small id='NDdBV'></small><noframes id='NDdBV'>

      <i id='NDdBV'><tr id='NDdBV'><dt id='NDdBV'><q id='NDdBV'><span id='NDdBV'><b id='NDdBV'><form id='NDdBV'><ins id='NDdBV'></ins><ul id='NDdBV'></ul><sub id='NDdBV'></sub></form><legend id='NDdBV'></legend><bdo id='NDdBV'><pre id='NDdBV'><center id='NDdBV'></center></pre></bdo></b><th id='NDdBV'></th></span></q></dt></tr></i><div id='NDdBV'><tfoot id='NDdBV'></tfoot><dl id='NDdBV'><fieldset id='NDdBV'></fieldset></dl></div>
        <bdo id='NDdBV'></bdo><ul id='NDdBV'></ul>

      • <legend id='NDdBV'><style id='NDdBV'><dir id='NDdBV'><q id='NDdBV'></q></dir></style></legend>

            <tbody id='NDdBV'></tbody>

                <tfoot id='NDdBV'></tfoot>
                本文介绍了为什么标签没有完全显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在学习如何使用 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)
                
                    # ...
                

                这篇关于为什么标签没有完全显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:PyQt 对齐复选框并将其放在每一行 下一篇:使用 pyinstaller 将 pyqt 构建为 exe 后文件大小较大

                相关文章

                最新文章

                <legend id='ACtJy'><style id='ACtJy'><dir id='ACtJy'><q id='ACtJy'></q></dir></style></legend>

                  <bdo id='ACtJy'></bdo><ul id='ACtJy'></ul>

                <small id='ACtJy'></small><noframes id='ACtJy'>

                  <i id='ACtJy'><tr id='ACtJy'><dt id='ACtJy'><q id='ACtJy'><span id='ACtJy'><b id='ACtJy'><form id='ACtJy'><ins id='ACtJy'></ins><ul id='ACtJy'></ul><sub id='ACtJy'></sub></form><legend id='ACtJy'></legend><bdo id='ACtJy'><pre id='ACtJy'><center id='ACtJy'></center></pre></bdo></b><th id='ACtJy'></th></span></q></dt></tr></i><div id='ACtJy'><tfoot id='ACtJy'></tfoot><dl id='ACtJy'><fieldset id='ACtJy'></fieldset></dl></div>
                  <tfoot id='ACtJy'></tfoot>