简体   繁体   English

从调用窗口中的对话框中检索数据

[英]Retrieve data from a dialog in the calling window

I am struggling to retrieve results from a QDialog window and pass them back to the previous window.我正在努力从 QDialog 窗口中检索结果并将它们传递回前一个窗口。 I want the user to be able to select a value/values, and then once they click okay in the dialog window, it should return them back to the previous window, and pass the text into the byVariables Textbox.我希望用户能够选择一个值/值,然后一旦他们在对话框窗口中单击确定,它应该将它们返回到前一个窗口,并将文本传递到 byVariables 文本框。 The error returns on the line self.procUnivariate.byVariables.setText(item.text()) .错误返回self.procUnivariate.byVariables.setText(item.text())行。 The error states:错误指出:

AttributeError: 'ProcUnivariateVariables' object has no attribute 'procUnivariate' AttributeError:“ProcUnivariateVariables”对象没有属性“procUnivariate”

I also tried using a return statement as you can see that is commented out.我还尝试使用 return 语句,你可以看到它被注释掉了。 Unfortunately it seemed like the code got stuck and wasn't moving to the next line as I threw a print statement after and it never printed that line.不幸的是,代码似乎卡住了,没有移动到下一行,因为我在之后抛出了一个打印语句,但它从未打印过该行。 I need help figuring out how to pass it back to the ProcUnivariate class once the okay button is clicked.单击确定按钮后,我需要帮助确定如何将其传递回 ProcUnivariate 类。 Also, if I can figure this out, I'll likely turn the ProcUnivariate Class into a dialog window as well because it should take away the focus from the main window and shouldn't stay open if the main window is closed.此外,如果我能弄清楚这一点,我可能会将 ProcUnivariate 类也变成一个对话框窗口,因为它应该从主窗口中移开焦点,并且在主窗口关闭时不应该保持打开状态。

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QListWidget, QLineEdit, QTextEdit, QGridLayout, QHBoxLayout, QVBoxLayout, QDialog, QSizePolicy, QFileDialog, QTabWidget, QCheckBox
import PyQt5.QtGui as qtg
import glob
import os
from PyQt5.QtCore import Qt, QSettings
import inspect
from PyQt5 import QtCore
import pandas as pd 
import pathlib
import pyreadstat
import json

class ProcUnivariateVariables(QDialog):
    def __init__(self):
        super().__init__()
        
        
        self.procUnivariateByVariables = []
        self.layout = QGridLayout()
        self.setLayout(self.layout)
        self.allVariables = QListWidget()
        self.allVariables.setSelectionMode(3)
        self.variablesSelected = QListWidget()
        self.variablesSelected.setSelectionMode(3)
        self.layout.addWidget(self.allVariables, 1,0, 4, 1)
        self.layout.addWidget(self.variablesSelected, 1, 1, 4, 1)
        self.okButton = QPushButton("Ok")
        self.resetButton = QPushButton("Reset")
        self.cancelButton = QPushButton("Cancel")
        self.layout.addWidget(self.okButton, 1, 2)
        self.layout.addWidget(self.resetButton, 2, 2)
        self.layout.addWidget(self.cancelButton, 3, 2)
        self.addByVariable = QPushButton(">")
        self.removeVariable = QPushButton("<")
        self.layout.addWidget(self.addByVariable, 5, 0)
        self.layout.addWidget(self.removeVariable, 5, 1)
        
        self.button_Connections()
        
        
    def setItems(self, items):
        self.allVariables.clear()
        for item in items:
            self.allVariables.addItem(item)
      
    def add_by_variable(self):
        selected_elements=[item.text() for item in self.allVariables.selectedItems()]
        variableItem = self.variablesSelected.insertItems(self.variablesSelected.count(),selected_elements)
    
    def button_Connections(self):
        self.addByVariable.clicked.connect(self.add_by_variable)
        self.removeVariable.clicked.connect(self.remove_variable)
        self.okButton.clicked.connect(self.okay_clicked)
    
    def remove_variable(self):
      removeVariables = [item.text() for item in self.variablesSelected.selectedItems()]
      self.selectedVariables.takeItems(removeVariables)
      #sourceItem = self.currentSource.takeItem(oldSource)
    
    def okay_clicked(self):
        self.byVariablesSelected = [item.text() for item in self.variablesSelected.selectedItems()]
        #self.procUnivariateByVariables = byVariablesSelected
        print(self.byVariablesSelected)
        self.accept()
        
        #return self.byVariablesSelected
        

        for item in self.byVariablesSelected:
            print(item)
            self.procUnivariate.byVariables.setText(item.text())

class ProcUnivariate(QWidget):
    def __init__(self):
        super().__init__()
        
        self.procUnivariateVariables = None
        
        layout = QGridLayout(self)

        ##Proc Univariate window Widgets
        self.by = QLabel("By")
        self.byVariables = QLineEdit()
        self.byVariableList = QListWidget()
        self.byButton = QPushButton("...")
        self.varLabel = QLabel("Var")
        self.classLabel = QLabel("Class")
        self.freqLabel = QLabel("Freq")
        self.histogramLabel = QLabel("Histogram")
        self.freqBox = QLineEdit()
        self.histBox = QLineEdit()
        self.varBox = QLineEdit()
        self.varButton = QPushButton("...")
        self.sourceLabel = QLabel("Name")
        self.sourceText = QLineEdit()
        self.sourceButton = QPushButton("...")
        self.selectionLabel = QLabel("Selection criteria")
        self.selectionCriteria = QTextEdit()
        self.statisticLabel = QLabel("Output")
        self.statisticSearch = QLineEdit()
        self.statisticList = QListWidget()
        self.statisticList.setSortingEnabled(True)
        self.outputLabel = QLabel("Output Statement")
        self.outputText = QTextEdit()
        self.fileOutputPath = QLineEdit("Output File Path")
        self.runButton = QPushButton("Run")
        self.previewButton = QPushButton("Preview")
        self.okButton = QPushButton("Ok")
        self.resetButton = QPushButton("Reset")
        self.cancelButton = QPushButton("Cancel")
        self.secondWindowConnections()
        
        layout.addWidget(self.by, 1,0,1,1)
        layout.addWidget(self.byVariables, 2, 0, 1, 4)
        layout.addWidget(self.byButton, 2, 5, 1, 1)
        #layout.addWidget(self.byVariableList, 3, 0, 1, 1)
        layout.addWidget(self.freqLabel, 3, 0)
        layout.addWidget(self.freqBox, 4, 0, 1, 4)
        layout.addWidget(self.histogramLabel, 5, 0)
        layout.addWidget(self.histBox, 6, 0, 1, 4)
        layout.addWidget(self.varLabel, 7, 0, 1, 4)
        layout.addWidget(self.varBox, 8, 0)
        layout.addWidget(self.varButton, 8,5)
        layout.addWidget(self.sourceLabel, 1, 6)
        layout.addWidget(self.sourceText, 2, 6, 1, 4)
        layout.addWidget(self.sourceButton, 2, 10)
        layout.addWidget(self.selectionLabel, 3, 6)
        layout.addWidget(self.selectionCriteria, 4, 6, 1, 4)
        layout.addWidget(self.statisticLabel, 5, 6)
        layout.addWidget(self.statisticSearch, 6, 6, 1, 4)
        layout.addWidget(self.statisticList, 7, 6, 3, 4)
        layout.addWidget(self.outputLabel, 10, 6)
        layout.addWidget(self.outputText, 11, 6, 1, 4)
        layout.addWidget(self.runButton, 12, 6)
        layout.addWidget(self.previewButton, 12, 7)
        layout.addWidget(self.okButton, 12, 8)
        layout.addWidget(self.resetButton, 12, 9)
        layout.addWidget(self.cancelButton, 12, 10)
        
        self.setLayout(layout)
     
    def secondWindowConnections(self):   # this had a typo
        self.byButton.clicked.connect(self.show_third_window)

    def show_third_window(self):
        if self.procUnivariateVariables is None:           # if window has been created yet
            self.procUnivariateVariables = ProcUnivariateVariables()   # create window
        if not self.procUnivariateVariables.isVisible():   # if window is showing
            self.procUnivariateVariables.show()            # show window
        self.procUnivariateVariables.setItems(self.procUnivariateVariablesItems)  # send items to window

    def send_items(self, items):       # this is to collect the variable that
        self.procUnivariateVariablesItems = items  # move to the third window

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        # Add a title
        self.setWindowTitle("GUI Querying Program")
        self.layout = QHBoxLayout()
        self.setLayout(self.layout)
        self.initUI()
        self.setButtonConnections()
        self.sw = None    # dont initialize until neccessary.

    def initUI(self):
        subLayouts = {}
        subLayouts['LeftColumn'] = QGridLayout()
        self.layout.addLayout(subLayouts['LeftColumn'],1)
        self.buttons = {}
        self.buttons['addVariable'] = QPushButton('>')
        self.buttons['removeVariable'] = QPushButton('<')
        self.buttons['Toolkit'] = QPushButton('Toolkit')
        self.variables = QListWidget()
        self.selectedVariables = QListWidget()
        subLayouts['LeftColumn'].addWidget(self.variables, 7,0,4,1)
        subLayouts['LeftColumn'].addWidget(self.selectedVariables, 7,1,4,1)
        subLayouts['LeftColumn'].addWidget(self.buttons['addVariable'], 10,0,1,1)
        subLayouts['LeftColumn'].addWidget(self.buttons['removeVariable'], 10,1,1,1)
        subLayouts['LeftColumn'].addWidget(self.buttons['Toolkit'], 11,1,1,1)
        names = ['apple', 'banana', 'Cherry']
        self.variables.insertItems(0, names)

    def setButtonConnections(self):
        self.buttons['addVariable'].clicked.connect(self.add_variable)
        self.buttons['Toolkit'].clicked.connect(self.show_new_window)
        # self.buttons['Toolkit'].clicked.connect(self.add_selected_variables)
        # only use one connnect slot

    def add_variable(self):
        for item in self.variables.selectedItems():
            self.selectedVariables.addItem(item.clone())

    def show_new_window(self):     
        if self.sw is None:   #  check if window has been constructed
            self.sw = ProcUnivariate()  # construct window
        if not self.sw.isVisible():    #  If winow is not showing
            self.sw.show()         #  show window
        self.sw.send_items(self.add_selected_variables())   # send selected 
                                                            # variables to second window

    def add_selected_variables(self):
        items = []
        for i in range(self.selectedVariables.count()):
            items.append(self.selectedVariables.item(i).clone())
        # self.tw.setItems(items) ...  self.tw doesnt exist so return them
        return items 

if __name__ == "__main__":
    import sys
    app = QApplication([])
    mw = MainWindow()
    mw.show()
    app.exec()

What you need to do is connect a slot to the accepted signal of the dialog, and then retrieve the variables via a dedicated method of the dialog when that signal is emitted.您需要做的是将插槽连接到对话框的accepted信号,然后在发出该信号时通过对话框的专用方法检索变量。

Here are all the relevant changes (fully tested as working):以下是所有相关更改(完全测试为有效):

class ProcUnivariateVariables(QDialog):
    ...
    def button_Connections(self):
        ...
        # connect to the dialog's built-in accept slot
        self.okButton.clicked.connect(self.accept)
    
    # add a method for retrieving the variables
    def byVariablesSelected(self):
        return [item.text() for item in self.variablesSelected.selectedItems()]

class ProcUnivariate(QWidget):
    ...
    def secondWindowConnections(self):   # this had a typo
        self.byButton.clicked.connect(self.show_third_window)
        self.varButton.clicked.connect(self.show_third_window)

    def show_third_window(self):
        if self.procUnivariateVariables is None:           # if window has been created yet
            self.procUnivariateVariables = ProcUnivariateVariables()   # create window
            # connect the new slot to the dialog's built-in accepted signal
            self.procUnivariateVariables.accepted.connect(self.receive_selected_variables)
        if not self.procUnivariateVariables.isVisible():   # if window is showing
            self.procUnivariateVariables.show()            # show window
        self.procUnivariateVariables.setItems(self.procUnivariateVariablesItems)  # send items to window
        # set an identifier from the source button
        self.procUnivariateVariables.sourceName = self.sender().text()

    # add a slot for handling the dialog's accepted signal
    def receive_selected_variables(self):
        variables = self.procUnivariateVariables.byVariablesSelected()
        text = ', '.join(variables)
        if self.procUnivariateVariables.sourceName == 'byButton':
            self.byVariables.setText(text)
        else:
            self.varBox.setText(text)

(Note that the above changes make the okay_clicked method of ProcUnivariateVariables redundant, so it should be removed). (注意上面的改动使得 ProcUnivariateVariables 的okay_clicked方法ProcUnivariateVariables多余,所以应该去掉)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM