Renzky
Renzky

Reputation: 3

How to use the calendar module with tkinter?

Using the calendar module with tkinter there is always a displacement in the last row, if the week doesn't start on a Monday. Does anyone know why this happens?

import calendar
from tkinter import *

gui = Tk()
gui.title("Calendar")

def cal():
    y = e1.get()
    m = e2.get()
    cal_x = calendar.month(int(y),int(m),w = 2, l = 1)
    print (cal_x)
    cal_out = Label(gui, text=cal_x, font=('courier', 12, 'bold'), bg='lightblue')
    cal_out.pack(padx=3, pady=10)

label1 = Label(gui, text="Year:")
label1.pack()

e1 = Entry(gui)
e1.pack()

label2 = Label(gui, text="Month:")
label2.pack()

e2 = Entry(gui)
e2.pack()

button = Button(gui, text="Show",command=cal)
button.pack()

gui.mainloop()

Upvotes: 0

Views: 5272

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122032

You simply need to left-justify the text in the Label:

cal_out = Label(
    gui,
    bg='lightblue',
    font=('courier', 12, 'bold'), 
    justify=LEFT,  # like this
    text=cal_x, 
)

For this month, that gives me:

Calendar with correct day alignment

tkinter will CENTER-justify by default, which makes it look odd in this case; calendar just returns text, so you can't really blame it on that module!

Upvotes: 2

Related Questions