jim leahy
jim leahy

Reputation: 35

inserting python calendar into tkinter label

I've a problem with the code that I have written. When I insert the variable calendar2019 into the tkinter label LabelCalen it does not show up in the window root4, and the window root4 is not created at all.

import calendar
import tkinter as tk

def CalScr():
    calendar2019 = calendar.calendar(2019)#creating calender variable
    root4 = tk.Tk()
    labelCalen= tk.Label(root4, text = calendar2019, )
    root4.mainloop
CalScr()

the calender should be printed out in the label LabelCalen

Upvotes: 2

Views: 382

Answers (1)

Diego Torres Milano
Diego Torres Milano

Reputation: 69426

One of the problems is that you are not invoking mainloop as you omitted the (). The other is you are not giving the label a position.

#! /usr/bin/env python
import calendar
import tkinter as tk
def CalScr():
    calendar2019 = calendar.calendar(2019) #creating calender variable
    root4 = tk.Tk()
    labelCalen= tk.Label(root4, text = calendar2019, font=("Courier New", 14))
    labelCalen.grid(column=0, row=0)
    root4.mainloop()
CalScr()

This is also setting the font to a fixed space one otherwise the calendar won't be aligned properly.

Upvotes: 2

Related Questions