Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nesting Windows
#1
I have a Main Window with a button which imports a Second Window.
The Second Window has a Home button which imports the Main Window.
When the Second Window button in the Main Window is clicked, the Second Window is displayed. When the Home button in the Second Window is clicked the Main Window is displayed.
When the Second Window button in the Main Window is clicked again the program hangs.
Can someone please tell me why this happens?

#Displays Main window
import sys, os
from PyQt4 import QtCore, QtGui, uic
win1 = uic.loadUiType("MainWin.ui")[0]
class UserWindow(QtGui.QMainWindow, win1):
    def __init__ (self,parent=None):
        QtGui.QMainWindow.__init__(self,parent)
        self.setupUi(self)
        self.SecondWinBtn.clicked.connect(self.SecondWin)
        self.SignOutBtn.clicked.connect(self.LogOut)       

    def LogOut(self):
        self.close()       

    def SecondWin(self):       
        import pyqtSecondWin
        self.close()     

    def LogOut(self):
        self.close()
                
app=QtGui.QApplication(sys.argv)
Win1=UserWindow(None)
Win1.show()
app.exec_()
#shows Second window
import sys, os
from PyQt4 import QtCore, QtGui, uic

win1 = uic.loadUiType("SecondWin.ui")[0]

class OverdueWindow(QtGui.QMainWindow, win1):
    def __init__ (self,parent=None):
        QtGui.QMainWindow.__init__(self,parent)
        self.setupUi(self)
        self.SignOutBtn.clicked.connect(self.LogOut)
        self.HomeBtn.clicked.connect(self.DisplayMainWin)

    def LogOut(self):
        self.close()

    def DisplayMainWin(self):
        import pyqtDisplayMainWin
        self.close()

app=QtGui.QApplication(sys.argv)
Win1=OverdueWindow(None)
Win1.show()
app.exec_() 
Reply
#2
Is it odd to have two QApplication instances running. Instead you should make one subclass for each window, and simply toggle them by using hide() and show() method. close() destroy the window, so it makes no sense if you intend to create it again later. Also, if you do use close(), you must add app.setQuitOnLastWindowClosed(False) or the program will likely exit unexpectedly.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Nesting menu buttons tkinter for python Mocap 1 2,716 Jul-18-2019, 11:46 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020