Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Encapsulation issue
#1
Hi there,

I am building a GUI application that has multiple windows and I am having difficulty passing data between them all cleanly. Each window is in it's own class in it's own module, and I have a helper module to do things like create database connection, save to database that sort of thing. But the biggest problem I am finding is that if I instantiate another widget in the main window, e.g.
self.input = NewWidget()
self.input.show()
In order to use the helper functions with this, I end up using the local variable to make it work:
myVariable = self.input.ui_line_edit.text()
Making it seem rather useless to have as a seperate function as it still will have to be re written to be used else where.

How do you get around this issue?

I would post my code, but there is hundreds of lines
Reply
#2
If I understand the problem, I would solve this one of two ways. The first option would be to have an overall class handling the calculations that are displayed in the windows, which are attributes of the overall class. If necessary, the windows can have a link back to the overall class, and can access the other windows through the overall class. The second option would to cross link the windows to each other as attributes, so they can access the necessary features of each other.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Hi Ichabod,

Thanks for that, I do indeed have a separate class dealing with all the calculations etc, but where I am having difficulty is linking back to the overall class. For example, I am using the mainWindow class to handle displaying of any other windows, from here, the other windows will accept some form of data which gets saved as a dictionary (usually named doc as I am working with couchdb, and that can be easily sent to the database) The problem I am having is sending this doc "back" to the main window which is already open. My work around, is to instantiating everything from the main window which looks very messy to me and stops being Object Oriented also, as it becomes one script but with a couple of random classes that are the other widgets being used.

How would you send something back to an already open window?
Reply
#4
I only know the answer for wxpython. There an own module exist for that kind of things, the 'Publisher':
https://wxpython.org/docs/api/wx.lib.pub...class.html

I guess that other toolkits also have an similar modul.
Reply
#5
If you have a master class, you could do it like this:

class Top(object):

    def __init__(self, n):
        self.bottoms = [Bottom(self) for bottom in range(n)]

    def send(self, data):
        for bottom in self.bottoms:
            bottom.receive(data)

class Bottom(object):

    def __init__(self, top):
        self.top = top
        self.data = []

    def receive(self, data):
        self.data.append(data)

    def send(self, data):
        for bottom in self.top.bottoms:
            if bottom != self:
                bottom.receive(data)
This way the Bottom instances can send data to each other through their link to the Top instance. The Top instance can also send data down to the Bottom instances. You could add a send_up method to the Bottom class so Bottoms could send data to the Top.

Without a top you can do cross linking:

class Side(object):

    def __init__(self):
        self.siblings = []
        self.data = []

    def link(self, side):
        self.siblings.append(side)
        side.siblings.append(self)

    def receive(self, data):
        self.data.append(data)

    def send(self, data):
        for sibling in self.siblings:
            sibling.send(data)

sides = []
for side in range(3):
    new_side = Side()
    for other in sides:
        new_side.link(other)
    sides.append(new_side)
Here the Side instances can send data to each other through their cross linkages.

Of course, this is just broadcasting to everything. If your windows have different roles, you could just make an attribute for each role to fine tune the communication.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Encapsulation codinglearner 1 1,249 Mar-24-2022, 07:26 PM
Last Post: deanhystad
  Function encapsulation Oldman45 4 2,254 Jan-22-2021, 11:38 AM
Last Post: Oldman45
  Preserve Encapsulation while Displaying Information QueenSvetlana 13 6,865 Dec-07-2017, 06:13 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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