如何使用来自QWidget的信号告诉主窗口执行一个函数?

2024-09-30 14:22:05 发布

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

我试着在confirmPoputWindow上按下enter按钮时运行一个函数inputAttendance(AthleteInfo)。这个函数包含在我导入的另一个文件中,不在任何类中。 我的一个问题是它似乎在运行

self.confirmw.confirmAthlete.connect(inputAttendance(AthleteInfo))

在信号发出之前。一旦inputAttendance()完成,整个窗口将在我收到错误后关闭

argument 1 has unexpected type 'NoneType'

我试着查找它,可能是我没有定义连接类型?在

任何帮助都将不胜感激,因为我已经被困在这个问题上有一段时间了。在

Edit:InputAttendance()是一个函数,用于更新我导入的另一个文件中的电子表格,但由于它与我的问题无关,因此没有包含在文章中。我已经测试了这个函数,它工作得很好,所以我确信它不会导致程序崩溃,而是它的调用方式。很抱歉给你添麻烦了!在

from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit, 
    QInputDialog, QApplication, QLabel)
from PyQt5.QtCore import *


class Ex(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):      

        self.le = QLineEdit(self)
        self.le.move(500, 500)
        self.le.returnPressed.connect(self.pushEnter)    

        self.setGeometry(1000, 1000, 1000, 1000)
        self.setWindowTitle('Input dialog')
        self.show()

    def pushEnter(self):
        text = self.le.text()
        AthleteInfo = getID(text)

        if (AthleteInfo == -1):
            print ("Could nto find that ID")

        else:
            try:
                self.confirmw =confirmPopup("Confirm Window")
            except Exception in e:
                print(e)
                time.sleep(10)
            self.confirmw.setGeometry(1000, 1000, 1000, 1000)
            self.confirmw.show()
            try:

                self.confirmw.setWindowModality(Qt.ApplicationModal)
            except Exception as e:
                print(e)
                time.sleep(5)
            try:                   self.confirmw.confirmAthlete.connect(inputAttendance(AthleteInfo))

            except Exception as e:
                print(e)
                time.sleep(5)

class confirmPopup(QWidget):

    confirmAthlete = pyqtSignal(str)

    def __init__(self, name):
        super().__init__()
        self.name = name
        self.initUI()

    def initUI(self):
        lblName = QLabel(self.name, self, text = "Press enter to confirm")

    def keyPressEvent(self, event):
        keyPress = event.text()

        if event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return:

            try:
                #print("Emitting Signal")
                self.confirmAthlete.emit("Yes")
            except Exception as e:
                print(e)
                time.sleep(5)

        if event.key() == Qt.Key_Backspace:
            print("Backspace was pressed")

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Ex()
    sys.exit(app.exec_())

Tags: 函数textnameselfleeventifinit
1条回答
网友
1楼 · 发布于 2024-09-30 14:22:05

你好,先生,这是一个很好的问题,让我们这样总结一下。在

  1. 假设我们有两个类,Widget类和MainWindow类。在
  2. 小部件将包含两个信号,一个信号从自身运行一个方法,另一个信号从主窗口运行一个方法。在
  3. 我们将在小部件内部连接的第一个信号,从小部件发出并运行一个方法。在
  4. 第二个将连接到主窗口内,在那里我们有一个类的小部件实例,并将从主窗口运行一个方法

让我们看看这个小例子,它代表了我刚才想总结的内容。

from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget


class MainWindow(QMainWindow):

    myWidget = None

    def __init__(self):
        super(MainWindow, self).__init__()

        self.myWidget = Widget()

        self.setFixedSize(500,500)
        self.myWidget.setFixedSize(500,500)

        self.layout().addWidget(self.myWidget)

        self.myWidget.signalExecuteAMainWindowFunction.connect(self.mainWindowFunction)

    def mainWindowFunction(self):
        print("running my MAINWINDOW function...")


class Widget(QWidget):

    signalExecuteMyFunction = pyqtSignal()
    signalExecuteAMainWindowFunction = pyqtSignal()

    def __init__(self):
        super(Widget, self).__init__()
        self.signalExecuteMyFunction.connect(self.myFunction)

    def mousePressEvent(self, QMouseEvent):
        self.signalExecuteMyFunction.emit()
        super(Widget, self).mousePressEvent(QMouseEvent)

    def mouseMoveEvent(self, QMouseEvent):
        self.signalExecuteAMainWindowFunction.emit()
        super(Widget, self).mouseMoveEvent(QMouseEvent)

    def myFunction(self):
        print("running my WIDGET function...")



if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

Ps:我做过的一些事情有很多更好的方法,比如变量名、面向对象和组织,但这是你所要求的关键。希望它能更清楚地说明它是如何工作的。:D

相关问题 更多 >