Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将值从一个对话框传递到另一个对话框_Python_Qt_Dialog_Pyqt - Fatal编程技术网

Python 如何将值从一个对话框传递到另一个对话框

Python 如何将值从一个对话框传递到另一个对话框,python,qt,dialog,pyqt,Python,Qt,Dialog,Pyqt,下面的代码创建了一个带有QLabel和单个QPushButton的对话框窗口。 单击按钮将弹出第二个对话框,其中包含文本字段和确认按钮。 用户在文本字段中输入文本,然后单击“确认”按钮。 第二个对话框关闭,返回用户输入的文本。 返回时,第一个对话框使用它替换标签的“默认文本值” 如何将用户文本值传递到第一个对话框 from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * app = QA

下面的代码创建了一个带有
QLabel
和单个
QPushButton
的对话框窗口。 单击按钮将弹出第二个对话框,其中包含文本字段和
确认
按钮。
用户在文本字段中输入文本,然后单击“确认”按钮。
第二个对话框关闭,返回用户输入的文本。
返回时,第一个对话框使用它替换标签的“默认文本值”

如何将用户文本值传递到第一个对话框

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
app = QApplication([])


class Modal(QDialog):
    def __init__(self, parent):
        super(Modal, self).__init__(parent)
        self.setLayout(QVBoxLayout())
        self.lineedit = QLineEdit(self)
        self.layout().addWidget(self.lineedit)
        button = QPushButton(self)
        button.setText('Confirm')
        button.clicked.connect(self.close)
        self.layout().addWidget(button)
        self.setModal(True)
        self.show()


class Dialog(QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setLayout(QVBoxLayout())
        button = QPushButton('Show modal dialog')
        button.clicked.connect(self.showModal)
        self.label = QLabel('Default text value')
        self.layout().addWidget(self.label)
        self.layout().addWidget(button)
        self.resize(200, 50)
        self.show()


    def showModal(self):
        dialog = Modal(self)


dialog = Dialog()
app.exec_()

您可以从一个对话框发送一个信号,然后在另一个对话框中捕捉它

使用以下命令在“发射”对话框中定义信号:

该信号有一个类型为
str
的参数,从编辑用户输入的文本行读取后,将从
确认
插槽发出:

def confirm(self):
    self.close()
    value = self.lineedit.text()
    print ('entered value: %s' % value)
    self.confirmed.emit(value) #emit the signal, passing the text as its only argument
要捕获信号,另一个类需要一个插槽:

class Dialog(QDialog):
# ...
def changeText(self, t):
    self.label.setText(t)
slot函数将在其
t
参数中接收文本,并相应地设置标签文本,但要实现这一点,必须连接信号和插槽

首先,让我们编辑
Modal
类构造函数,并删除最后两行:

    self.setModal(True)
    self.show()
在连接
changeText
插槽和
Modal
确认信号后,让我们在
对话框的
showmodel
中使用它们:

def showModal(self):
    modal_dialog = Modal(self)
    modal_dialog.confirmed.connect(self.changeText) #connect signal and slot
    modal_dialog.setModal(True)
    modal_dialog.show()
全文参考:

在confirm中使用对话框的属性accept,在接受模式对话框之前,访问父对话框及其属性标签并设置其文本。此外,我还使用了partial来传递带有参数的插槽,使用
exec()
,而不是
show()
,然后只需执行
self.label.setText(模式对话框.lineedit.text())
exec()
为我工作!谢谢
def showModal(self):
    modal_dialog = Modal(self)
    modal_dialog.confirmed.connect(self.changeText) #connect signal and slot
    modal_dialog.setModal(True)
    modal_dialog.show()
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
from functools import partial

class Modal(QDialog):
    def __init__(self, parent):
        super(Modal, self).__init__(parent)
        self.setLayout(QVBoxLayout())
        self.lineedit = QLineEdit(self)
        self.layout().addWidget(self.lineedit)
        button = QPushButton(self)
        button.setText('Confirm')
        button.clicked.connect(partial(self.confirm,parent)) #using partial to make a slot alog with parameters
        self.layout().addWidget(button)
        self.setModal(True)
        self.exec_()  #  Use exec if you want to really want to create modal dialog

    def confirm(self,parent):
        self.accept() #instead of close use its accept feature
        value = self.lineedit.text()
        parent.label.setText(value)  # acessing DialogClass object that you passed  while calling show modal
        print('entered value: %s' % value)


class Dialog(QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setLayout(QVBoxLayout())
        button = QPushButton('Show modal dialog')
        button.clicked.connect(self.showModal)
        self.label = QLabel('Default text value')
        self.layout().addWidget(self.label)
        self.layout().addWidget(button)
        self.resize(200, 50)
        self.show()

    def showModal(self):
        modal_dialog = Modal(self)

app = QApplication([])
dialog = Dialog()
sys.exit(app.exec_())