python - gui design question -


i use tkinter, question general.

this first time i'm designing gui. each of objects modeled class. don't understand how tie gui class hierarchy rest of class hierarchy.

for example, have creatures:

# before had gui in code class creature:   def current_position() # returns real-time x, y coords     # ... 

i want display them circles move around on canvas. seemed reasonable me graphical representation of movement should provided method update_display in class creature. however, means class creature has know details gui. in addition, class app(tkinter.tk) redraw method need know list of existing creature instances in order call update_display methods. that's way dependency.

what's right approach?

the accepted pattern called model/view/controller. have controller object both knows creatures (your "model") , gui (your "view"). controller responsible connecting two. example, view have "draw_creature" method accepts creature input. controller iterate on creatures, passing one-by-one view. or, each creature has "draw" method accepts view parameter, , view asks each creature draw itself, passing reference view.

with pattern in place have multiple views creatures don't have updated know them. likewise, change how store , manage creatures , view doesn't need know that.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -