Python Forum
Show second window using classes
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Show second window using classes
#1
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.
Reply
#2
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.
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 341 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] How to show text in window? wxnerd 4 3,658 Jun-20-2020, 04:16 PM
Last Post: wxnerd
  tkinter window and turtle window error 1885 3 6,623 Nov-02-2019, 12:18 PM
Last Post: 1885
  [PyQt] Can't show PyQt5 mediaPlayer in window DecaK 1 5,335 Sep-15-2018, 09:54 AM
Last Post: Axel_Erfurt
  update a variable in parent window after closing its toplevel window gray 5 8,974 Mar-20-2017, 10:35 PM
Last Post: Larz60+
  using qt with classes basic window iFunKtion 0 2,854 Feb-06-2017, 09:50 AM
Last Post: iFunKtion
  Show GPG keys in pyqt5 window Fred Barclay 1 4,531 Oct-24-2016, 07:09 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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