Important: Please read the 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()                 

  • Lifetime Qt Champion

    @Julsenn Ist dieses Fenster deine eigene Classe? Wenn ja, dann kannst du ü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

  • 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.


  • Lifetime Qt Champion


    def showEvent(self, ev):
        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()
    #here is the show event ?

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

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

    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()
        def open_text(self):                    #this automatically when window start
            with open("test.txt", "r") as f:
               file_text = f.readlines()[0]                 
        def setupUi(self, MainWindow):
            MainWindow.resize(800, 600)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.eingabe_1 = QtWidgets.QTextEdit(self.centralwidget)
            self.eingabe_1.setGeometry(QtCore.QRect(80, 180, 301, 51))
            self.eingabe_2 = QtWidgets.QTextEdit(self.centralwidget)
            self.eingabe_2.setGeometry(QtCore.QRect(80, 260, 301, 51))
            self.speichern_btn = QtWidgets.QPushButton(self.centralwidget)
            self.speichern_btn.setGeometry(QtCore.QRect(420, 180, 121, 51))
            self.oeffnen_btn = QtWidgets.QPushButton(self.centralwidget)
            self.oeffnen_btn.setGeometry(QtCore.QRect(580, 180, 131, 51))
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
            self.statusbar = QtWidgets.QStatusBar(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()

  • @Julsenn

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

  • @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