更改按钮cli上的Pyqt状态栏文本

2024-03-29 07:37:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我不能让我的程序改变状态栏的文本按钮点击。我一直在

上的"TypeError: argument 1 has unexpected type 'NoneType'"错误self.closeButton.clicked.连接(自我过程('文本')'。 我不知道该怎么办了

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit, 
QPushButton
from PyQt5.QtGui import QIcon


class App(QMainWindow):

def process(self):
    self.statusBar.showMessage('online')

def __init__(self):
    super().__init__()
    self.title = 'Red Queen v0.4'
    self.initUI()

def initUI(self):
    self.setWindowTitle(self.title)
    self.statusBar().showMessage('Offline')
    self.showMaximized()
    self.setStyleSheet("background-color: #FFFFFF;")
    self.textbox = QLineEdit(self)
    self.textbox.move(500, 300)
    self.textbox.resize(350, 20)
    self.textbox.setStyleSheet("border: 3px solid red;")
    self.setWindowIcon(QIcon('Samaritan.png'))
    text = QLineEdit.text(self.textbox)
    self.closeButton = QPushButton('process', self)
    self.closeButton.clicked.connect(self.process('text'))
    self.closeButton.show()
    self.show()
    self.textbox.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

Tags: textfrom文本importselfdefshowsys
2条回答

换行:

    self.closeButton.clicked.connect(self.process('text'))

^{pr2}$

您需要将函数本身作为参数传递,而不是函数调用的结果(因为您的方法不包含return语句,self.process()返回{})。在

如果您想让process方法接受一个参数,您必须首先按照Avión的建议对其进行更改:

def process(self, text):
    self.statusBar.showMessage(text)

但将连接到点击信号的线路改为:

    self.closeButton.clicked.connect(lambda: self.process('offline'))

需要lambda表达式将可调用对象传递给connect()。在

process函数更改为:

def process(self, text):
    self.statusBar.showMessage(text)

现在当你调用函数时 self.closeButton.clicked.connect(self.process('text'))它将接收文本并打印它。在

相关问题 更多 >