0

I would like to know, what is the concept of information flow in GUI based apps, or any other app with same problem. When you have two seperate classes and their objects, how is the messeging process done between them. For example you have a GUI and AppLogic.

Scenario 1: Button is pressed -> GUI is processing event -> calls AppLogic method image_clicked()

Scenario 2: HTTPServer gets a message -> AppLogic receives image -> AppLogic calls GUI method render_image()

enter image description here

The problem is that you cannot reference classes each other because the first class does not know the second one (here AppLogic does not know GUI class):

class AppLogic():
    gui : GUI

    def image_clicked(self):
        pass #not important
    
class GUI():
    app_logic : AppLogic
    
    def render_image(self):
        pass #not important

I know this is more like go to school and study problem, but I would like to know how these problems are sovled, or some common practices. At least link with some detailed information. I am not able to name the problem right to find the answer.

Edit:

I can use this code without explicit type declaration and it works. But when I want to call functions of gui in AppLogic class definition, intellisense does not hint anything, because it does not know the type of attribute gui. And I don't think that it is good practice to use code like that.

class AppLogic():
    
    def __init__(self) -> None:
        self.gui = None

    def image_clicked(self):
        pass #not important
    
class GUI():
    
    def __init__(self) -> None:
        self.app_logic = None
        
    def render_image(self):
        pass #not important

app = AppLogic()
gui = GUI()

app.gui = gui
gui.app_logic = app
  • Answered here. Key is: "forward references" https://stackoverflow.com/questions/74451246/explicit-data-type-declaration-of-mutual-class-references-disabled-intellisense?noredirect=1#comment131429757_74451246 – Daniel Šebík Nov 15 '22 at 20:26

3 Answers3

0

You need to initialize your variables.

gui = Gui()

then you can call the methods

For example:

class AppLogic:
  gui: Gui

  def image_clicked(self):
      gui = Gui()
      gui.render_image()
    
class Gui:
  logic: AppLogic

    def render_image(self) :
        pass

Or you can initialize your variable directly

gui: Gui = Gui()

I hope this answers your question

Raboro
  • 76
  • 3
  • Yes, I know, sorry that I did not initialized them here explicitely, but I wanted to be minimalistic. The problem is that when I run the script, it throws error at `gui : GUI`, because at the time of processing, it does not know the class `GUI`. – Daniel Šebík Nov 15 '22 at 18:14
  • I would make two files each containing one class. Then you can import each class. See my new code below. – Raboro Nov 15 '22 at 18:26
  • I have tried it, but you get an error with circular import: partially initialized module 'test' has no attribute 'AppLogic' (most likely due to a circular import). I have editted the question and the point is how to get the intellisense to work when I don't explicitely specify the data type. Or some better practices of handling this situation. – Daniel Šebík Nov 15 '22 at 18:31
  • I wrote some new code wait a sec – Raboro Nov 15 '22 at 18:48
0
from LogicClass import Logic

class Gui:
    logic: AppLogic = AppLogic()

    def render_image(self) :
        pass

and:

from GuiClass import Gui

class AppLogic:
   gui: Gui

   def image_clicked(self):
      gui = Gui()
      gui.render_image()
    
Raboro
  • 76
  • 3
0
from Gui import Gui

class Logic:

    def __init__(self):
        self.gui = Gui()


if __name__ == "__main__":
    Logic()

and

class Gui:

  def __init__(self):
      print("GUI")
Raboro
  • 76
  • 3
  • yes, this will work, but look at my edited question please. But firstly, you don't have the attribute logic in your Gui class. And then, I am subjecting more another problem. The problem is with the explicit data type declaration and that without it, intellisense does not know the type of attribute, and hints are not possible. – Daniel Šebík Nov 15 '22 at 19:01
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Nov 16 '22 at 00:13