Simple Python Code to Auto Lock Your Computer!

About: A Computer Science and Engineering student and a Hobbyist. love to make robots,electric devices,diy things and Programming them by Myself. My youtube channel https://www.youtube.com/fussebatti Follow me on ...

How can you lock your Computer screen?? Simple way is to press the 'windows' + 'l' combination key. And there's a lot of other ways out there.

I do coding in python and made some codes to recognize or identify faces or objects. Then I thought what if I can control my pc based on those codes conditions?? Suppose to lock your pc if it detects someone else other than you? [ Many of you might think "I need this", I'll make that for you too.]

Turned out python has a library called pyautogui which is very powerful and yet easy to use. pyautogui works both for pyton2 and python3.

So today I'll write a simple and easy code that you can run to lock your pc's screen. You might also insert those few lines in your code to make an option to lock the pc.

LET'S HOP IN!!

Update: I Have started making that . find it here at github

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Things You'll Need Beforehand

First of all you'll need to install python. I assume you already have the latest version of python which is python3.7.1 by now. If not, there's a lot of tutorial out there, just google it.

After that you'll need to install pyautogui library. I did pip install like this- open windows search then type command prompt or powershell then type this

pip install pyautogui

Then some hodgepodge writings will come and then type python on your prompt and hit 'ENTER'. Then import pyautogui library (see image 2).

>>> import pyautogui
>>> 

If no error shows then congrats you have successfully installed pyautogui.

Step 2: The CODE

Although we can just lock our pc by keyboard combo WIN + L but for some reason that doesn't work using pyautogui. But pyautogui can write, click etc. I took advantage of that. If we type "rundll32.exe user32.dll, LockWorkStation" in the command prompt or powershell then it will lock the pc too. So I wrote the code so when you run the code it opens cmd (command prompt) then types that line and hits 'ENTER'. I used the sleep function to wait for some mili seconds so that the code does't run too faster than the system.

Copy the code from below or Download Code from github.

<p>"""<br>pyautogui automatic pc lock code.
by Ashraf Minhaj.
find me- ashraf_minhaj@yahoo.com
"""</p><p>import pyautogui   #import pyautogui library</p><p>from time import sleep    #import sleep - for the delay</p><p>pyautogui.hotkey('win', 'r')  #windows_key + Run</p><p>pyautogui.typewrite("cmd\n") #cmd to open command prompt
sleep(0.500)       #500 milisecond delay (depends on your computer speed)</p><p><br>#write the code then press enter('\n') thus pc will auto-lock</p><p>pyautogui.typewrite("rundll32.exe user32.dll, LockWorkStation\n")</p>

That's it now run the code. You can use an editor to edit the code as you want or can run it by command prompt or powershell ( see picture 1). Remember the path where you have saved the code (picture 2).

Step 3: Finsh!

So run the code and see the magic. It actually ain't any magic as you know what it does but it seems pretty "osthir" (cool).
I'll make codes to lock screen if some1 elses face is detected in the next. Stay tuned and pray for me.
Thanks a lot.

I made it!!! Still need to make it .exe file. Watch the video demonstration.

Safe and Secure Challenge

Participated in the
Safe and Secure Challenge

Share

    Recommendations

    • Indoor Lighting Contest

      Indoor Lighting Contest
    • Make It Fly Challenge

      Make It Fly Challenge
    • Growing Beyond Earth Maker Contest

      Growing Beyond Earth Maker Contest

    2 Discussions

    0
    None
    Ashraf Minhajaudreyobscura

    Reply 6 months ago

    I did and it's pending for approval. Please vote for that if it gets approved. Thanks a lot.