Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用Python开发日历GUI?_Python_Python 3.x_User Interface_Tkinter_Calendar - Fatal编程技术网

如何用Python开发日历GUI?

如何用Python开发日历GUI?,python,python-3.x,user-interface,tkinter,calendar,Python,Python 3.x,User Interface,Tkinter,Calendar,这是我目前拥有的代码。。。我的课几乎没有介绍Tkinter,我们正在使用的这本书对理解如何使用日历库没有帮助。标签Mon/Tu/Wed/Etc起作用,但日历不显示 图像: 首先,你应该考虑在最后创建主循环: root = Tkinter.Tk() app = Application(root) root.mainloop() 现在应该可以用了。试试这个: from tkinter import * from calendar import * class Application(Fram

这是我目前拥有的代码。。。我的课几乎没有介绍Tkinter,我们正在使用的这本书对理解如何使用日历库没有帮助。标签Mon/Tu/Wed/Etc起作用,但日历不显示

图像:


首先,你应该考虑在最后创建主循环:

root = Tkinter.Tk()
app = Application(root)
root.mainloop()
现在应该可以用了。

试试这个:

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
变量。所以我所做的是从
\uuu init\uuu()
本身获取这些变量的输入,所以在程序开始时,它会要求一个月和一年。完成后,我将其作为参数传递给
create\u widget()
,然后在
monthrange()中使用。还添加了Python程序来打印给定年份的日历

导入日历库

import calendar 

def printcalendar(year): 

# printing calendar 
print(calendar.calendar(2020)) 


year = 2020
printcalendar(year) 

这是如何回答这个问题的?问题是如何使用tkinter编写的GUI创建日历。回答前请仔细阅读问题并仔细阅读,否则您的答案将毫无用处,您将获得否决票。此示例给出:NameError:name'year'和TypeError:'
import calendar 

def printcalendar(year): 

# printing calendar 
print(calendar.calendar(2020)) 


year = 2020
printcalendar(year)