簡體   English   中英

Python Tkinter在crontab上不起作用

[英]Python tkinter not working on crontab

#!/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

暫無
暫無

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

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