如何在tkinter中使用日历模块?

时间:2015-04-21 21:04:57

标签: python python-3.x tkinter calendar

calendar模块与tkinter一起使用,如果一周不在星期一开始,则最后一行总是有位移。有谁知道为什么会这样?

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()

1 个答案:

答案 0 :(得分:1)

您只需要对Label中的文字进行左对齐:

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

本月,这给了我:

Calendar with correct day alignment

tkinterCENTER - 默认为对齐,这使得它在这种情况下看起来很奇怪; calendar只返回文字,因此您无法将其归咎于该模块!