簡體   English   中英

如何通過tkinter發送消息?

[英]How to send message via tkinter?

單擊按鈕10次后,tkinter是否可以發送電子郵件? 當按鈕被按下10次時,我要在第10次發送消息““水低”。 水結束了,我需要去充電。 這是飲水機的按鈕。 該按鈕與繼電器相連。

是的,這是可能的,而且非常簡單。 您可以添加一個功能,在按下10個按鈕后顯示一條消息。 如果要彈出消息,可以使用tkinter.messagebox模塊顯示彈出消息。

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()

要發送電子郵件,您必須使用其他庫。 我正在使用smtplib

  • 安裝smtplib

    pip install smtplib

    雖然我安裝了python

我在這個圖書館沒有太多經驗。 我建議您參閱smtplib 文檔 但是我做了一個從Gmail帳戶發送電子郵件的功能。 另外,如果您使用我的功能,我建議您創建一個新帳戶以發送電子郵件。

這是完整的代碼:

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()

電子郵件發送GUI時的另一件事可能會凍結,直到發送電子郵件為止。 要解決此問題,您需要使用threading模塊和Thread Send_Email(...)函數。

要使用線程,您需要from threading import Thread中將其from threading import Thread並且必須像這樣在單獨的線程中運行Send_Email(...)函數

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)

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM