gpt4 book ai didi

Python tkinter 无法在 crontab 上工作

转载 作者:太空狗 更新时间:2023-10-29 12:42:34 25 4
gpt4 key购买 nike

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
from Tkinter import *
import tkMessageBox
import smtplib
import sys
from email.mime.multipart
import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header

def Call():
global root
sender = 'Nome 1 <email1@exemplo.com>'
receivers = ['Nome 2 <email2@exemplo.com>',
'Nome 3 <email3@exemplo.com>',
'Nome 4 <email4@exemplo.com>']

msg = MIMEMultipart('alternative')
msg['Subject'] = Header("Envio de relatório diário", 'utf-8')
msg['From'] = sender
msg['To'] = ", ".join(receivers)
text = "A actividade de hoje foi:\n\n\n\n" + tb.get("1.0",'end-1c')

msg.attach(MIMEText(text.encode('utf-8'), 'plain', 'utf-8'))

try:
smtpObj = smtplib.SMTP('smtp.office365.com',587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.ehlo()
smtpObj.login('emailAqui','passwordAqui')
smtpObj.sendmail(sender, receivers, msg.as_string())
smtpObj.quit()
tkMessageBox.showinfo("Sucesso", "A sua mensagem foi enviada com sucesso.")
root.quit()
except SMTPException:
tkMessageBox.showinfo("Erro", "Ocorreu um erro a enviar a mensagem")

os.environ["DISPLAY"] = ":0.0"
root = Tk()
root.wm_title("Report sender")
root.geometry('600x675')
tb = Text(root, height = 40)
tb.pack(side=TOP,pady=10)
button = Button(root, text = 'Send report!', command = Call)
button.pack(side=TOP,pady=10)

root.mainloop()
root.destroy()

我有一个包含上面代码的 python 脚本。如果我使用绝对路径调用它,它会按预期工作:

/usr/local/bin/reportSender.py

但是当我将它放在 crontab 中时,它不会出现任何窗口,就像我使用它的绝对路径执行它时一样。

出于测试目的,我的 crontab 如下所示:

30 10 * * * /usr/local/bin/reportSender.py

最佳答案

提供 python完整路径,或者只是 python,如果它已经包含在用户 path 中,即:

30 10 * * * /full/path/to/python /usr/local/bin/reportSender.py

或者

30 10 * * * python /usr/local/bin/reportSender.py

关于Python tkinter 无法在 crontab 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37047241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com