将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()
答案 0 :(得分:1)
您只需要对Label
中的文字进行左对齐:
cal_out = Label(
gui,
bg='lightblue',
font=('courier', 12, 'bold'),
justify=LEFT, # like this
text=cal_x,
)
本月,这给了我:
tkinter
将CENTER
- 默认为对齐,这使得它在这种情况下看起来很奇怪; calendar
只返回文字,因此您无法将其归咎于该模块!