如何通过tkinter发送消息?

时间:2019-05-15 09:08:50

标签: python tkinter

单击按钮10次后,tkinter是否可以发送电子邮件?当按钮被按下10次时,我要在第10次发送消息““水低”。水结束了,我需要去充电。这是饮水机的按钮。该按钮与继电器相连。

1 个答案:

答案 0 :(得分:2)

是的,这是可能的,而且非常简单。您可以添加一个功能,在按下10个按钮后显示一条消息。如果您想要弹出消息,则可以使用DECLARE @SQL VARCHAR(MAX)= 'SELECT '+ @sqlstring + ' FROM MyTable ' EXEC (@SQL) 模块显示弹出消息。

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

    尽管我安装了python,但是

我在这个图书馆没有太多经验。我建议您看到pip install smtplib documentation。但是我做了一个从Gmail帐户发送电子邮件的功能。另外,如果您使用我的功能,建议您创建一个新帐户以发送电子邮件。

这是完整的代码:

smtplib

电子邮件发送时,GUI的另一件事可能会冻结,直到发送电子邮件为止。要解决此问题,您需要使用import smtplib from tkinter import * def Send_Email(email, password, to, subject, message): 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) 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" message = "Water is over and it needs to be changed." Send_Email( 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 show_press['text'] = 'You pressed the Button {} times'.format(count) button = Button(root, text='Button', command = times_pressed) button.pack() root.mainloop() 模块和threading Thread函数。

要使用线程,您需要将其导入Send_Email(...)并必须在这样的线程中运行from threading import Thread函数

Send_Email(...)
相关问题