如何使用来自 QWidget 的信号告诉主窗口执行函数? [英] How do I use signals from a QWidget to tell the main window to execute a function?

查看:54
本文介绍了如何使用来自 QWidget 的信号告诉主窗口执行函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当在确认弹出窗口上按下输入按钮时,我都会尝试运行函数 inputAttendance(AthleteInfo).这个函数包含在我导入的另一个文件中,而不是在任何类中.我遇到的一个问题是它似乎在运行

I am trying to run a function inputAttendance(AthleteInfo) whenever the enter button is pressed on the confirmPopup window. This function is contained within another file that I import and is not in any of the classes. One problem I have is that it seems to run

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

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

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

before the signal is even emitted. Once inputAttendance() is done, the entire window closes after I get an error

参数 1 具有意外类型NoneType"

argument 1 has unexpected type 'NoneType'

我试着查了一下,可能是我没有定义连接类型?

I tried looking it up and it possibly could be me not defining a connection type?

任何帮助将不胜感激,因为我已经坚持了很长一段时间.

Any help would be greatly appreciated because I have been stuck on this for quite some while.

InputAttendance() 是一个函数,用于更新我导入的另一个文件中的电子表格,但没有包含在帖子中,因为它与我的问题无关.我已经测试了该功能并且它运行良好,所以我确定它不会导致程序崩溃,而是它的调用方式.抱歉造成混乱!

InputAttendance() is a function that updates a spreadsheet that is in another file that I imported but didn't include in the post since it is irrelevant to my question. I have tested the function and it works perfectly so I am certain it is not causing the program to crash but rather how it is called. Sorry for the confusion!

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

推荐答案

你好,先生,这个问题很好,我们就这样总结一下.

Helloww sir, that's a really nice question, let's summarize in this way.

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

让我们看一下这个小例子,它代表了我刚刚试图总结的内容.

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

这篇关于如何使用来自 QWidget 的信号告诉主窗口执行函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆