示例#1
0
    def test_calendar_get_set(self):
        widget = Calendar(self.window,
                          foreground="red",
                          year=2010,
                          month=1,
                          day=3)
        widget.pack()
        self.window.update()

        options = [
            'cursor', 'font', 'borderwidth', 'state', 'selectmode',
            'textvariable', 'locale', 'date_pattern', 'mindate', 'maxdate',
            'firstweekday', 'weekenddays', 'showweeknumbers',
            'showothermonthdays', 'selectbackground', 'selectforeground',
            'disabledselectbackground', 'disabledselectforeground',
            'normalbackground', 'normalforeground', 'background', 'foreground',
            'bordercolor', 'disabledbackground', 'disabledforeground',
            'othermonthforeground', 'othermonthbackground',
            'othermonthweforeground', 'othermonthwebackground',
            'weekendbackground', 'weekendforeground', 'headersbackground',
            'headersforeground', 'disableddaybackground',
            'disableddayforeground', 'tooltipbackground', 'tooltipforeground',
            'tooltipalpha', 'tooltipdelay'
        ]

        self.assertEqual(sorted(widget.keys()), sorted(options))

        with self.assertRaises(AttributeError):
            widget.cget("test")
        self.assertEqual(widget["foreground"], "red")
        widget["foreground"] = "blue"
        self.window.update()
        self.assertEqual(widget["foreground"], "blue")
        widget.configure({
            'foreground': 'cyan',
            'background': 'green'
        },
                         background="blue",
                         borderwidth=4)
        self.window.update()
        self.assertEqual(widget["foreground"], "cyan")
        self.assertEqual(widget["background"], "blue")
        self.assertEqual(widget["borderwidth"], 4)
        widget.config(locale='fr_FR')
        self.assertEqual(widget['locale'], 'fr_FR')
        self.assertEqual(widget._week_days[0].cget('text'), 'lun.')
        widget.config(locale='en_US')
        self.assertEqual(widget['locale'], 'en_US')
        self.assertEqual(widget._week_days[0].cget('text'), 'Mon')
        with self.assertRaises(UnknownLocaleError):
            widget.config(locale="jp")
        widget.config(cursor="watch")
        self.window.update()
        self.assertEqual(widget["cursor"], "watch")
        self.assertTrue(widget["showweeknumbers"])
        self.assertNotEqual(widget._week_nbs[0].grid_info(), {})
        widget.config(showweeknumbers=False)
        self.window.update()
        self.assertFalse(widget["showweeknumbers"])
        self.assertEqual(widget._week_nbs[0].grid_info(), {})
        self.assertFalse(widget._week_nbs[0].winfo_ismapped())
        self.assertNotEqual(widget._calendar[-1][-1].cget('text'), '')
        self.assertTrue(widget["showothermonthdays"])
        widget.config(showothermonthdays=False)
        self.window.update()
        self.assertFalse(widget["showothermonthdays"])
        self.assertEqual(widget._calendar[-1][-1].cget('text'), '')
        widget.config(font="Arial 20 bold")
        self.window.update()
        self.assertEqual(widget["font"], "Arial 20 bold")
        widget.config(borderwidth=5)
        self.window.update()
        self.assertEqual(widget["borderwidth"], 5)
        with self.assertRaises(ValueError):
            widget.config(borderwidth="a")
        widget.config(firstweekday='sunday')
        self.window.update()
        self.assertEqual(widget["firstweekday"], 'sunday')
        with self.assertRaises(ValueError):
            widget.config(firstweekday="a")

        widget.config(weekenddays=[5, 7])
        self.window.update()
        we_style = 'we.%s.TLabel' % widget._style_prefixe
        normal_style = 'normal.%s.TLabel' % widget._style_prefixe
        for i in range(7):
            self.assertEqual(widget._calendar[0][i].cget('style'), we_style if
                             (i + 1) in [5, 7] else normal_style)

        widget["mindate"] = datetime(2018, 9, 10)
        self.assertEqual(widget["mindate"], date(2018, 9, 10))
        widget.selection_set(date(2018, 9, 23))
        self.window.update()
        i, j = widget._get_day_coords(date(2018, 9, 2))
        self.assertIn('disabled', widget._calendar[i][j].state())
        i, j = widget._get_day_coords(date(2018, 9, 21))
        self.assertNotIn('disabled', widget._calendar[i][j].state())
        self.assertIn('disabled', widget._l_month.state())
        self.assertIn('disabled', widget._l_year.state())
        i, j = widget._get_day_coords(date(2018, 9, 10))
        self.assertNotIn('disabled', widget._calendar[i][j].state())
        i, j = widget._get_day_coords(date(2018, 9, 9))
        self.assertIn('disabled', widget._calendar[i][j].state())
        with self.assertRaises(TypeError):
            widget.config(mindate="a")
        self.assertEqual(widget["mindate"], date(2018, 9, 10))
        widget["mindate"] = None
        self.window.update()
        self.assertIsNone(widget["mindate"])
        i, j = widget._get_day_coords(date(2018, 9, 2))
        self.assertNotIn('disabled', widget._calendar[i][j].state())
        self.assertNotIn('disabled', widget._l_month.state())
        self.assertNotIn('disabled', widget._l_year.state())

        widget["maxdate"] = datetime(2018, 9, 10)
        self.assertEqual(widget["maxdate"], date(2018, 9, 10))
        widget.selection_set(date(2018, 9, 2))
        self.window.update()
        i, j = widget._get_day_coords(date(2018, 9, 22))
        self.assertIn('disabled', widget._calendar[i][j].state())
        i, j = widget._get_day_coords(date(2018, 9, 4))
        self.assertNotIn('disabled', widget._calendar[i][j].state())
        self.assertIn('disabled', widget._r_month.state())
        self.assertIn('disabled', widget._r_year.state())
        i, j = widget._get_day_coords(date(2018, 9, 10))
        self.assertNotIn('disabled', widget._calendar[i][j].state())
        i, j = widget._get_day_coords(date(2018, 9, 11))
        self.assertIn('disabled', widget._calendar[i][j].state())
        with self.assertRaises(TypeError):
            widget.config(maxdate="a")
        self.assertEqual(widget["maxdate"], date(2018, 9, 10))
        widget["maxdate"] = None
        self.window.update()
        self.assertIsNone(widget["maxdate"])
        i, j = widget._get_day_coords(date(2018, 9, 22))
        self.assertNotIn('disabled', widget._calendar[i][j].state())
        self.assertNotIn('disabled', widget._r_month.state())
        self.assertNotIn('disabled', widget._r_year.state())

        widget.config(selectmode="none")
        self.window.update()
        self.assertEqual(widget["selectmode"], "none")
        widget.config(selectmode="day")
        self.window.update()
        self.assertEqual(widget["selectmode"], "day")
        with self.assertRaises(ValueError):
            widget.config(selectmode="a")
        self.assertEqual(widget.cget('state'), tk.NORMAL)
        with self.assertRaises(ValueError):
            widget.config(state="test")
        widget['date_pattern'] = 'MM/dd/yyyy'
        self.window.update()
        self.assertEqual(widget["date_pattern"], 'MM/dd/yyyy')
        with self.assertRaises(ValueError):
            widget.config(date_pattern="mm-dd-cc")
        with self.assertRaises(AttributeError):
            widget.config(test="test")
        dic = {op: "yellow" for op in options[12:-4]}
        widget.configure(**dic)
        self.window.update()
        for op in options[12:-4]:
            self.assertEqual(widget.cget(op), "yellow")
        widget.config(tooltipalpha=0.5)
        self.assertEqual(widget["tooltipalpha"], 0.5)
        self.assertEqual(widget.tooltip_wrapper["alpha"], 0.5)
        widget.config(tooltipdelay=1000)
        self.assertEqual(widget["tooltipdelay"], 1000)
        self.assertEqual(widget.tooltip_wrapper["delay"], 1000)
        widget.config(tooltipforeground='black')
        self.assertEqual(widget["tooltipforeground"], 'black')
        widget.config(tooltipbackground='cyan')
        self.assertEqual(widget["tooltipbackground"], 'cyan')
示例#2
0
    def test_calendar_init(self):
        widget = Calendar(self.window)
        widget.pack()
        self.window.update()
        widget.destroy()
        widget = Calendar(self.window, showweeknumbers=False)
        self.window.update()
        widget.destroy()
        today = format_date(date.today(), 'short')
        var = tk.StringVar(self.window, today)
        widget = Calendar(self.window,
                          textvariable=var,
                          month=3,
                          year=2011,
                          day=10)
        self.window.update()
        self.assertEqual(var.get(), today)
        self.assertEqual(widget.selection_get(), date.today())
        widget.destroy()
        widget = Calendar(self.window,
                          font="Arial 14",
                          selectmode='day',
                          cursor="hand1",
                          year=2018,
                          month=2,
                          day=5)
        widget.pack()
        self.window.update()
        self.assertEqual(widget.selection_get(), date(2018, 2, 5))
        self.assertEqual(widget.get_displayed_month(), (2, 2018))
        w, d = widget._get_day_coords(date(2018, 2, 5))
        self.assertEqual(widget._calendar[w][d].cget('text'), '5')
        widget.destroy()

        widget = Calendar(self.window, year=2011, month=2, day=35)
        widget.pack()
        self.window.update()
        self.assertIsNone(widget.selection_get())
        self.assertEqual(widget._date, date(2011, 2, 1))
        widget.destroy()

        with self.assertRaises(ValueError):
            widget = Calendar(self.window, month=23)
            widget.pack()
            self.window.update()
            widget.destroy()

        with self.assertRaises(ValueError):
            widget = Calendar(self.window, borderwidth="e")
            widget.pack()
            self.window.update()
            widget.destroy()

        with self.assertRaises(TypeError):
            widget = Calendar(self.window, weekenddays=7)
            widget.pack()
            self.window.update()
            widget.destroy()

        with self.assertRaises(ValueError):
            widget = Calendar(self.window, weekenddays="e")
            widget.pack()
            self.window.update()
            widget.destroy()

        with self.assertRaises(ValueError):
            widget = Calendar(self.window, weekenddays=[1])
            widget.pack()
            self.window.update()
            widget.destroy()

        with self.assertRaises(ValueError):
            widget = Calendar(self.window, weekenddays=['a', 'b'])
            widget.pack()
            self.window.update()
            widget.destroy()

        with self.assertRaises(ValueError):
            widget = Calendar(self.window, weekenddays=[12, 3])
            widget.pack()
            self.window.update()
            widget.destroy()

        with self.assertRaises(TypeError):
            widget = Calendar(self.window, maxdate="e")
            widget.pack()
            self.window.update()
            widget.destroy()

        widget = Calendar(self.window,
                          mindate=datetime(2013, 5, 22, 10, 5),
                          maxdate=datetime.today())
        widget.pack()
        self.window.update()
        widget.destroy()

        with self.assertRaises(TypeError):
            widget = Calendar(self.window, mindate="e")
            widget.pack()
            self.window.update()
            widget.destroy()

        with self.assertRaises(ValueError):
            widget = Calendar(self.window,
                              mindate=date(2018, 4, 5),
                              maxdate=date(2018, 4, 4))
            widget.pack()
            self.window.update()
            widget.destroy()

        with self.assertRaises(ValueError):
            widget = Calendar(self.window, firstweekday="e")
            widget.pack()
            self.window.update()
            widget.destroy()

        widget = Calendar(self.window,
                          font="Arial 14",
                          selectmode='day',
                          cursor="hand1",
                          year=2018,
                          month=2,
                          day=5)
        widget.pack()
        self.window.update()
        widget.destroy()

        widget = Calendar(self.window,
                          selectmode='none',
                          year=2015,
                          month=1,
                          background="black",
                          foreground="white",
                          key="a")
        widget.pack()
        self.window.update()
        self.assertIsNone(widget.selection_get())
        self.assertEqual(widget._date, date(2015, 1, 1))
        widget.destroy()

        with self.assertRaises(ValueError):
            widget = Calendar(self.window, selectmode='wrong')
            widget.pack()
            self.window.update()
            widget.destroy()