如何从Gtk.Calendar月创建一周日历并在python中每天显示注释 [英] how to create from month Gtk.Calendar a week calendar and display the notes in each day in python

查看:125
本文介绍了如何从Gtk.Calendar月创建一周日历并在python中每天显示注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个日历应用程序,该应用程序具有python的月和周视图. 在月视图中,我每天都可以写笔记,将它们存储在字典中,然后将字典保存到磁盘中,以便随时可以阅读.

I have created a calendar app with month and week view in python. In month view, I can write notes in each day, store them in a dictionary and save the dictionary in to disk so I can read it any time.

现在我正在使用周视图.我设法显示了从月视图中选择的日期(星期几和周一至周日的天数).在代码中,我使用了Gtk.Label表示星期几和星期几,将它们附加在Gtk.Grid中.在每天的下面,我使用for in循环附加了Gtk.TextView,从而创建了Gtk.TextView列表,并在该视图中从月视图显示每天的笔记.现在,每次选择时,我只能显示一个笔记一周的一天.如果我在不同的日期有多个笔记,则无法显示所有笔记.这是我的问题. 下面是工作日日历正在更新的代码片段(def update_week_cal).如何在循环部分中修改代码,以便我可以显示从月视图中选取的每周的所有笔记?

Now I am working on week view.I managed to display the week of the month regard the day I have picked from month view (the number of the week and the days, Monday to Sunday).In code, I used Gtk.Label for the number of the week and the days of the week attached them in a Gtk.Grid. Beneath each day I have attached a Gtk.TextView using a for in loop creating a list of Gtk.TextView where is being displayed the notes of each day from month view.Right now I have managed to display only one note each time I pick a day of a week.If I have multiple notes in different days can not display all of them.And here is my question. Below is a snippet of code where the week calendar is updating(def update_week_cal).How to modify the code in loop part, so I can display all the notes in each week I have picked from month view?

    def update_week_cal(self, *args):
        year, month, day = self.calendar.get_date()
        month = month + 1
        today = datetime.date(year, month, day)
        self.page2.remove(self.label)
        self.label = Gtk.Label(str(today.isocalendar()[1]) + "η Εβδομάδα")
        self.page2.attach(self.label, 3, 1, 1, 1)
        dates = list([today + datetime.timedelta(days=i) for i in range(0 - today.weekday(), 7 - today.weekday())])
        j = 0
        for days in dates:
            self.labels[j].destroy()
            if days == today:
                self.labels[j] = Gtk.Label()
                self.labels[j].set_markup('<span font = "12" weight = "bold">{0}</span>'.format(days.strftime("%a ""%d/""%m")))
            else:
                self.labels[j] = Gtk.Label(days.strftime("%a ""%d/""%m"))
            self.page2.attach(self.labels[j], j, 2, 1, 1)
            self.page2.remove(self.textviews[j])
            self.textviews[j] = Gtk.TextView()
            self.textviews[j].set_editable(False)
            self.textviews[j].set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
            self.page2.attach(self.textviews[j], j, 3, 1, 1)
            for i in range(7):
                try:
                    if self.text is not None and self.d[(year, month, day)]:
                        x = int(today.strftime("%w"))-1
                        self.textviews[x].get_buffer().set_text(self.text)
                except KeyError:
                    x = int(today.strftime("%w")) - 1
                    self.textviews[x].get_buffer().set_text(self.text)
                    print(i, j, x)

                except TypeError:
                    x = int(today.strftime("%w")) - 1
                    self.textviews[x].get_buffer().set_text(self.text)

            self.show_all()
            j = j + 1

考虑到我在月和周视图中使用了相同的Gtk.TextBuffer. self.text是月份视图中的笔记.

Take into account that I have used the same Gtk.TextBuffer for month and week view. self.text are the notes from month view.

谢谢.

推荐答案

好吧,我设法根据日期显示了注释,从而改变了思考问题的方式并从头开始.在末尾添加了新代码.旧的,减去这个:

Well I managed to display the notes according to date, changing the way of thinking the problem and starting from beginning.The new code is been added at the end of the old one, subtracting this:

            for i in range(7):
                try:
                    if self.text is not None and self.d[(year, month, day)]:
                        x = int(today.strftime("%w"))-1
                        self.textviews[x].get_buffer().set_text(self.text)
                except KeyError:
                    x = int(today.strftime("%w")) - 1
                    self.textviews[x].get_buffer().set_text(self.text)
                    print(i, j, x)

                except TypeError:
                    x = int(today.strftime("%w")) - 1
                    self.textviews[x].get_buffer().set_text(self.text)

问题的答案是下面的代码:

The answer to the problem is the below code:

list_of_dates = list(self.d.keys())
list_of_notes = list(self.d.values())
dictionary = dict(zip(list_of_dates, list_of_notes))
for key in dictionary.items():
    date = datetime.date(year=key[0][0], month=key[0][1]+1, day=key[0][2])
    week_of_day = str(date.isocalendar()[1])            
    day_of_week = int(date.strftime("%w"))-1          
    if week == week_of_day:
        self.textviews[day_of_week].get_buffer().set_text(key[1])

这篇关于如何从Gtk.Calendar月创建一周日历并在python中每天显示注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆