問題描述
我正在學習如何使用 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模板網!