这是我目前的代码......我的课程几乎没有介绍Tkinter,我们使用的书对理解如何使用Calendar库没有帮助。标签Mon / Tu / Wed / Etc工作,但日历未显示。
图片: problem problem cont
from tkinter import *
from time import *
from calendar import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
#create labels
for i in range(7):
label = Label(self, text=days[i])
label.grid(row = 0, column = i)
weekday, numDays = monthrange(year, month)
week = 1
for i in range(1, numDays + 1):
button = Button(self, text = str(i))
button.grid(row = week, column = weekday)
weekday+=1
if weekday > 6:
week +=1
weekday = 0
答案 0 :(得分:0)
首先,您应该考虑最后创建主循环:
root = tk()
app = Application(root)
root.mainloop()
它现在应该可以正常工作。
答案 1 :(得分:0)
试试这个:
from tkinter import *
from calendar import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
year = int(raw_input('\nEnter Year eg. 2017\n'))
month = int(raw_input('\nEnter month number.\n'))
self.create_widgets(year, month)
def create_widgets(self, year, month):
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
#create labels
for i in range(7):
label = Label(self, text=days[i])
label.grid(row = 0, column = i)
weekday, numDays = monthrange(year, month)
week = 1
for i in range(1, numDays + 1):
button = Button(self, text = str(i))
button.grid(row = week, column = weekday)
weekday += 1
if weekday > 6:
week += 1
weekday = 0
mainWindow = tk()
obj = Application(root)
mainWindow.mainloop()
在计算monthrange()
时,year
和month
变量未定义。所以我所做的是从__init__()
本身获取这些变量的输入,因此在程序开始时它会询问一个月和一年。完成后,我只是将其作为参数传递给create_widget()
,然后在monthrange()
中使用。还添加了mainloop
答案 2 :(得分:-2)
Python程序可以打印给定年份的日历
导入日历库
import calendar
def printcalendar(year):
# printing calendar
print(calendar.calendar(2020))
year = 2020
printcalendar(year)