top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Ideal way to separate GUI and logic when coding in Python?

0 votes

I'm developing a program with a GUI in tkinter, and I'm wondering what is the best, 'most pythonic' way of doing this? I could, obviously, write a monolithic block of code.

I can define the logic and the GUI as two separate classes and then call from those classes within a single script.

Those are probably less than ideal.

But how then do I separate out the logic and the GUI? Because the GUI needs to have commands defined for button presses and so forth, and those have to come from the logic section. Do I write the logic in a separate file and then import those to the GUI, and then run it there? Wouldn't it be better to have one file for the GUI, one for the logic, and then a third one that imports from both and then executes the app? How do I do that? (Examples would be nice, if possible.)

posted Jul 13, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote

Recommend PyPubsub:

answer Jul 13, 2013 by anonymous
0 votes

The important thing is that the application logic is separate from the user interface. It doesn't matter whether than means "two functions in the same module", or "two modules". Whether you decide to split your code into two modules depends on how complex your code is.

For example, if you are writing a "Hello World" application, you would be nuts to split this into two files.

def greet(name):
 return "Hello, %s!" % name

def do_greet(name=None):
 if name is None:
 name = raw_input("What is your name? ").strip()
 print greet(name)


Only you know how big and complex your application is, so only you know whether or not you should separate the GUI interface from the internal logic. But, my guess is that for anything non-trivial, you probably should have one main module handling the UI, and a second (and possibly more) modules handling the implementation, plus at least one other module for testing.

answer Jul 13, 2013 by anonymous
0 votes

You should investigate strategies like model view presenter, and test or behavior driven development.

My recommendation echos the other advice you've been given. Basically you think of your application in terms of two problems or domains. One is the what that you want no do. What information do you need?

The other problem is how do you get that information from the user and return to them information than they need?

Regardless of which side you have driving, UI or Presenter, having a design such as this allows for much cleaner code.

answer Jul 13, 2013 by anonymous
Similar Questions
0 votes

How can write a python script which will execute some perl scripts in a gui and shouldn't be post processed....?

+2 votes

Also is there a way to detect if the user presses a key in Python that works on most OS's? I've only seen 1 method and that only works in Python 2.6 and less. If you get the key, can you store it in a variable?

+2 votes

Is there a way to use pdb to debug Google apps written in Python? When I start the development system to run the app "test" like this -

'./google_appengine/ ./test'

  • I'd like to send the program into debug. I couldn't see anything in the documentation how to do this. If I do this - python -mpdb './google_appengine/' './test'
  • then goes into debug but doesn't know anything about "test".
+1 vote

I'd like to install ALL Python packages on my machine. Even if it takes up 4-5GB, or more, I'd like to get everything, and then use it when I need it. Now, I'd like to import packages, like numpy and pandas, but nothing will install. I figure, if I can just install everything, I can simply use it when I need it, and if I don't need it, then I just won't use it.

I know R offers this as an option. I figure Python must allow it too.

Any idea how to grab everything?