简体   繁体   中英

pyqt5 creating new window

I'm trying to create new window when fieldE button is pressed but it is not happening. Mapeditor() class is working so I am thinking that the problem is in line when I call the fieldE.clicked.connection -function. Any tips?

def startGpressed(name):
    print(name)

def fieldeditorOPEN():

   fieldScreen = Mapeditor()


def main():
     app = QApplication(sys.argv)
     ex = App()
     buttons = ex.initUI()
     startG = buttons[0]
     fieldE = buttons[1]
     startG.clicked.connect(lambda:(startGpressed("Game starting")))
     fieldE.clicked.connect(partial(fieldeditorOPEN))
     startG.show()
     fieldE.show()
     sys.exit(app.exec_())

main()

The variables that are created in a function are eliminated when the function finishes executing, so even if the window is shown, it will be closed and eliminated in an instant. So the solution is to extend its life cycle so that it can be created as a global variable:

fieldScreen = None
def fieldeditorOPEN():
   global fieldScreen
   fieldScreen = Mapeditor()
   fieldScreen.show()

Another option is to create Mapeditor in main and have the clicked show it:

 # ...
 fieldScreen = Mapeditor()
 fieldE.clicked.connect(fieldScreen.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