Search code examples
pythonunit-testingtkintermodal-dialogbatch-processing

How to disable Tkinter dialogs while running in batch mode


I've got what must be quite a common problem, but I can't seem to find an obvious solution.

I'm trying to include an automated test as part of my TFS build script which distributes Python. Unfortunately, however, one of the developers has put this in one of the package's _ _ init _ _.py:

import Tkinter
import tkMessageBox 

warningWindow = Tkinter.Tk()
warningWindow.withdraw()
tkMessageBox.showwarning("WARNING", "blah, blah, blah")

warningWindow.destroy()

This is good in and of itself - but not when it is being run in batch (it makes everything pause).

My original hope was that Tkinter had a config setting that disables popups, but if it does then I can't find it.

The other way is to 'hack in to' the tkMessageBox module, and replace showwarning with something that doesn't do anything. I've seen this done, but I can't remember how to do it...

I'm rather hoping that there's a better way than either?


Solution

  • OK, in the absence of a better solution, I will say what I did:

    import os
    if not os.environ.has_key('SILENT_MODE'):
        import Tkinter
        import tkMessageBox 
    
        warningWindow = Tkinter.Tk()
        warningWindow.withdraw()
        tkMessageBox.showwarning("WARNING", "blah, blah, blah")
    
        warningWindow.destroy()
    

    And in the start script:

    SET SILENT_MODE=1
    python -m unittest discover
    

    This is OK I guess, but I'd hoped for something more elegant/generic.