How to reference the app context from window on appcontext?

Heads up! You've already completed this tutorial.

Wu_Yi | 2020-05-11 10:51:10 UTC | #1

I got a question on how to reference the appcontext while the window component are itself part of the appcontext attribute. Here is my code snippet setup so far.

python
#Main Window Component
class MainWindow(QMainWindow):
    def __init__(self,ctx):
        super().__init__()  # inherit the parent class attributes
        self.ctx =ctx

#Application Context Class
class AppContext(ApplicationContext):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.window = MainWindow()

    # show the main window and
    def run(self):
        self.window.show()
        return self.app.exec_()

    #method to get the resource image
    def getImg(self):
        return self.get_resource('sample.png')


if __name__ == '__main__':
    appctxt = AppContext()
    appctxt.run()
    sys.exit(appctxt.app.exec_())

I guess my question is how could i pass appcontext as a object reference into the MainWindow compnent as reference when MainWindow itself is initialised inside the appcontext?

Thanks for helping out on this dumb question.


Wu_Yi | 2020-05-11 10:51:03 UTC | #2

Actually, I figured that all I need to do is as below to cross reference class. self.window = MainWindow(self)

Problem Solved.

PyQt/PySide 1:1 Coaching with Martin Fitzpatrick — Save yourself time and frustration. Get one on one help with your Python GUI projects. Working together with you I'll identify issues and suggest fixes, from bugs and usability to architecture and maintainability.

Book Now 60 mins ($195)


martin | 2020-05-15 20:14:05 UTC | #3

Great you worked it out, but another way you can do this is by passing your AppContext to your MainWindow as you create it. In your example code MainWindow accepts a single parameter ctx which is intended for this.

Get your project to market with focused expert help
Martin Fitzpatrick Python Application Launch Support
Find out More

Launch Support for your Python Applications

Comprehensive code reviewBugfixes & improvements • Maintainability advice and architecture improvements • Design and usability assessment • Suggestions and tips to expand your knowledgePackaging and distribution help for Windows, Mac & Linux • Find out more.

[quote="Wu_Yi, post:1, topic:182"]

python
#Main Window Component
class MainWindow(QMainWindow):
    def __init__(self, ctx):  # this is receiving the context as ctx
        super().__init__()  # inherit the parent class attributes
        self.ctx =ctx # storing a reference to the context

#Application Context Class
class AppContext(ApplicationContext):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.window = MainWindow(self) # add self here to pass the context object to the MainWindow

[/quote]

Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PyQt6 Edition) The hands-on guide to making apps with Python — Over 15,000 copies sold!

More info Get the book


Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

How to reference the app context from window on appcontext? was written by Martin Fitzpatrick.

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.