Python日历对齐
当我运行代码时,日历日数字与它们所在的星期不一致。我试过左、右、中。这些都不能解决我的问题,即使没有justify语句,它也不起作用Python日历对齐,python,tkinter,calendar,Python,Tkinter,Calendar,当我运行代码时,日历日数字与它们所在的星期不一致。我试过左、右、中。这些都不能解决我的问题,即使没有justify语句,它也不起作用 from tkinter import * import calendar as cl import datetime as datet year = int(datet.date.today().strftime("%Y")) #year month = int(datet.date.today().strftime("%m")) #month number
from tkinter import *
import calendar as cl
import datetime as datet
year = int(datet.date.today().strftime("%Y")) #year
month = int(datet.date.today().strftime("%m")) #month number
root = Tk()
root.geometry("300x300")
app = Frame(root)
app.grid()
cal = Label(app, text = cl.month(year, month), justify = RIGHT)
cal.grid(row = 0, column = 0)
root.mainloop()
Tkinter默认使用非单空格字体作为标签<代码>日历。月(年,月)是一个长字符串,如果您在控制台中打印,它将按您的意愿显示出来。将代码更改为这样:
import calendar
import datetime
from tkinter import *
year = int(datetime.date.today().strftime("%Y")) # year
month = int(datetime.date.today().strftime("%m")) # month number
root = Tk()
root.geometry("300x300")
app = Frame(root)
app.grid()
cal = Label(app, text=calendar.month(year, month), font=('Consolas', 12))
cal.grid(row=0, column=0)
root.mainloop()
ConsoleAS是一种单空间字体,还有其他选择
请注意,如果您将日历的每个部分拆分为自己的小部件,则无需更改字体以使它们按照您的意愿对齐