Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

PyQt5 öffnen Event



  • Guten Tag,

    ich versuche beim Start eines neuen Fensters eine Definition zu laden aber finde nichts dazu. Wie kann ich beim Start des Fensters automatisch die def load ansprechen ? Vielen Dank im vorraus.

        def load(self):  
    
    
            with open("Slot1.txt", "r") as f:
               file_text1 = f.readline()                 
               self.pushButton.setText(file_text1)
    

  • Qt Champions 2019

    @Julsenn Ist dieses Fenster deine eigene Classe? Wenn ja, dann kannst du https://doc.qt.io/qt-5/qwidget.html#showEvent überschreiben und dort load aufrufen.
    Oder du rufst load() dort auf wo du auch das Fenster erzeugst/anzeigst.



  • @jsulm danke schonmal fürs antworten ich verzweifel hier :D.
    Ja die class ist meine wie kann ich dann das load() event einsetzen also wo ?

    class Ui_Handbetrieb(object):
    
     
    
        def load(self):   # anfangsevent erstellen !!!                                           
            with open("Slot1.txt", "r") as f:
               file_text1 = f.readline()                  #zeilenangabe hinten
               self.pushButton.setText(file_text1)
    


  • jetzt sprech ich load durch einen button an aber ich will das des automatisch ausgefrührt wird beim start ohne das ich ein button klicke.

    self.Auswahlladen_btn.clicked.connect(self.load)
    

  • Lifetime Qt Champion

    Hi

    def showEvent(self, ev):
        load(self)
        return QWidget.showEvent(self, ev)
    

    Dies ist eine Funktion, die Qt kurz vor der Anzeige des Widgets aufruft



  • @mrjj
    Yes indeed to your answer. But perhaps you should remind the OP that showEvent() will be called as the window is minimized->maximized as well as when first shown, I think, and then he wouldn't always want to re-load() that file to initialize the button? IIRC I keep a static bool first_shown state variable in the widget.

    My German isn't good enough to translate the above :)



  • @JonB when the window open i want to call the def load(self) automatically



  • @Julsenn
    I know this :) @mrjj & @jsulm are trying to tell you that you need to override the showEvent() method of your QWidget or QMainWindow (" neuen Fensters"). You will call your load() method from there.



  • @JonB sorry i'm still amateur. You mean here ?

    
    if __name__ == "__main__":
        
            import sys
        
            app = QtWidgets.QApplication(sys.argv)
        
            Handbetrieb = QtWidgets.QWidget()
        
            ui = Ui_Handbetrieb()
        
            ui.setupUi(Handbetrieb)
            Handbetrieb.show()  #here is the show event ?
    
        
        
        
            
        
            sys.exit(app.exec_())


  • @Julsenn
    I can't type it all, but you will need to change over to like:

    class MeinHandbetrieb(QtWidgets.QWidget):
    
        def showEvent(self, ev):
            load(self)
            return QWidget.showEvent(self, ev)
    ...
    
    Handbetrieb = MeinHandbetrieb()
    ...
    Handbetrieb.show()
    
    

    I know you are an amateur, but for this and other things Qt you have to understand C++ sub-classing.



  • ok thank you but i don't know how i implement this when i use the code always comes NameError: name 'Ui_Handbetrieb' is not defined. . :(



  • from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_MainWindow(object):
    
        def save_text(self):
            with open("test.txt", "w") as f:
                text = self.eingabe_1.toPlainText()
                f.write(text)
    
        def open_text(self):                    #this automatically when window start
            with open("test.txt", "r") as f:
               file_text = f.readlines()[0]                 
               self.eingabe_2.setText(file_text)
        
        
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(800, 600)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.eingabe_1 = QtWidgets.QTextEdit(self.centralwidget)
            self.eingabe_1.setGeometry(QtCore.QRect(80, 180, 301, 51))
            self.eingabe_1.setObjectName("eingabe_1")
    
    
            
            self.eingabe_2 = QtWidgets.QTextEdit(self.centralwidget)
            self.eingabe_2.setGeometry(QtCore.QRect(80, 260, 301, 51))
            self.eingabe_2.setObjectName("eingabe_2")
    
    
            
            self.speichern_btn = QtWidgets.QPushButton(self.centralwidget)
            self.speichern_btn.setGeometry(QtCore.QRect(420, 180, 121, 51))
            self.speichern_btn.setObjectName("speichern_btn")
    
            self.speichern_btn.clicked.connect(self.save_text)
            
            self.oeffnen_btn = QtWidgets.QPushButton(self.centralwidget)
            self.oeffnen_btn.setGeometry(QtCore.QRect(580, 180, 131, 51))
            self.oeffnen_btn.setObjectName("oeffnen_btn")
    
            self.oeffnen_btn.clicked.connect(self.open_text)
            
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.speichern_btn.setText(_translate("MainWindow", "Speichern"))
            self.oeffnen_btn.setText(_translate("MainWindow", "Öffnen"))
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    
    


  • @Julsenn

        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        # insert the following line
        # (not the best way to do it, but simplest from where you are now)
        ui.open_text()
        MainWindow.show()
    


  • @JonB thank you so much, finally it works :D :D so easy :D



  • @Julsenn
    Yes :) But it probably is not the right way of doing it.

    When do you think you are going to call your save_text()? You are going to have a problem trying to do that the same way... :(



  • @JonB i dont want to call the save_text its just a TestWindow in my Main Projekt i dont have the save_text. Thank you.



  • but now when i start the window from a other window it does not work ? :(



  • @Julsenn
    Don't know what you mean. You can see the code and how it works, so long as ui.open_text() is called before whatever window has the self.pushButton() is shown then the text will be read in & set. Use a debugger, or put in some print()-type statements, to see what is/is not being called at what point. That is what developing programs is all about :)



  • @JonB okey thanks i try


Log in to reply