获得程序运行的时间(Python)

时间:2013-04-17 22:09:19

标签: python string time python-imaging-library

我正在编写一个Python脚本,我想让它说上次更新:然后它更新的时间但我不确定如何

这里是示例代码

from PIL import ImageDraw
import ctypes, time
import urllib
import manipulate
import datetime



font_size = 50
font_color = "red"

font = ImageFont.load_default() 
img = Image.open('bg.bmp')
draw = ImageDraw.Draw(img)

time = datetime.datetime.now()


draw.text((650, 450),'                      CURRENT GRADES' )
draw.text((650, 500), 'Period 1: Geography -----------------------------')
draw.text((650, 550), 'Period 2: Francais-------------------------------')
draw.text((650, 600), 'Period 3: Science--------------------------------')
draw.text((650, 650), 'Period 4: P.E------------------------------------')
draw.text((650, 700), 'Period 5: Algebra 9------------------------------')
draw.text((650, 750), 'Period 6: LA-------------------------------------')
draw.text((650, 800), 'Last Updated:'(time))
draw = ImageDraw.Draw(img)
draw = ImageDraw.Draw(img)
img.save('mod_bg.bmp')







pathToBmp = "C:\Python27\mod_bg.bmp"
SPI_SETDESKWALLPAPER = 20  
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, pathToBmp ,
                                       0)

以及最后更新的地方我需要时间

当我尝试它时,我收到此错误

Traceback (most recent call last):
File "C:\Python27\Project.py", line 28, in <module>
draw.text((650, 800), 'Last Updated:'(time))
TypeError: 'str' object is not callable

如果有人可以帮忙请做! 谢谢!

2 个答案:

答案 0 :(得分:6)

使用datetime

import datetime
now = datetime.datetime.now()
draw.text((650, 800), 'Last Updated: {0}'.format(now))

答案 1 :(得分:0)

问题在于'Last Updated:'(time)部分。您将括号放在字符串后面,从而将其视为一个函数。你应该'Last Updated: %s' % time