python - 如何从月 Gtk.Calendar 创建周日历并在 python 中显示每天的注释

标签 python linux gtk

我用 python 创建了一个带有月和周 View 的日历应用程序。 在月 View 中,我可以每天写笔记,将它们存储在字典中,并将字典保存到磁盘中,以便我可以随时阅读。

现在我正在使用周 View 。我成功地显示了与我从月 View 中选择的日期相关的月份中的星期(周数和天数,周一到周日)。在代码中,我使用 Gtk.Label 表示周数,并将一周中的天数附加在 Gtk.Grid 中。在每一天下面,我都使用 for in 循环附加一个 Gtk.TextView,创建一个 Gtk.TextView 列表,其中显示月 View 中每天的笔记。现在,我每次选择一周中的一天时,都只能显示一个笔记。如果我在不同的日子有多个笔记,则无法显示所有笔记。这是我的问题。 下面是更新周日历的代码片段(def update_week_cal)。如何修改循环部分中的代码,以便我可以显示从月 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

考虑到我在月 View 和周 View 中使用了相同的 Gtk.TextBufferself.text 是月 View 中的注释。

提前致谢。

最佳答案

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

            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)

问题的答案是以下代码:

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])

关于python - 如何从月 Gtk.Calendar 创建周日历并在 python 中显示每天的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58250628/

相关文章:

python - Unittest Django : Mock external API, 什么是正确的方法?

python - Python bottle 中的自调用

linux - 在docker中模拟VPN连接

linux - 从 Bash 中的变量获取特定值

python - pyGTK/GTK filechooser - 是否可以在文件或文件夹写入光盘之前验证用户输入?

Python 和 Gtk - 使用的是哪个 GTK 版本?

python - 在seaborn中绘制两列dataFrame

python - 如何在字符串变量中存储单个\?

linux - Raspberry Pi 终端环境中的 Unicode 和 256 色

python - 如何避免Python中Gtk.Dialog关闭?