Python Forum

Full Version: Show second window using classes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there,

I have this, it works ok and I have to admit it is cobbled from things that I have found and I am still struggling with classes, I really didn't think I was that stupid.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
Qt4 tutorial using classes

This example will be built
on over time.
"""

import sys
from PyQt4 import QtGui, QtCore


class Window(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 1600, 900)
                
        new_window = QtGui.QPushButton("New", self)
        new_window.setGeometry(QtCore.QRect(1400, 800, 120, 26))
        new_window.clicked.connect(self.sub_window)
        self.dialog = Form(self)
        quit_btn = QtGui.QPushButton("Quit", self)
        quit_btn.setGeometry(QtCore.QRect(1400, 850, 120, 26))
        quit_btn.clicked.connect(self.closeEvent)
        
    def sub_window(self):
        self.dialog.show()

    def closeEvent(self):
        message = QtGui.QMessageBox.question(self, 'Message',
                                             "Are you sure?", 
                                             QtGui.QMessageBox.Yes
                                             | QtGui.QMessageBox.No,
                                             QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            sys.exit()
        else:
            pass


class Form(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(Form, self).__init__()
        self.setGeometry(1000, 500, 300, 200)
        
        
def main():
    app = QtGui.QApplication(sys.argv)
    main_window = Window()
    main_window.show()
    sys.exit(app.exec_())    

if __name__ == "__main__":
    main()
 

So the second window is going to be like a contextual menu screen with a series of buttons, click on one button and the window will change to a different window taking up exactly the same space. That window will always be there but the plan was to add fuctionality to the application over time, I thought the best way was using a class for each context as one window will be a data input window, one of them will become a settings window and one will be a messageing window, all of these things I have working but only on the command line, but I am struggling with the whole classes thing still. What I was hoping was to create like a place marker that I could just drop a class into, that way the class hopefully would not interfere with the rest of the program, making the application nicely modular.

So currently, I now have worked out how to make a second window be there from a class, but I don't understand it enough to remove the button that makes it appear, I was hoping it was as simple as
Form.show()
But of course it hasn't worked, how do I now make this window be there without a button press. I just can't work it out.
Form.show()
does not work because Form is the class and not the created instance of Form.

The instance of Form is created by
self.dialog = Form(self)
and has a ref to it in the instance variable dialog.

To call the instance of Form's show method you would call
self.dialog.show()
just as it is in the button event handler.
Thank you once again Yoriz, this now works, I have stripped it down to the basics of what the question was addressing and I have also formatted the code in what appears to me to be a more standard pythonic format, please correct me if I have that wrong. This is what I have that now does exactly what I was after, I have set geometry the same as in my project and as the sub_windows will all be the same size and location, geometry is defined in init definition of the Form class.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
Qt4 example to embed
dialog box within window
"""

from PyQt4 import QtGui, QtCore

class Form(QtGui.QWidget):
    """Menu window"""
    def __init__(self, parent=None):
        super(Form, self).__init__()
        self.setGeometry(1000, 200, 400, 500)


class Window(QtGui.QMainWindow):
    """Main Window"""
    def __init__(self, parent=None):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 1600, 900)
        self.new_window = Form(self)
        self.show() 
        self.dialog = Form(self)
        self.dialog.show()


         
def main():
    app = QtGui.QApplication(sys.argv)
    main_window = Window()
    sys.exit(app.exec_())    
 
if __name__ == "__main__":
    main()
Kind regards
iFunk