Wednesday, March 6, 2019

Python Code Snippets #12

Python Code Snippets #12

shambles python newb code-snippets-logo

56-Windows notification pop up

I found this awesome little snippet from a Reddit post.  I ‘Shambleized‘ it a little, and it works great.  I guess this could be linked to a timer or date, software update message, or anything really.

The ‘duration‘ parameter is in seconds, (then it fades out), and this is for Windows only. There is not much more to say about the code really, as it is so straight-forward.

The module name would suggest it is for Windows 10 only, but it works fine on my Windows 7, not sure about Windows XP though. A classic, simple Newb Code Snippet.

More information about the module here.

python-newb-code-snippets-12-windows-pop-up

Double-click inside code box to select all.

'''
    Use Windows pop up notification to show a desktop message.
    Tested on Windows 7 and works great.
    You may need to "pip install win10toast"
'''

from win10toast import ToastNotifier as toast

pop_up = toast()

pop_up.show_toast("Smeg Alert",  \
"Don't forget to turn your underwear inside out to double "  \
"wear time, and save the planet.", duration=8)

57-send Email with attachment

I did a post on this baby a few days ago, so check it out for more info on this.

sending-email-with-python-yagmail-pdf-test

'''
   Send Email with image attachment using gmail smtp
   You may need to pip install yagmail
'''
import yagmail

# set up your gmail credentials.
# any problems could be gmail blocking you
# or wrong pw etc. I had to allow unsafe apps
# in my gmail security settings to get
# this to work.
YAG_SMTP = yagmail.SMTP(user="your@gmail.com",  \
password="your-password", host='smtp.gmail.com')

# email subject
SUBJECT = 'Yagmail Test'

# email content with attached file from current dir,
# or state file location.
CONTENTS = ['Hi Dude', 'image attached.', 'some-image.jpg']

# send mail
YAG_SMTP.send('person@anymail.com', SUBJECT, CONTENTS)

58-List all running processes

I found this snippet on a nice programming site called ‘this pointer‘ . I haven’t ‘Shambleized’ it at all, so check out the sites page on this snippet for more info. I’m not getting lazy, I just can’t think of anything to say about it, (which is very unusual for me, maybe I’m getting depressed or something?), but I like it. The list is sorted by memory usage.

python-newb-code-snippets-12-list-running-processes

'''
    List all processes running on your system.

    Code borrowed with thanks from a great Programming
    site: https://thispointer.com

    You may need to 'pip install psutil.
'''

import psutil
# Iterate over all running processes

for proc in psutil.process_iter():
    try:
        # Get process name & pid from process object.
        processName = proc.name()
        processID = proc.pid
        print(processName , ' ::: ', processID)
    except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
        pass

59-Create hotkey shortcuts

This allows you to create a keyboard shortcut that calls a function when pressed.  It runs in the background until your defined stop keys are pressed.

Taken from a post on Reddit.

The default code that I first found uses the Windows key, so I may have to assume this is for Windows only, though Linux\Mac users could try taking out the Windows key of course, and see if it works for them.

Update: Should work on Linux, according to the keyboard module docs.

On my Windows 7, the stop key combination does not seem to work, I’ve tried changing it to a simpler combination, but it still doesn’t work for some reason. It is still a very useful bit of code though.

'''
Create hotkey shortcuts for anything.

For more snippets:
https://stevepython.wordpress.com/
You may neeed to "pip install keyboard"
'''
import os
import keyboard as kb

def example():
    '''This function will be called when the shortcut key combination,
       defined in main(), is pressed. You can run anything you want here.
       For this example I have just opened notepade.exe.'''

    os.startfile('notepad.exe')

def main():
    '''The shortcut defined in here will be the Windows key + control + r.
    To quit checking for keypresses is Win + control + escape key.
    You can change these keys to anything you want.'''

    short_cut = "Win+Ctrl+R"
    kb.add_hotkey(short_cut, example, args=None)
    kb.wait("Win+Ctrl+Esc")

main()

60-Pass parameter from Tk callback command

It is impossible, and totally annoying, that you can’t return a parameter from a Tkinter button, well you can, but it needs to use a lambda call.

I never understood how to do this until I chanced upon a good and clear explanation of how to do it, I lost the link to that page sorry. Here is my simplified example.

python-newb-code-snippets-12-pass-parameter-from-tk-callback-using-lambda

'''
   Pass a parameter from command callback
   in Tkinter using lambda.

   By Steve Shambles March 2019
   More gross negligence in Python at my blog:
   https://stevepython.wordpress.com/
'''

from tkinter import Tk, Button, LabelFrame

ROOT = Tk()
ROOT.geometry('200x60')
ROOT.title('Button Test')

def clk_but(butn_num):
    '''A button was clicked, print which one'''
    print(butn_num)

FRAME0 = LabelFrame(ROOT, text='')
FRAME0.grid()

BUT1 = Button(FRAME0, bg='skyblue', text='Click Me 1',
              command=lambda: clk_but("clicked button 1"))
BUT1.grid()

BUT2 = Button(FRAME0, bg='orange', text='Click Me 2',
              command=lambda: clk_but("clicked button 2"))
BUT2.grid()

ROOT.mainloop()

That’s all for now. I will be back very soon with Snippets #13. Lucky for some maybe!

Using Python V3.6.5 32bit, on Windows 7 64bit

Previous post:Python Email with attachment

Don’t forget to check out my other snippets:

Snippets #1, #2, #3, #4, #5, #6, #7, #8,  #9, #10 and #11

You can get all my source code snippets and projects from my Dropbox folder.


I have also started another free blog of my personal Memoirs,  that nobody will be interested in, except maybe my son and my mum 🙂

“If one does not leave a record of ones doings, then what is the point of it all?”

Steve Shambles.

Advertisements


from Python Coder
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...