Ok. i have a python program launch in the windows command prompt window after you log in to increase security. (its simple, it asks three questions and if you answer all of them right the program ends) is there anyway to make it so that you cant just hit the x at the top of the screen and exit this? or have it so if u hit the x itll ask for a password and wont close till u guess the right password? any help is appreciaed.

Ryan

I don't believe Python can do that to the console. You might be able to achieve something like this using some GUI toolkit like wxPython or tkinter.

But if you don't want to do that a possible workaround would be to write another small script to run alongside your main program and start it again if the questions have not been answered. You wouldn't want this secondary program to have a window either, so you should save it as a .pyw file when it is working.

A very sloppy example:

import os
import time

AllQuestionsCorrect = False #Set this to true from the main program when it needs to be

while (AllQuestionsCorrect == False): #do this over and over while AllQuestionsCorrect is false
    time.sleep(0.5) # Sleep a little to avoid 100 percent processor usage

    # --- Check if main program is running ---

    if (NotRunning):
        os.system("main program directory")

If you want to do this you'll need to download a third party module. Python doesn't have a built-in way to check running processes in Windows. WMI seems like it will do the job.

Here's a link to its download page: http://timgolden.me.uk/python/wmi/index.html
And here are some useful examples: http://tgolden.sc.sabren.com/python/wmi/cookbook.html#running_processes

The canonical simple solutions are variants on each other

  • Write a loop around the body of your code that asks the user to 'q' if they wish to quit, else it will go around again.
  • Just say raw_input('Press return to quit') at the bottom of the script. Assuming you are using Python 2.x. Else use input(...)

i dont know what your talking about wildbamaboy. i am just learning the fundamentals. is there a way to say "on exit"? or something like that?

to add to my last post, i have a friend that is a programmer by profession. he doesnt use python anymore but he believes there was a line you could use.

This article has been dead for over six months. Start a new discussion instead.