7

I’ve writen a little python script that just pops up a message box containing the text passed on the command line. I want to pop it up only when the window —resulting from a previous call— is not open.

from Tkinter import *
import tkMessageBox

root = Tk()
root.withdraw() 

# TODO not if a window with this title exists
tkMessageBox.showinfo("Key you!", " ".join(sys.argv[1:]))

Any idea how to check that?

2 Answers 2

2

I believe you want:

if 'normal' != root.state():
    tkMessageBox.showinfo("Key you!", " ".join(sys.argv[1:]))
1
  • I’ve tried this, it doesn’t work, the new window opens anyway. Maybe I wasn’t clear enough: Two python interpreters are running at the same time. I want the second process to exit when the other one is running, i.e. when the window —or any window with that title— is already open.
    – Tibi
    Oct 13, 2008 at 12:01
0

The previous answer works accordingly to the code you have provided. You say it does not work because the answerer complies with "sois bête et discipliné" rule in that he did not add root.mainloop() to his code since your question does not either.

By adding the later line, for some reason caused by the event loop, you should test the exact string "withdrawn" as follows:

import tkinter as tk
from tkinter import messagebox
import sys


root = tk.Tk()
root.withdraw()

if 'withdrawn' != root.state():
   messagebox.showinfo("Key you!", sys.argv[1:])


root.mainloop()

Note: do not run this code otherwise your Terminal session will hang up. To circumvent this discomfort, you will have to reset the window state using either root.state("normal") which will lead to the message box to disappear as if a click on the Ok button occurred, or root.iconify() through which you can stop the Terminal session to hang up by right clicking on the tkinter icon appearing on your OS taskbar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.