将python日历插入到tkinter实验室

2024-06-01 21:44:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我写的代码有问题。当我将变量calendar2019插入tkinter标签LabelCalen时,它不会显示在窗口root4中,并且根本不会创建窗口root4。你知道吗

import calendar
import tkinter as tk

def CalScr():
    calendar2019 = calendar.calendar(2019)#creating calender variable
    root4 = tk.Tk()
    labelCalen= tk.Label(root4, text = calendar2019, )
    root4.mainloop
CalScr()

日历应该在标签LabelCalen上打印出来


Tags: 代码importcreatingtkinterdefas标签variable
1条回答
网友
1楼 · 发布于 2024-06-01 21:44:40

问题之一是您没有调用mainloop,因为您省略了()。另一个是你没有给标签一个位置。你知道吗

#! /usr/bin/env python
import calendar
import tkinter as tk
def CalScr():
    calendar2019 = calendar.calendar(2019) #creating calender variable
    root4 = tk.Tk()
    labelCalen= tk.Label(root4, text = calendar2019, font=("Courier New", 14))
    labelCalen.grid(column=0, row=0)
    root4.mainloop()
CalScr()

这也是将字体设置为固定间距,否则日历将无法正确对齐。你知道吗

相关问题 更多 >