Python PyQt按钮单击不会触发事件

时间:2020-11-02 15:43:37

标签: python pyqt5

我是PyQt的新手,但有一个我无法解决的问题。我正在尝试从第二个窗口获取文本并将其设置为字段,因此当我关闭第二个窗口时,可以从第一个主窗口打印它,但是我的“ AnotherWindow”按钮将不会触发事件,我真的不知道为什么吗?这是代码。谁能指导我? 谢谢

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

2 个答案:

答案 0 :(得分:1)

  1. 在主窗口构造函数中实例化第二个窗口。 The provided information does not map to an AD object id.
  2. 在创建第二个窗口的实例时,New-AzRoleAssignment-self.w = AnotherWindow (self)作为父级传递,因此,当主窗口关闭时,第二个窗口也将关闭。
  3. 要从QLineEdit小部件获取文本,请应用self.w = AnotherWindow (self)和更多https://doc.qt.io/qt-5/qlineedit.html#text-prop
  4. 您没有显示方法self,据我所知,您想在其中显示您的意图。

尝试一下:

QString text() const

enter image description here

enter image description here

答案 1 :(得分:1)

问题出在self.buttonf.clicked.connect(...)上。该调用将功能附加到按钮上的“单击”操作。调用该函数时不带参数,并且返回值被简单地丢弃。在您的情况下,lambda: self.get_text是只返回self.get_text方法地址的函数。由于get_text不需要任何其他参数,因此您可以将其直接绑定到此插槽。

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

稍后,您还会在需要调用text方法的地方遇到一个错误。有了这两个更改,工作程序就是

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