Ejemplo n.º 1
0
    def test_widget_destroy(self):
        # automatically created variable
        x = ttk.LabeledScale()
        var = x._variable._name
        x.destroy()
        self.failUnlessRaises(Tkinter.TclError, x.tk.globalgetvar, var)

        # manually created variable
        myvar = Tkinter.DoubleVar()
        name = myvar._name
        x = ttk.LabeledScale(variable=myvar)
        x.destroy()
        self.failUnlessEqual(x.tk.globalgetvar(name), myvar.get())
        del myvar
        self.failUnlessRaises(Tkinter.TclError, x.tk.globalgetvar, name)

        # checking that the tracing callback is properly removed
        myvar = Tkinter.IntVar()
        # LabeledScale will start tracing myvar
        x = ttk.LabeledScale(variable=myvar)
        x.destroy()
        # Unless the tracing callback was removed, creating a new
        # LabeledScale with the same var will cause an error now. This
        # happens because the variable will be set to (possibly) a new
        # value which causes the tracing callback to be called and then
        # it tries calling instance attributes not yet defined.
        ttk.LabeledScale(variable=myvar)
        if hasattr(sys, 'last_type'):
            self.failIf(sys.last_type == Tkinter.TclError)
    def test_initialization(self):
        # master passing
        master = tkinter.Frame(self.root)
        x = ttk.LabeledScale(master)
        self.assertEqual(x.master, master)
        x.destroy()

        # variable initialization/passing
        passed_expected = (('0', 0), (0, 0), (10, 10), (-1, -1),
                           (sys.maxint + 1, sys.maxint + 1))
        if self.wantobjects:
            passed_expected += ((2.5, 2), )
        for pair in passed_expected:
            x = ttk.LabeledScale(self.root, from_=pair[0])
            self.assertEqual(x.value, pair[1])
            x.destroy()
        x = ttk.LabeledScale(self.root, from_='2.5')
        self.assertRaises(ValueError, x._variable.get)
        x.destroy()
        x = ttk.LabeledScale(self.root, from_=None)
        self.assertRaises(ValueError, x._variable.get)
        x.destroy()
        # variable should have its default value set to the from_ value
        myvar = tkinter.DoubleVar(self.root, value=20)
        x = ttk.LabeledScale(self.root, variable=myvar)
        self.assertEqual(x.value, 0)
        x.destroy()
        # check that it is really using a DoubleVar
        x = ttk.LabeledScale(self.root, variable=myvar, from_=0.5)
        self.assertEqual(x.value, 0.5)
        self.assertEqual(x._variable._name, myvar._name)
        x.destroy()

        # widget positionment
        def check_positions(scale, scale_pos, label, label_pos):
            self.assertEqual(scale.pack_info()['side'], scale_pos)
            self.assertEqual(label.place_info()['anchor'], label_pos)

        x = ttk.LabeledScale(self.root, compound='top')
        check_positions(x.scale, 'bottom', x.label, 'n')
        x.destroy()
        x = ttk.LabeledScale(self.root, compound='bottom')
        check_positions(x.scale, 'top', x.label, 's')
        x.destroy()
        # invert default positions
        x = ttk.LabeledScale(self.root, compound='unknown')
        check_positions(x.scale, 'top', x.label, 's')
        x.destroy()
        x = ttk.LabeledScale(self.root)  # take default positions
        check_positions(x.scale, 'bottom', x.label, 'n')
        x.destroy()

        # extra, and invalid, kwargs
        self.assertRaises(tkinter.TclError, ttk.LabeledScale, master, a='b')
Ejemplo n.º 3
0
    def test_variable_change(self):
        x = ttk.LabeledScale()
        x.pack()
        x.wait_visibility()
        x.update()

        curr_xcoord = x.scale.coords()[0]
        newval = x.value + 1
        x.value = newval
        # The following update is needed since the test doesn't use mainloop,
        # at the same time this shouldn't affect test outcome
        x.update()
        self.failUnlessEqual(x.label['text'], newval)
        self.failUnless(x.scale.coords()[0] > curr_xcoord)
        self.failUnlessEqual(x.scale.coords()[0],
                             int(x.label.place_info()['x']))

        # value outside range
        x.value = x.scale['to'] + 1  # no changes shouldn't happen
        x.update()
        self.failUnlessEqual(x.label['text'], newval)
        self.failUnlessEqual(x.scale.coords()[0],
                             int(x.label.place_info()['x']))

        x.destroy()
Ejemplo n.º 4
0
    def test_horizontal_range(self):
        lscale = ttk.LabeledScale(from_=0, to=10)
        lscale.pack()
        lscale.wait_visibility()
        lscale.update()

        linfo_1 = lscale.label.place_info()
        prev_xcoord = lscale.scale.coords()[0]
        self.failUnlessEqual(prev_xcoord, int(linfo_1['x']))
        # change range to: from -5 to 5. This should change the x coord of
        # the scale widget, since 0 is at the middle of the new
        # range.
        lscale.scale.configure(from_=-5, to=5)
        # The following update is needed since the test doesn't use mainloop,
        # at the same time this shouldn't affect test outcome
        lscale.update()
        curr_xcoord = lscale.scale.coords()[0]
        self.failUnless(prev_xcoord != curr_xcoord)
        # the label widget should have been repositioned too
        linfo_2 = lscale.label.place_info()
        self.failUnlessEqual(lscale.label['text'], 0)
        self.failUnlessEqual(curr_xcoord, int(linfo_2['x']))
        # change the range back
        lscale.scale.configure(from_=0, to=10)
        self.failUnless(prev_xcoord != curr_xcoord)
        self.failUnlessEqual(prev_xcoord, int(linfo_1['x']))

        lscale.destroy()
Ejemplo n.º 5
0
    def test_variable_change(self):
        x = ttk.LabeledScale(self.root)
        x.pack()
        x.wait_visibility()
        x.update()

        curr_xcoord = x.scale.coords()[0]
        newval = x.value + 1
        x.value = newval
        # The following update is needed since the test doesn't use mainloop,
        # at the same time this shouldn't affect test outcome
        x.update()
        self.assertEqual(x.label['text'],
                         newval if self.wantobjects else str(newval))
        self.assertGreater(x.scale.coords()[0], curr_xcoord)
        self.assertEqual(x.scale.coords()[0],
            int(x.label.place_info()['x']))

        # value outside range
        if self.wantobjects:
            conv = lambda x: x
        else:
            conv = int
        x.value = conv(x.scale['to']) + 1 # no changes shouldn't happen
        x.update()
        self.assertEqual(conv(x.label['text']), newval)
        self.assertEqual(x.scale.coords()[0],
            int(x.label.place_info()['x']))

        x.destroy()
Ejemplo n.º 6
0
 def test_widget_destroy(self):
     x = ttk.LabeledScale()
     var = x._variable._name
     x.destroy()
     self.assertRaises(Tkinter.TclError, x.tk.globalgetvar, var)
     myvar = Tkinter.DoubleVar()
     name = myvar._name
     x = ttk.LabeledScale(variable=myvar)
     x.destroy()
     if x.tk.wantobjects():
         self.assertEqual(x.tk.globalgetvar(name), myvar.get())
     else:
         self.assertEqual(float(x.tk.globalgetvar(name)), myvar.get())
     del myvar
     self.assertRaises(Tkinter.TclError, x.tk.globalgetvar, name)
     myvar = Tkinter.IntVar()
     x = ttk.LabeledScale(variable=myvar)
     x.destroy()
     ttk.LabeledScale(variable=myvar)
     if hasattr(sys, 'last_type'):
         self.assertNotEqual(sys.last_type, Tkinter.TclError)
Ejemplo n.º 7
0
 def test_initialization_no_master(self):
     # no master passing
     with swap_attr(tkinter, '_default_root', None), \
          swap_attr(tkinter, '_support_default_root', True):
         try:
             x = ttk.LabeledScale()
             self.assertIsNotNone(tkinter._default_root)
             self.assertEqual(x.master, tkinter._default_root)
             self.assertEqual(x.tk, tkinter._default_root.tk)
             x.destroy()
         finally:
             destroy_default_root()
Ejemplo n.º 8
0
 def test_resize(self):
     x = ttk.LabeledScale()
     x.pack(expand=True, fill='both')
     x.wait_visibility()
     x.update()
     width, height = x.master.winfo_width(), x.master.winfo_height()
     width_new, height_new = width * 2, height * 2
     x.value = 3
     x.update()
     x.master.wm_geometry('%dx%d' % (width_new, height_new))
     self.assertEqual(int(x.label.place_info()['x']), x.scale.coords()[0])
     x.master.wm_geometry('%dx%d' % (width, height))
     x.destroy()
Ejemplo n.º 9
0
 def test_horizontal_range(self):
     lscale = ttk.LabeledScale(from_=0, to=10)
     lscale.pack()
     lscale.wait_visibility()
     lscale.update()
     linfo_1 = lscale.label.place_info()
     prev_xcoord = lscale.scale.coords()[0]
     self.assertEqual(prev_xcoord, int(linfo_1['x']))
     lscale.scale.configure(from_=-5, to=5)
     lscale.update()
     curr_xcoord = lscale.scale.coords()[0]
     self.assertNotEqual(prev_xcoord, curr_xcoord)
     linfo_2 = lscale.label.place_info()
     self.assertEqual(lscale.label['text'], 0 if lscale.tk.wantobjects() else '0')
     self.assertEqual(curr_xcoord, int(linfo_2['x']))
     lscale.scale.configure(from_=0, to=10)
     self.assertNotEqual(prev_xcoord, curr_xcoord)
     self.assertEqual(prev_xcoord, int(linfo_1['x']))
     lscale.destroy()
Ejemplo n.º 10
0
 def test_variable_change(self):
     x = ttk.LabeledScale()
     x.pack()
     x.wait_visibility()
     x.update()
     curr_xcoord = x.scale.coords()[0]
     newval = x.value + 1
     x.value = newval
     x.update()
     self.assertEqual(x.label['text'], newval if x.tk.wantobjects() else str(newval))
     self.assertGreater(x.scale.coords()[0], curr_xcoord)
     self.assertEqual(x.scale.coords()[0], int(x.label.place_info()['x']))
     if x.tk.wantobjects():
         conv = lambda x: x
     else:
         conv = int
     x.value = conv(x.scale['to']) + 1
     x.update()
     self.assertEqual(conv(x.label['text']), newval)
     self.assertEqual(x.scale.coords()[0], int(x.label.place_info()['x']))
     x.destroy()
Ejemplo n.º 11
0
    def test_initialization(self):
        x = ttk.LabeledScale()
        self.assertEqual(x.master, Tkinter._default_root)
        x.destroy()
        master = Tkinter.Frame()
        x = ttk.LabeledScale(master)
        self.assertEqual(x.master, master)
        x.destroy()
        passed_expected = (('0', 0),
         (0, 0),
         (10, 10),
         (-1, -1),
         (sys.maxint + 1, sys.maxint + 1))
        if x.tk.wantobjects():
            passed_expected += ((2.5, 2),)
        for pair in passed_expected:
            x = ttk.LabeledScale(from_=pair[0])
            self.assertEqual(x.value, pair[1])
            x.destroy()

        x = ttk.LabeledScale(from_='2.5')
        self.assertRaises(ValueError, x._variable.get)
        x.destroy()
        x = ttk.LabeledScale(from_=None)
        self.assertRaises(ValueError, x._variable.get)
        x.destroy()
        myvar = Tkinter.DoubleVar(value=20)
        x = ttk.LabeledScale(variable=myvar)
        self.assertEqual(x.value, 0)
        x.destroy()
        x = ttk.LabeledScale(variable=myvar, from_=0.5)
        self.assertEqual(x.value, 0.5)
        self.assertEqual(x._variable._name, myvar._name)
        x.destroy()

        def check_positions(scale, scale_pos, label, label_pos):
            self.assertEqual(scale.pack_info()['side'], scale_pos)
            self.assertEqual(label.place_info()['anchor'], label_pos)

        x = ttk.LabeledScale(compound='top')
        check_positions(x.scale, 'bottom', x.label, 'n')
        x.destroy()
        x = ttk.LabeledScale(compound='bottom')
        check_positions(x.scale, 'top', x.label, 's')
        x.destroy()
        x = ttk.LabeledScale(compound='unknown')
        check_positions(x.scale, 'top', x.label, 's')
        x.destroy()
        x = ttk.LabeledScale()
        check_positions(x.scale, 'bottom', x.label, 'n')
        x.destroy()
        self.assertRaises(Tkinter.TclError, ttk.LabeledScale, a='b')
        return