This small program is neat:

import calendar

calendar.prmonth(2006, 3)

"""
     March 2006
Mo Tu We Th Fr Sa Su
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
"""

Is there way to trap output in a string so it can be used in GUI like Tkinter?

I am scratching my head on this. Would be nice to have for Tkinter! I know wxPython has wx.calendar.CalendarCtrl(), which is quite elaborate and allows to select any month of any year at runtime.

Actually a1eio's question about the use of os.popen() gave me this idea:

# pipe calendar.prmonth(2005, 6) to a string
# save the short code within the triple quotes as cal_mar2006.py:
"""
import calendar
calendar.prmonth(2006, 3)
"""

import os

p = os.popen("python D:/Python24/Atest/cal_mar2006.py")
str1 = p.read()
print str1

I am sure there must be a better way.

There is.

import calendar
s = calendar.month(2006, 3)
print s

- gives the same effect. If I am understanding this correctly, the prmonth() method does not return a value - it only prints. If you are dead-set on using prmonth() to get at this data, I think you will need to do what vegaseat suggests: catch the output using a file, or something similar.

Comments
Great and simple answer!

I think that's exactly what our friend Bumsfeld wanted. A short

help('calendar')

would have helped. Thanks G-Do, I learned a new thing!

Hi!

I once wrote a calendar widget for Tkinter. It's not great, but ... well, I was young then :) You can find it here.

Regards, mawe

Hi!

I once wrote a calendar widget for Tkinter. It's not great, but ... well, I was young then :) You can find it here.

Regards, mawe

Hallo mawe, works just fine!

(Bumsfeld must be on spring break, or part of the student protest!)

This question has already been answered. Start a new discussion instead.