0

I have written a Python 3 data processing application.
It has a front end that can be run from a Linux command line, or a GUI, using tkinter.

If a am on a headless console, or coming via SSH, a Windows manager is not available, so the GUI won’t work. What is the recommended way of detecting this, so I don’t try the GUI and just run the command line version?

The best I can come up with is:

try:
   start_gui()
except:
   start_commandline()

But think there must be a better way!

Ideally the solution would be platform independent!

  • A better way would be to have two explicit commands provided, and actually call the one you want. – Mad Physicist Apr 26 '20 at 07:31
  • Check out [argparse](https://docs.python.org/2/library/argparse.html) when calling your script you could just append -G True argument for starting a GUI or -G False if you want the command line version – Björn Apr 26 '20 at 08:01
  • Relevant [tkincter-tclerror-no-display-name-and-no-display-environment-variable](https://stackoverflow.com/questions/37604289) – stovfl Apr 26 '20 at 08:59
  • The way you do it is platform independent and works, why do you need something better, and how do you define "better"? Asking for a "best" way is asking for opinions, and opinionated questions are off-topic on stackoverflow. – Bryan Oakley Apr 26 '20 at 15:50
  • Thanks for the feedback. I don’t really want to go down the arg parsing route, as the behaviour I want it for the GUI to open if no args are present (default behaviour), but fall back to the command line if tk/tcl is not available. Seems like my general approach is considered OK, so I’ll progress that way, thanks.. – Colin Robbins Apr 28 '20 at 11:21

0 Answers0