0

Is it possible tkinter to send email after click 10 times on button? When button is push 10 times, I want at 10th press to send mesage "" water is low "". Water is over and I need to go to charge water. This is a button for water dispenser. The button is swich with relay.

Alexandre Tranchant
  • 4,426
  • 4
  • 43
  • 70
chano
  • 17
  • 5

1 Answers1

2

Yes, it is possible and very simple as well. You can add a function which shows a message after 10 button presses. If you want a pop-up message you can use tkinter.messagebox module to show pop-up messages.

from tkinter import *
from tkinter.messagebox import *

root = Tk()

show_press = Label(root, text='You pressed the Button 0 times')
show_press.pack()

count = 0
def times_pressed():
    global count
    count+=1
    if count >= 10:
        # Code to be executed when "water is low"
        showwarning("Water is Low", 'Water is over and it needs to be changed.') 
    show_press['text'] = 'You pressed the Button {} times'.format(count)

button = Button(root, text='Button', command = times_pressed)
button.pack()

root.mainloop()

To send an email you have to use other libraries. I'm using smtplib.

  • To install smtplib

    pip install smtplib

    Though I had it with the python I installed

I don't have much experience in this library. I would recommend you see smtplib documentation. But I made a function which sends email from an gmail account. Also if you use my function I would suggest you to create a new account just to send email.

Here is the complete code:

import smtplib
from tkinter import *

def Send_Email(email, password, to, subject, message):
    """Sends email to the respected receiver."""
    try: 
        # Only for gmail account.
        with smtplib.SMTP('smtp.gmail.com:587') as server: 
            server.ehlo()  # local host
            server.starttls()  # Puts the connection to the SMTP server.
            # login to the account of the sender
            server.login(email, password)  
            # Format the subject and message together 
            message = 'Subject: {}\n\n{}'.format(subject, message)   
            # Sends the email from the logined email to the receiver's email
            server.sendmail(email, to, message)
            print('Email sent')
    except Exception as e:
        print("Email failed:",e)

count = 0
def times_pressed():
    global count
    count+=1
    if count >= 10:
        # Code to be executed when "water is low"
        message = "Water is over and it needs to be changed."

        Send_Email(
            email='tmpaccount@gmail.com',   # Gmail account 
            password='test@123',            # Its password
            to='Your email address',        # Receiver's email
            subject='Water is Low',         # Subject of mail
            message=message )               # The message you want

    show_press['text'] = 'You pressed the Button {} times'.format(count)


root = Tk()

show_press = Label(root, text='You pressed the Button 0 times')
show_press.pack()
button = Button(root, text='Button', command = times_pressed)
button.pack()

root.mainloop()

One more thing when the email is sending the GUI might freeze till the email is sent. To fix this issue you need to use threading module and Thread the Send_Email(...) function.

To use threading you need to import it from threading import Thread and have to run Send_Email(...) function in a separate thread like so

from threading import Thread

...

def times_pressed():
    global count
    count+=1
    if count >= 10:
        # Code to be executed when "water is low"
        message = "Water is over and it needs to be changed."

        thread = Thread( target =  Send_Email, kwargs = {
                        'email' : 'tmpaccount@gmail.com',   # Gmail new account 
                        'password' : 'test@123',            # Its password
                        'to' = 'Your email address',        # Receiver's email
                        'subject' : 'Water is Low',         # Subject of mail
                        'message' : message }               # The message you want
                       )
        thread.start()

    show_press['text'] = 'You pressed the Button {} times'.format(count)

...
Saad
  • 3,340
  • 2
  • 10
  • 32
  • 1
    If you need to send out notifications (email / pushover / pushbullet) please also check my python cli tool here: https://github.com/ltpitt/python-simple-notifications – Pitto May 15 '19 at 10:43
  • You are a gold men,is it possible to send this message to Gmail with out popup on desktop.Thank you very much – chano May 15 '19 at 14:27
  • @chano: Yes it is possible see the first comment and this [post](https://stackoverflow.com/a/39615138/10364425). And if you don’t want the pop-up then remove `showwarning(...)`and import `tkinter.messagebox`. – Saad May 15 '19 at 14:41
  • ehhh,,i am a beginner and i can't know it,you are sure big prgram men. – chano May 15 '19 at 14:57
  • @chano: I added more useful information to the answer – Saad May 15 '19 at 16:32
  • cekovy@gmail.com,this is the addres,but i can't fill this code,it tell me:errno-2 name or service not know"".can you help me to fill this kode.Thank you – chano May 16 '19 at 10:57
  • You need to fill in your Email and password and the Email of the receiver from the line 37 in the code. Please read and see the code carefully and try to understand. – Saad May 16 '19 at 11:01
  • oleleeeee bois.. this work,you are gold boys,thank you very very much. – chano May 16 '19 at 12:27
  • @chano: You're welcome. Consider accepting and upvoting the answer if it was helpful. – Saad May 16 '19 at 12:49
  • All work very good but how i can send mesage to multiple acount? is it possible? – chano Jun 05 '19 at 09:28
  • @chano: You can use `for` loop on a `list` of all the receiver's email addresses to send multiple emails. I guess there is a better way out there to send email to multiple recipients. Here is a post on [how to send emails to multiple recipients using python smtplib?](https://stackoverflow.com/questions/8856117/how-to-send-email-to-multiple-recipients-using-python-smtplib) – Saad Jun 06 '19 at 05:35