You seem to be implying that you'd maintain two versions of your application -- one which runs via Tk, and one which is a console app. This is a lot of extra work. Also, consider that X is running may not be the only appropriate time to use Tk -- Windows, for example, can run Tk apps without an X server available.

There are two problems here -- detecting whether to use a GUI, and creating an application that is dual-mode.

Since the former one is relatively straightforward, depending on the OS configuration and detection accuracy you need, let's look closely at the latter.

Maintaining two entire applications is probably not the best use of time. As soon as you need to make a change, you will have to be duplicating logic -- the potential for error is very high. To address that concern, you'll need to split up your application on the logic/interface boundary.

There are really two approaches. First, if your application is simple, you can abstract all of your interface functions (print a message, prompt the user, etc.) into subroutines. Then you can have each routine check to see if you've instantiated a MainWindow or not before deciding whether to use the GUI or text interface component.

The second, and probably better, apprach is to abstract your logic into a module. This module makes events available that a calling program can decide how to handle. Then, you write two pure-interface scripts (one Tk, one console) that present the interface and call the module for logic (and perhaps to poll for events).

The second is likely better in part because it allows a future maintainer to alter logic (say, to fix a bug) in the module without having to update either interface script. It also allows for easy expansion of interfaces -- since all the logic is in a module, adding a web interface (for example) would be simple.

Anima Legato
.oO all things connect through the motion of the mind


In reply to Re: How to detect X? by legato
in thread How to detect X? by blazar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.