Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Separate program logic and GUI code?

Tags:

python

pygtk

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.

like image 764
Samuel Taylor Avatar asked Dec 23 '10 20:12

Samuel Taylor


People also ask

How do I add a GUI to a Python program?

If you're new to creating GUI's in Python the basic steps are to: Import the Tkinter module: import Tkinter, top = Tkinter.Tk() Create the GUI main window that will “house” your GUI and widgets. Add whatever widgets your GUI needs including buttons, labels, lists etc.

Can you code a GUI in Python?

Creating a simple graphical user interface (GUI) that works across multiple platforms can be complicated. But it doesn't have to be that way. You can use Python and the PySimpleGUI package to create nice-looking user interfaces that you and your users will enjoy!

Is Python GUI cross-platform?

3–wxPythonwxPython is a cross-platform GUI toolkit you can use to create robust, functional GUIs in a simple and easy manner. The implementation is a set of Python extension modules that wrap the GUI components of the wxWidgets cross-platform library, which is written in C++.


2 Answers

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.

like image 122
Rafe Kettler Avatar answered Sep 28 '22 10:09

Rafe Kettler


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

like image 32
kirasole Avatar answered Sep 28 '22 09:09

kirasole