简体   繁体   中英

Python pyqt5 reset all widget inputs

I am using this method to have multiple windows GUI in a python script. With a button press, I open one of the two forms, that have multiple textEdits and comboBoxes, used to fill a word teamplate easier.

Now, I want to reset all the inputs after I finished filling the form, and save the document.

What behaviour I want:

From main window, open one of the forms depending on 2 ComboBoxes index (type of document, and the client's gender).

After I fill the form and exit it, I go back to the main window, and when I open again a form, the text edits are empty and the combo boxes are on index 0.

But i can't make it to work, either the inputs are not resetted, either I kill the popup window and the main window.

I can't only kill the popup, I can only hide it and show it again.

# import some PyQt5 modules
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QImage
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QTimer




from lib.eco_barbat_ui import *
from lib.eco_femeie_ui import *
from lib.eco_main_ui import *

class eco_abd_barbat(QWidget):
    def __init__(self):
        # call QWidget constructor
        super().__init__()
        # self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.ui = Ui_EcoAbdBarbat()
        self.ui.setupUi(self)

    def display(self):
        self.show()


class eco_abd_femeie(QWidget):
    def __init__(self):
        # call QWidget constructor
        super().__init__()
        # self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.ui = Ui_EcoAbdFemeie()
        self.ui.setupUi(self)
    def display(self):
        self.show()


class MainWindow(QWidget):
    # class constructor
    def __init__(self):
        # call QWidget constructor
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.eco_abd_barbat = eco_abd_barbat()
        self.eco_abd_femeie = eco_abd_femeie()
        self.ui.pushButton.clicked.connect(self.open)

    def open(self):
        if self.ui.tip.currentIndex() == 0 and self.ui.sex.currentIndex()==0:
            self.eco_abd_femeie.ui.qnume = self.ui.nume.toPlainText()
            self.eco_abd_femeie.ui.qprenume = self.ui.prenume.toPlainText()
            self.eco_abd_femeie.ui.qvarsta = self.ui.varsta.toPlainText()
            self.eco_abd_femeie.ui.qdomiciliu = self.ui.domiciliu.toPlainText()
            self.eco_abd_femeie.show()


        elif self.ui.tip.currentIndex() == 0 and self.ui.sex.currentIndex()==1:
            self.eco_abd_barbat.ui.qnume=self.ui.nume.toPlainText()
            self.eco_abd_barbat.ui.qprenume = self.ui.prenume.toPlainText()
            self.eco_abd_barbat.ui.qvarsta = self.ui.varsta.toPlainText()
            self.eco_abd_barbat.ui.qdomiciliu = self.ui.domiciliu.toPlainText()
            self.eco_abd_barbat.show()





if __name__ == '__main__':
    app = QApplication(sys.argv)

    # create and show mainWindow
    mainWindow = MainWindow()
    mainWindow.show()

    sys.exit(app.exec_())

This is the code that make it work for me, I cant explain 100% why it works, but i think before I reloaded the same variable over and over, as now I am forcing it to reload the ui from the file containing it every time the variable is accesed.

For some reason, without the self in front of variable (on self.formWindow, in open function), the second window would just close a split second after it opened.

This works for me, I mostly understand the reason why is performing like this, but if someone can make things clear about the self part, I'm happy to learn.

# import some PyQt5 modules
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QImage
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QTimer




from lib.eco_barbat_ui import *
from lib.eco_femeie_ui import *
from lib.eco_main_ui import *

class eco_abd_barbat(QWidget):
    def __init__(self):
        # call QWidget constructor
        super().__init__()
        # self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.ui = Ui_EcoAbdBarbat()
        self.ui.setupUi(self)




class eco_abd_femeie(QWidget):
    def __init__(self):
        # call QWidget constructor
        super().__init__()
        # self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.ui = Ui_EcoAbdFemeie()
        self.ui.setupUi(self)


class MainWindow(QWidget):

    # class constructor
    def __init__(self):
        # call QWidget constructor
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.open)

    def select(self):
        if self.ui.tip.currentIndex() == 0 and self.ui.sex.currentIndex() == 0:
            return eco_abd_femeie()
        else:
            return eco_abd_barbat()


    def open(self):
            self.formWindow=self.select()
            self.formWindow.ui.qnume = self.ui.nume.toPlainText()
            self.formWindow.ui.qprenume = self.ui.prenume.toPlainText()
            self.formWindow.ui.qvarsta = self.ui.varsta.toPlainText()
            self.formWindow.ui.qdomiciliu = self.ui.domiciliu.toPlainText()
            self.formWindow.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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