【问题标题】:Change Pyqt status bar text on button click单击按钮时更改 Pyqt 状态栏文本
【发布时间】:2017-08-03 21:53:28
【问题描述】:

我无法让我的程序在单击按钮时更改状态栏文本。我不断得到

"TypeError: argument 1 has unexpected type 'NoneType'"'self.closeButton.clicked.connect(self.process('text'))' 上的错误。 我不知道该怎么办了

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_())

【问题讨论】:

  • 您必须将信号连接到插槽,并且 self.process('text') 不是插槽/可调用的。此外,您的示例远非最小。

标签: python pyqt


【解决方案1】:

换行:

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

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

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

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

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

但将连接到 clicked 信号的线更改为:

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

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

【讨论】:

    【解决方案2】:

    process 函数更改为:

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

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

    【讨论】:

    • 好的,已经修复了,但是现在点击按钮后窗口本身会关闭,尽管没有链接到它的退出命令。
    猜你喜欢
    • 2014-03-06
    • 1970-01-01
    • 2016-07-11
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多