尝试编写一个显示在我的桌面上的小日历程序。一切正常,除了输出中的行居中对齐而不是漂亮的日历网格布局。我敢肯定,这与label.config函数有关。我被困住了。 Windows 10上的Python 3.7。
import calendar as cd
import tkinter as tk
from datetime import date
from datetime import datetime
from tkinter import *
root = Tk()
root.tk_setPalette('#000000')
label = tk.Label(text="", fg="Light Blue")
label.place(x=1,y=1)
now = date.today()
year = now.year
month = now.month
c = cd.TextCalendar(cd.SUNDAY)
data = c.formatmonth(year,month)
label.config(text=data)
root.lift()
root.overrideredirect(1)
root.geometry("+530+1")
root.geometry("130x130")
root.mainloop()
答案 0 :(得分:1)
您需要使用等宽字体,并将justify
添加到标签中。
label = tk.Label(text="", fg="Light Blue",font="Courier",justify="left")
答案 1 :(得分:0)
终于找到了办法。
import calendar as cd
import tkinter as tk
from tkinter import ttk
from datetime import date
from datetime import datetime
from tkinter import *
now = date.today()
year = now.year
month = now.month
c = cd.TextCalendar(cd.SUNDAY)
root = Tk()
data = Text(root, fg="Light Blue", font=("Consolas", 10), borderwidth=0)
data.pack()
x = c.formatmonth(year,month)
data.insert(END, x)
root.tk_setPalette('#000000')
label = tk.Label(text="")
label.place(x=1,y=1)
root.lift()
root.overrideredirect(1)
root.geometry("+500+8")
root.geometry("150x125")
root.mainloop()