user3657752
user3657752

Reputation: 81

Change Pyqt status bar text on button click

I can'get my program to change status bar text on button click. I keep getting

"TypeError: argument 1 has unexpected type 'NoneType'" error on the 'self.closeButton.clicked.connect(self.process('text'))'. I don't know what to do anymore

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

Upvotes: 0

Views: 7514

Answers (2)

Odysseas
Odysseas

Reputation: 1060

Change the line:

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

To

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

You need to pass the function itself as the argument, not the result of a function call (since your method does not contain a return statement, self.process() returns None).

If you want for the process method to accept an argument, you have to first change it as Avión has already suggested:

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

but change the line which connects to the clicked signal to:

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

The lambda expression is needed to pass a callable object to connect().

Upvotes: 3

Avión
Avión

Reputation: 8376

Change the process function to:

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

Now when you call the function self.closeButton.clicked.connect(self.process('text')) it will take the text and print it.

Upvotes: 3

Related Questions