vannie92
vannie92

Reputation: 1

Python PyQt button click won't fire a event

i am new in PyQt but i have a problem i can't solve. I am trying to get text from second window and set it to field, so when i close second window i can print it form first main window, but my "AnotherWindow" button won't fire event and i really don't know why? Here is code. Can anyone guide me? Thanks

class AnotherWindow(QMainWindow):


    def __init__(self):
        super().__init__()
        self.resize(1200, 600)
        self.text = "basetext"

        self.layoutf = QFormLayout()
        self.buttonf = QPushButton("get text")
        self.buttonf.clicked.connect(lambda: self.getText)

        self.line = QLineEdit()
        self.layoutf.addRow(self.buttonf,self.line)


        self.widgetf = QWidget()
        self.widgetf.setLayout(self.layoutf)

        self.setCentralWidget(self.widgetf)

    def getText(self):
        print(self.line.text)
        self.text = self.line.text
        self.close()



class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.w = None  # No external window yet.


        self.mainLayout = QGridLayout()

        self.button = QPushButton("Push for Window")
        self.button.clicked.connect(self.show_new_window)

        self.button1 = QPushButton("Push ")
        self.button1.clicked.connect(self.printFromSecondWindow)

        self.mainLayout.addWidget(self.button,0,0)
        self.mainLayout.addWidget(self.button1, 0, 1)

        self.widget = QWidget()
        self.widget.setLayout(self.mainLayout)

        self.setCentralWidget(self.widget)


    def show_new_window(self):
        if self.w is None:
            self.w = AnotherWindow()
            self.w.show()

        else:
            self.w.close()  # Close window.
            self.w = None  # Discard reference.

    def printFromSecondWindow(self):
        print(self.w.text)


    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    app.exec_()

Upvotes: 0

Views: 1409

Answers (2)

S. Nick
S. Nick

Reputation: 13641

  1. Instantiate the second window once in the main window constructor. self.w = AnotherWindow (self)
  2. When creating an instance of the second window, self.w = AnotherWindow (self) - self is passed as a parent, so that when the main window is closed, the second window also closes.
  3. To get text from a QLineEdit widget - apply QString text() const, more https://doc.qt.io/qt-5/qlineedit.html#text-prop
  4. You did not show the method printFromSecondWindow in which, as I understand it, you wanted to show what you intended.

Try it:

import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.Qt import *


class AnotherWindow(QMainWindow):
    def __init__(self, parent=None):
        super(AnotherWindow, self).__init__(parent)
        self.widgetf = QWidget()
        self.setCentralWidget(self.widgetf)        
        
#        self.resize(1200, 600)
        self.text = "basetext"

        self.layoutf = QFormLayout(self.widgetf)
        self.buttonf = QPushButton("get text")
#        self.buttonf.clicked.connect(lambda: self.getText)       # ??? lambda
        self.buttonf.clicked.connect(self.getText)
        
        self.line = QLineEdit()
        self.layoutf.addRow(self.buttonf,self.line)

    def getText(self):
        print(self.line.text())                                   # !   .text()
        self.text = self.line.text()                              # !   .text() 
        self.close()
        
        
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.widget = QWidget()
        self.setCentralWidget(self.widget)
# ?        self.w = None  # No external window yet.

        self.button = QPushButton("Push for Window")
        self.button.clicked.connect(self.show_new_window)
        self.button1 = QPushButton("Push ")
        self.button1.clicked.connect(self.printFromSecondWindow)
        
        self.mainLayout = QGridLayout(self.widget)
        self.mainLayout.addWidget(self.button, 0, 0)
        self.mainLayout.addWidget(self.button1, 0, 1)

        self.w = AnotherWindow(self)                                # +++ 

    def show_new_window(self):
#        if self.w is None:
#            self.w = AnotherWindow()
        self.w.show()                                 # !
#        else:
#            self.w.close()  # Close window.
#            self.w = None  # Discard reference.

    def printFromSecondWindow(self):                                 # +++
        print(self.w.text)
            

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())            

enter image description here

enter image description here

Upvotes: 1

tdelaney
tdelaney

Reputation: 77337

The problem is with self.buttonf.clicked.connect(...). This call attaches a function to the "clicked" action on the button. The function is called without parameters and the return is simply discarded. In your case, lambda: self.get_text is a function that does nothing but return the address of the self.get_text method. Since get_text doesn't need any additional parameters, you can bind it directly to this slot.

self.buttonf.clicked.connect(self.get_text)

You also have a bug later on where you need to call the text method. With these two changes, the working program is

import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.Qt import *
    
class AnotherWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.resize(1200, 600)
        self.text = "basetext"

        self.layoutf = QFormLayout()
        self.buttonf = QPushButton("get text")
        self.buttonf.clicked.connect(self.getText)

        self.line = QLineEdit()
        self.layoutf.addRow(self.buttonf,self.line)

        self.widgetf = QWidget()
        self.widgetf.setLayout(self.layoutf)

        self.setCentralWidget(self.widgetf)

    def getText(self):
        print("the info", self.line.text())
        self.text = self.line.text()
        self.close()


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.w = None  # No external window yet.

        self.mainLayout = QGridLayout()

        self.button = QPushButton("Push for Window")
        self.button.clicked.connect(self.show_new_window)

        self.button1 = QPushButton("Push ")
        self.button1.clicked.connect(self.printFromSecondWindow)

        self.mainLayout.addWidget(self.button,0,0)
        self.mainLayout.addWidget(self.button1, 0, 1)

        self.widget = QWidget()
        self.widget.setLayout(self.mainLayout)

        self.setCentralWidget(self.widget)

    def show_new_window(self):
        if self.w is None:
            self.w = AnotherWindow()
            self.w.show()

        else:
            self.w.close()  # Close window.
            self.w = None  # Discard reference.

    def printFromSecondWindow(self):
        print(self.w.text)


app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()

Upvotes: 1

Related Questions