PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 20, 2022

[FIXED] What is a reasonable way of keeping track of multiple windows in PyQt?

 October 20, 2022     data-members, pyqt, windows     No comments   

Issue

I'm writing a PyQt application that shall feature multiple windows. Right now, I am interested in having one of two windows open at a time (so a click of a button in one window causes a switch to the other window). What is a reasonable way of keeping track of multiple windows in a PyQt application? My initial attempt, as shown below, essentially stores instances of the QtGui.QWidget in data members of a global instance of a simple class.

I'm new to PyQt. Is there a better way to approach this?

#!/usr/bin/env python

import sys
from PyQt4 import QtGui

class Program(object):
    def __init__(
        self,
        parent = None
        ):
        self.interface = Interface1()

class Interface1(QtGui.QWidget):
    def __init__(
        self,
        parent = None
        ):
        super(Interface1, self).__init__(parent)

        self.button1 = QtGui.QPushButton(self)
        self.button1.setText("button")
        self.button1.clicked.connect(self.clickedButton1)

        self.layout = QtGui.QHBoxLayout(self)
        self.layout.addWidget(self.button1)

        self.setGeometry(0, 0, 350, 100)
        self.setWindowTitle('interface 1')
        self.show()

    def clickedButton1(self):
        self.close()
        program.interface = Interface2()

class Interface2(QtGui.QWidget):
    def __init__(
        self,
        parent = None
        ):
        super(Interface2, self).__init__(parent)

        self.button1 = QtGui.QPushButton(self)
        self.button1.setText("button")
        self.button1.clicked.connect(self.clickedButton1)

        self.layout = QtGui.QHBoxLayout(self)
        self.layout.addWidget(self.button1)

        self.setGeometry(0, 0, 350, 100)
        self.setWindowTitle('interface 2')
        self.show()

    def clickedButton1(self):
        self.close()
        program.interface = Interface1()

def main():
    application = QtGui.QApplication(sys.argv)
    application.setApplicationName('application')
    global program
    program = Program()
    sys.exit(application.exec_())

if __name__ == "__main__":
    main()

Solution

Have a single main window with a QStackedWidget to hold the different interfaces. Then use QStackedWidget.setCurrentIndex to switch between the interfaces.

Also, try to avoid using global references. If you want GUI components to communicate with each other, use signals and slots. You can easily define your own custom signals if there are no suitable built-in ones.



Answered By - ekhumoro
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing