如何从pyqt中的另一个窗口返回一个变量? [英] how to return a variable from another window in pyqt?

查看:94
本文介绍了如何从pyqt中的另一个窗口返回一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PYQT 中有一个主窗口和一个对话框.我在主窗口中接收用户的输入,然后打开对话窗口并将输入传递给此窗口.

I have one main window and one dialog in PYQT. I receive the input from user in main window and then open the dialog window and pass the input to this window.

再次在对话框窗口中,我收到另一个输入,并将这个新输入与我通过主窗口传递的另一个输入一起添加.

in dialog window again I receive another input and add this new input with the other input which I passed form main window.

class MainWindow (QMainWindow):
    def __init__(self):
        super().__init__()
        self.window=Ui_MainWindow() 
        self.window.setupUi(self)   

     def open_qdialog_window(self):
        
        self.dialog_window = Qdialog(self.window.lineedit1.currentText())

        self.dialog_window .show()

class Qdialog(QDialog):
    def __init__(self,DS_parameter):
        super().__init__()
        
        self.DS_parameters=DS_parameters
        #UI file of oscilloscope
    
        self.window=Ui_Qdialog()
        self.window.setupUi(self)
        self.UI()
    
    def summation(self):
        self.DS_parameters=self.DS_parameters+self.window.lineedit2.currentText()

现在的问题是如何将 self.DS_parameters 返回到主窗口并使用这个新值更新 editline1?

Now the question is how I can return self.DS_parameters to the main window and update editline1 with this new value?

推荐答案

对我来说,很难就您的代码提供具体(可能更有帮助)的反馈,因为您共享的部分不完整.不过,我希望我比较笼统的回答对您有所帮助.

To me, it is difficult to provide you with specific (probably more helpful) feedback on your code, since the part you shared is incomplete. However, I hope that my rather general answer is somewhat useful.

在 Qt 中,signals 用于在 GUI 元素之间建立连接.基本思想是事件触发函数的执行.例如,每当按下按钮时,就会更新一些文本.下面是一个说明如何将总和返回到主窗口并更新相应行的工作示例.

In Qt, signals are used to establish connections between GUI elements. The basic idea is that an event triggers the execution of a function. For instance, whenever a button is pressed, some text gets updated. Below you find a working example illustrating how to return the sum to the main window and update the corresponding line.

from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow,
    QDialog,
    QWidget,
    QVBoxLayout,
    QLineEdit,
    QLabel,
    QPushButton
    )


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.user_input = QLineEdit()
        self.populate()
        self.show()

    def populate(self):
        widgets = [QLabel("Insert a number"), self.user_input]
        centralWidget = self.group_widgets(widgets)
        self.setCentralWidget(centralWidget)

    def group_widgets(self, widgets):
        parentWidget = QWidget()
        layout = QVBoxLayout()
        for widget in widgets: layout.addWidget(widget)
        parentWidget.setLayout(layout)
        return parentWidget

    def when_input(self, function):
        self.user_input.textChanged.connect(function)

class Dialog(QDialog):
    def __init__(self):
        super().__init__()
        self.user_input = QLineEdit()
        self.admin_input = QLineEdit("0")
        self.button = QPushButton("add")
        self.relay_sum = None  # function to relay result of addition
        self.populate()
        self.show()

    def populate(self):
        widgets = self.get_widgets()
        layout = self.get_layout(widgets)
        self.setLayout(layout)

    def get_widgets(self):
        widgets = [
            QLabel("Inserted number"),
            self.user_input,
            QLabel("Insert the second number"),
            self.admin_input,
            self.button
            ]
        return widgets

    def get_layout(self, widgets):
        layout = QVBoxLayout()
        for widget in widgets: layout.addWidget(widget)
        return layout

    def when_buttonReleased(self, function):
        self.relay_sum = function
        self.button.released.connect(self.process_input)

    def process_input(self):
        user_number = float(self.user_input.text())
        admin_number = float(self.admin_input.text())
        result = "%.2f" %(user_number + admin_number)
        self.relay_sum(result)

def main():
    app = QApplication([])
    mainWindow = MainWindow()
    dialog = Dialog()
    mainWindow.when_input(lambda text: dialog.user_input.setText(text))
    dialog.when_buttonReleased(lambda text: mainWindow.user_input.setText(text))
    app.exec_()

if __name__ == "__main__":
    main()

正如您所注意到的,我允许自己更改您建议的变量名称.在示例中,创建了两个窗口并建立了两个连接:

As you have noticed, I allowed myself to change variable names you proposed. In the example, two windows are created and two connections are established:

[1.connection] 每当主窗口的文本发生变化 (MainWindow.user_input.textChanged) 时,新文本就会被设置为对话窗口中相应行的内容 (Dialog.user_input.setText).

[1. connection] Whenever the text of the main window is changed (MainWindow.user_input.textChanged), the new text is set as the content of the corresponding line in the dialog window (Dialog.user_input.setText).

[2.connection] 每当对话框窗口中的按钮被释放 (Dialog.button.released),对话框窗口中的值就会相加,结果被设置为主窗口的文本 (MainWindow.user_input.setText).

[2. connection] Whenever the button in the dialog window is released (Dialog.button.released), the values in the dialog window are summed and the result is set as text of the main window (MainWindow.user_input.setText).

这篇关于如何从pyqt中的另一个窗口返回一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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