小编Imr*_*nti的帖子

如何在 PyQt5 的 QWidget 中使用 statusBar().showMessage()?

我正在尝试学习 PyQt5/Python 3.6.3。我试图在单击按钮时在状态栏中显示一条消息。问题是上述按钮位于 QWidget 内,据我所知,statusBar() 仅在 QMainWindow 中可用。这是我到目前为止拼凑的代码......

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets  import QStatusBar, QMainWindow, QApplication, QWidget,QHBoxLayout, QVBoxLayout, QPushButton, QSlider, QLCDNumber, QLabel

class MyMainWindow(QMainWindow):

    def __init__(self, parent=None):
        super().__init__()
        self.main_widget = FormWidget(self)
        self.setCentralWidget(self.main_widget)
        self.init_UI()

    def init_UI(self):
        self.statusBar().showMessage('Ready')
        self.setGeometry(200, 100, 300, 300)
        self.setWindowTitle('Central Widget')
        self.show()

class FormWidget(QWidget):

    def __init__(self, parent):
        super(FormWidget, self).__init__(parent)
        self.init_UI()

    def init_UI(self):
        hbox = QHBoxLayout()
        button_1 = QPushButton('Button 1', self)
        button_1.clicked.connect(self.buttonClicked)
        hbox.addWidget(button_1)
        button_2 = QPushButton('Button 2', self)
        button_2.clicked.connect(self.buttonClicked)
        hbox.addWidget(button_2)
        self.setLayout(hbox)
        self.setGeometry(200, 100, 300, 300)
        self.setWindowTitle('Slider …
Run Code Online (Sandbox Code Playgroud)

python pyqt5

2
推荐指数
1
解决办法
6745
查看次数

标签 统计

pyqt5 ×1

python ×1