簡體   English   中英

小部件未在 pyqt5 中顯示標簽

[英]Widget not displaying label in pyqt5

我想在 pyqt5 應用程序中顯示一個帶有簡單消息的窗口。 為此,我創建了一個簡單的小部件、一個布局和一個標簽。 我將標簽添加到布局並將布局分配給小部件,如下所示:

app = QApplication(sys.argv)
# Create a widget to store information
load_widget = QWidget()
# Create a label with the message to display
load_label = QLabel()
# Set label text
load_label.setText('Data being imported, please wait...')
# Create a layout for the widget
load_layout = QVBoxLayout()
# Add label to the layout
load_layout.addWidget(load_label)
# Set layout for the widget
load_widget.setLayout(load_layout)
# Set window title
load_widget.setWindowTitle('Read SD')
# Show the widget
load_widget.show()
sys.exit(app.exec_())

但是,窗口與其標題一起顯示,但沒有顯示標簽,如下圖所示。

在此處輸入圖片說明

正如@eyllanesc 所說,這不是 MRE,因為如果復制/粘貼它並運行它,他們會得到:

QWidget: Must construct a QApplication before a QWidget 

我已經擴展了你的代碼並正確格式化了它,它工作得很好

from sys import exit as sysExit

from PyQt5.QtCore import *
from PyQt5.QtGui  import *
from PyQt5.QtWidgets import *

# Outline a MainWindow Class using a QWidget
class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)

      # Set window title
        self.setWindowTitle('Read SD')

      # Create a label with the message to display
      # And and attach it to the MainWindow 
        self.lblLoad = QLabel()
      # Set label text
        self.lblLoad.setText('Data being imported, please wait...')
      # Create a layout for the widget
        VBox = QVBoxLayout()
      # Add label to the layout
        VBox.addWidget(self.lblLoad)
      # Add Stretch so Label stays at the top
        VBox.addStretch(1)
      # Set layout for the widget
        self.setLayout(VBox)

if __name__ == "__main__":
  # Create Main Thread to Run Program In
    MainThred = QApplication([])
  # Create Main Window from Outlined Class
    MainGUI = MainWindow()
  # Show the Main Window
    MainGUI.show()
  # Launch the Main Thread
    sysExit(MainThred.exec_())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM