14

What would be the best way of separating program logic to the GUI code?

I wanted different GUI (GTK, KDE, CLI) code using the same program logic.

I was thinking of using different python module (winecellar-common, winecellar-gtk, winecellar-cli) not sure how I would do this and if its the best way.

*EDITED*

Just to add to my question what would be the best way to organize the projects file structure and build platform with different modules. Keep in mind its mainly being used on Ubuntu.

Samuel Taylor
  • 1,181
  • 2
  • 14
  • 25
  • Wanting to be frontend agnostic is a noble goal, but (1) the second, third, ... GUI propably has very low priority and (2) it yields little, if any benefit - if you settle for one portable GUI toolkit, rewriting the same stuff in another one won't add any portability. –  Dec 23 '10 at 20:32

2 Answers2

12

Define functions or classes for your business logic in one module, and define your presentation in another, using those functions to get your presentation. You should almost entirely be using functions and classes from the main module in the GUI module. You should do the same thing for your CLI. That way, you can have different distributions with different interfaces, and not have to create a different "logic" file for each one.

Basically, you have the right idea. Just keep them as separate as possibility so that a.) you can support multiple interfaces easily and b.) you can easily make changes to the interfaces.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 3
    An addendum to this: you want to avoid what I think of as the "VB programmer" failure, where business logic is tied to GUI controls. Just make sure that almost all of your GUI events (`button.click()`) call methods on objects that actually do the heavy lifting. – asthasr Dec 23 '10 at 20:34
  • @syrion I couldn't have put it better myself. – Rafe Kettler Dec 23 '10 at 20:37
  • Just to add to my question what would be the best way to organize the projects file structure and build platform with different modules. Keep in mind its mainly being used on Ubuntu. – Samuel Taylor Dec 23 '10 at 22:22
  • @syrion I'm not completely sure if I understand your point. You mean that the gui shoudln't do anything "business logical" but a `button.click()` should call a method from the logic module rather than doing some data crunching itself? In order to keep up the separation of concerns here? – LCsa Jan 28 '19 at 10:56
  • 1
    That's correct, @LCsa. This is the logic behind most UX patterns. – asthasr Jan 28 '19 at 13:53
3

Maybe "Model-View-Controller" pattern will be useful for you. There is a nice tutorial with wxPython on implementing this architecture: http://wiki.wxpython.org/ModelViewController