adding new objects to existing class instance

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
FakirMaker
Posts: 5
Joined: Mon Jan 18, 2021 7:06 pm

adding new objects to existing class instance

Post by FakirMaker » Tue Mar 02, 2021 12:49 pm

Hi,
Curently i am building a windows type gui with a tft screen, and made many classes for it. i can create a window app like

Code: Select all

#to create

win = Window(50,50,100,100,color.white,"TestWindow",tft,touch)
text1 = textView(win,win.centerX,win.top+10,fonts.sans9,color.red,"Hello World")
button = Button(win,win.centerX,win.bottom,"TestButton")

#to show them

win.show()
text1.show()
button1.show()
is it possible the make windows objects a member of window instance i mean instead of the code above i would like to use

Code: Select all

win = Window(50,50,100,100,color.white,"TestWindow",tft,touch)
win.addObject(textView(win.centerX,win.top+10,fonts.sans9,color.red,"Hello World"))
win.addObject(Button(win.centerX,win.bottom,"TestButton"))

win.show()
these two codes serve the same purpose but i would like to add objects to window dynamically.
thanks

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: adding new objects to existing class instance

Post by pythoncoder » Tue Mar 02, 2021 1:26 pm

You might be looking for Python's setattr?

Code: Select all

class Foo:
    pass
 
>>> foo = Foo()
>>> setattr(foo, 'rats', 20)
>>> foo.rats
20
>>> 
Peter Hinch
Index to my micropython libraries.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: adding new objects to existing class instance

Post by stijn » Tue Mar 02, 2021 1:52 pm

Not entirely sure what you mean, but you want win.show() to also call text1.show() and button1.show() why not declare an addObject function?

Code: Select all

class Window:
  def __init__(self, *args):
    self.children = []

  def addObject(self, o):
    self.children.append(o)

  def show(self):
    foreach child in self.children:
      child.show()
    self._show()
On the other hand, since you already pass the window instance to each child you can have the children add themselves to their parent which is going to be less code and sort of makes sense:

Code: Select all

class Button:
  def __init__(self, win, *args):
    self.button = createButton(...)
    win.addObject(self)
    
# Usage is now:
win = Window(...)
button = Button(win, ....)

win.show()
Anyway: there are a couple of fairly standard ways to do this, and there are many GUI frameworks out there already so it's not a bad idea to first check how other frameworks deal with hierarchies, and then implement that.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: adding new objects to existing class instance

Post by pythoncoder » Tue Mar 02, 2021 4:38 pm

Agreed. Storing objects as members of a list makes more sense than adding them as attributes because you will need to be able to iterate over the list members. For example, if all or part of a window is obscured by another window, you need to consider what happens when the top window is closed. The revealed objects will need to be wholly or partially redrawn.

It's worth giving plenty of thought at the outset to the data structure. On a micro implementation there are also potential tradeoffs of code complexity vs performance.
Peter Hinch
Index to my micropython libraries.

FakirMaker
Posts: 5
Joined: Mon Jan 18, 2021 7:06 pm

Re: adding new objects to existing class instance

Post by FakirMaker » Tue Mar 02, 2021 5:19 pm

stijn wrote:
Tue Mar 02, 2021 1:52 pm
Not entirely sure what you mean, but you want win.show() to also call text1.show() and button1.show() why not declare an addObject function?

Code: Select all

class Window:
  def __init__(self, *args):
    self.children = []

  def addObject(self, o):
    self.children.append(o)

  def show(self):
    foreach child in self.children:
      child.show()
    self._show()
On the other hand, since you already pass the window instance to each child you can have the children add themselves to their parent which is going to be less code and sort of makes sense:

Code: Select all

class Button:
  def __init__(self, win, *args):
    self.button = createButton(...)
    win.addObject(self)
    
# Usage is now:
win = Window(...)
button = Button(win, ....)

win.show()
Anyway: there are a couple of fairly standard ways to do this, and there are many GUI frameworks out there already so it's not a bad idea to first check how other frameworks deal with hierarchies, and then implement that.
thanks for reply, sometimes i forget how flexible python is, yes you are right i pass parent instance to all child object and each object can add itself to the parent. and i didn't know there are frameworks for window apps on tft screens, i will check the internet and of course it is better to modify to existing and tested code to adapt my app. thank you, you showed the path.

FakirMaker
Posts: 5
Joined: Mon Jan 18, 2021 7:06 pm

Re: adding new objects to existing class instance

Post by FakirMaker » Tue Mar 02, 2021 5:27 pm

pythoncoder wrote:
Tue Mar 02, 2021 4:38 pm
Agreed. Storing objects as members of a list makes more sense than adding them as attributes because you will need to be able to iterate over the list members. For example, if all or part of a window is obscured by another window, you need to consider what happens when the top window is closed. The revealed objects will need to be wholly or partially redrawn.

It's worth giving plenty of thought at the outset to the data structure. On a micro implementation there are also potential tradeoffs of code complexity vs performance.
thank you for reply, you are right about the tradeoffs, microcontrollers are not good candiates for such applications, but i will keep my app as simple as possible and do not want to draw many objects on a single window. This is an education project and i am trying to make a simple microcomputer based on micropython and giving an idea to the students how micropython is easy to use. I hope they will learn and make my simple framework better and better.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: adding new objects to existing class instance

Post by stijn » Tue Mar 02, 2021 7:29 pm

FakirMaker wrote:
Tue Mar 02, 2021 5:19 pm
and i didn't know there are frameworks for window apps on tft screens
I didn't necessarily mean frameworks especially for TFT screens. Just any GUI framework, principles are usually the same.

Post Reply