2009-01-28 09:09:03 -04:00
|
|
|
import sys
|
|
|
|
import unittest
|
2014-08-21 04:00:41 -03:00
|
|
|
import Tkinter as tkinter
|
2009-01-28 09:09:03 -04:00
|
|
|
import ttk
|
2014-08-24 03:07:09 -03:00
|
|
|
from test.test_support import requires, run_unittest, swap_attr
|
|
|
|
from test_ttk.support import AbstractTkTest, destroy_default_root
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
requires('gui')
|
|
|
|
|
2014-08-24 03:07:09 -03:00
|
|
|
class LabeledScaleTest(AbstractTkTest, unittest.TestCase):
|
2009-02-01 20:38:54 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
2014-08-24 03:07:09 -03:00
|
|
|
self.root.update_idletasks()
|
|
|
|
super(LabeledScaleTest, self).tearDown()
|
2009-02-01 20:38:54 -04:00
|
|
|
|
2009-01-28 09:09:03 -04:00
|
|
|
def test_widget_destroy(self):
|
|
|
|
# automatically created variable
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root)
|
2009-01-28 09:09:03 -04:00
|
|
|
var = x._variable._name
|
|
|
|
x.destroy()
|
2014-08-21 04:00:41 -03:00
|
|
|
self.assertRaises(tkinter.TclError, x.tk.globalgetvar, var)
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
# manually created variable
|
2014-08-24 03:07:09 -03:00
|
|
|
myvar = tkinter.DoubleVar(self.root)
|
2009-01-28 09:09:03 -04:00
|
|
|
name = myvar._name
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root, variable=myvar)
|
2009-01-28 09:09:03 -04:00
|
|
|
x.destroy()
|
2014-08-24 03:07:09 -03:00
|
|
|
if self.wantobjects:
|
2014-01-07 13:32:58 -04:00
|
|
|
self.assertEqual(x.tk.globalgetvar(name), myvar.get())
|
|
|
|
else:
|
|
|
|
self.assertEqual(float(x.tk.globalgetvar(name)), myvar.get())
|
2009-01-28 09:09:03 -04:00
|
|
|
del myvar
|
2014-08-21 04:00:41 -03:00
|
|
|
self.assertRaises(tkinter.TclError, x.tk.globalgetvar, name)
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
# checking that the tracing callback is properly removed
|
2014-08-24 03:07:09 -03:00
|
|
|
myvar = tkinter.IntVar(self.root)
|
2009-01-28 09:09:03 -04:00
|
|
|
# LabeledScale will start tracing myvar
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root, variable=myvar)
|
2009-01-28 09:09:03 -04:00
|
|
|
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.
|
2014-08-24 03:07:09 -03:00
|
|
|
ttk.LabeledScale(self.root, variable=myvar)
|
2009-01-28 09:09:03 -04:00
|
|
|
if hasattr(sys, 'last_type'):
|
2014-08-21 04:00:41 -03:00
|
|
|
self.assertNotEqual(sys.last_type, tkinter.TclError)
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
|
2014-08-24 03:07:09 -03:00
|
|
|
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()
|
|
|
|
|
2009-01-28 09:09:03 -04:00
|
|
|
def test_initialization(self):
|
|
|
|
# master passing
|
2014-08-24 03:07:09 -03:00
|
|
|
master = tkinter.Frame(self.root)
|
2009-01-28 09:09:03 -04:00
|
|
|
x = ttk.LabeledScale(master)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(x.master, master)
|
2009-01-28 09:09:03 -04:00
|
|
|
x.destroy()
|
|
|
|
|
|
|
|
# variable initialization/passing
|
2014-01-07 13:32:58 -04:00
|
|
|
passed_expected = (('0', 0), (0, 0), (10, 10),
|
2009-01-28 09:09:03 -04:00
|
|
|
(-1, -1), (sys.maxint + 1, sys.maxint + 1))
|
2014-08-24 03:07:09 -03:00
|
|
|
if self.wantobjects:
|
2014-01-07 13:32:58 -04:00
|
|
|
passed_expected += ((2.5, 2),)
|
2009-01-28 09:09:03 -04:00
|
|
|
for pair in passed_expected:
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root, from_=pair[0])
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(x.value, pair[1])
|
2009-01-28 09:09:03 -04:00
|
|
|
x.destroy()
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root, from_='2.5')
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertRaises(ValueError, x._variable.get)
|
2009-01-28 09:09:03 -04:00
|
|
|
x.destroy()
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root, from_=None)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertRaises(ValueError, x._variable.get)
|
2009-01-28 09:09:03 -04:00
|
|
|
x.destroy()
|
|
|
|
# variable should have its default value set to the from_ value
|
2014-08-24 03:07:09 -03:00
|
|
|
myvar = tkinter.DoubleVar(self.root, value=20)
|
|
|
|
x = ttk.LabeledScale(self.root, variable=myvar)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(x.value, 0)
|
2009-01-28 09:09:03 -04:00
|
|
|
x.destroy()
|
|
|
|
# check that it is really using a DoubleVar
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root, variable=myvar, from_=0.5)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(x.value, 0.5)
|
|
|
|
self.assertEqual(x._variable._name, myvar._name)
|
2009-01-28 09:09:03 -04:00
|
|
|
x.destroy()
|
|
|
|
|
|
|
|
# widget positionment
|
|
|
|
def check_positions(scale, scale_pos, label, label_pos):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(scale.pack_info()['side'], scale_pos)
|
|
|
|
self.assertEqual(label.place_info()['anchor'], label_pos)
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root, compound='top')
|
2009-01-28 09:09:03 -04:00
|
|
|
check_positions(x.scale, 'bottom', x.label, 'n')
|
|
|
|
x.destroy()
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root, compound='bottom')
|
2009-01-28 09:09:03 -04:00
|
|
|
check_positions(x.scale, 'top', x.label, 's')
|
|
|
|
x.destroy()
|
2014-08-24 03:07:09 -03:00
|
|
|
# invert default positions
|
|
|
|
x = ttk.LabeledScale(self.root, compound='unknown')
|
2009-01-28 09:09:03 -04:00
|
|
|
check_positions(x.scale, 'top', x.label, 's')
|
|
|
|
x.destroy()
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root) # take default positions
|
2009-01-28 09:09:03 -04:00
|
|
|
check_positions(x.scale, 'bottom', x.label, 'n')
|
|
|
|
x.destroy()
|
|
|
|
|
|
|
|
# extra, and invalid, kwargs
|
2014-08-24 03:07:09 -03:00
|
|
|
self.assertRaises(tkinter.TclError, ttk.LabeledScale, master, a='b')
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_horizontal_range(self):
|
2014-08-24 03:07:09 -03:00
|
|
|
lscale = ttk.LabeledScale(self.root, from_=0, to=10)
|
2009-01-28 09:09:03 -04:00
|
|
|
lscale.pack()
|
|
|
|
lscale.wait_visibility()
|
|
|
|
lscale.update()
|
|
|
|
|
|
|
|
linfo_1 = lscale.label.place_info()
|
|
|
|
prev_xcoord = lscale.scale.coords()[0]
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(prev_xcoord, int(linfo_1['x']))
|
2009-01-28 09:09:03 -04:00
|
|
|
# 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]
|
2013-11-16 18:43:03 -04:00
|
|
|
self.assertNotEqual(prev_xcoord, curr_xcoord)
|
2009-01-28 09:09:03 -04:00
|
|
|
# the label widget should have been repositioned too
|
|
|
|
linfo_2 = lscale.label.place_info()
|
2014-08-24 03:07:09 -03:00
|
|
|
self.assertEqual(lscale.label['text'], 0 if self.wantobjects else '0')
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(curr_xcoord, int(linfo_2['x']))
|
2009-01-28 09:09:03 -04:00
|
|
|
# change the range back
|
|
|
|
lscale.scale.configure(from_=0, to=10)
|
2013-11-16 18:43:03 -04:00
|
|
|
self.assertNotEqual(prev_xcoord, curr_xcoord)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(prev_xcoord, int(linfo_1['x']))
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
lscale.destroy()
|
|
|
|
|
|
|
|
|
|
|
|
def test_variable_change(self):
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root)
|
2009-01-28 09:09:03 -04:00
|
|
|
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()
|
2014-01-07 13:32:58 -04:00
|
|
|
self.assertEqual(x.label['text'],
|
2014-08-24 03:07:09 -03:00
|
|
|
newval if self.wantobjects else str(newval))
|
2013-11-16 18:43:03 -04:00
|
|
|
self.assertGreater(x.scale.coords()[0], curr_xcoord)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(x.scale.coords()[0],
|
2009-01-28 09:09:03 -04:00
|
|
|
int(x.label.place_info()['x']))
|
|
|
|
|
|
|
|
# value outside range
|
2014-08-24 03:07:09 -03:00
|
|
|
if self.wantobjects:
|
2014-01-07 13:32:58 -04:00
|
|
|
conv = lambda x: x
|
|
|
|
else:
|
|
|
|
conv = int
|
|
|
|
x.value = conv(x.scale['to']) + 1 # no changes shouldn't happen
|
2009-01-28 09:09:03 -04:00
|
|
|
x.update()
|
2014-01-07 13:32:58 -04:00
|
|
|
self.assertEqual(conv(x.label['text']), newval)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(x.scale.coords()[0],
|
2009-01-28 09:09:03 -04:00
|
|
|
int(x.label.place_info()['x']))
|
|
|
|
|
|
|
|
x.destroy()
|
|
|
|
|
|
|
|
|
|
|
|
def test_resize(self):
|
2014-08-24 03:07:09 -03:00
|
|
|
x = ttk.LabeledScale(self.root)
|
2009-01-28 09:09:03 -04:00
|
|
|
x.pack(expand=True, fill='both')
|
|
|
|
x.wait_visibility()
|
|
|
|
x.update()
|
|
|
|
|
|
|
|
width, height = x.master.winfo_width(), x.master.winfo_height()
|
2009-02-02 16:23:29 -04:00
|
|
|
width_new, height_new = width * 2, height * 2
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
x.value = 3
|
|
|
|
x.update()
|
2009-02-02 16:23:29 -04:00
|
|
|
x.master.wm_geometry("%dx%d" % (width_new, height_new))
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(int(x.label.place_info()['x']),
|
2009-01-28 09:09:03 -04:00
|
|
|
x.scale.coords()[0])
|
|
|
|
|
2009-02-02 16:23:29 -04:00
|
|
|
# Reset geometry
|
2009-01-28 09:09:03 -04:00
|
|
|
x.master.wm_geometry("%dx%d" % (width, height))
|
|
|
|
x.destroy()
|
|
|
|
|
|
|
|
|
2014-08-24 03:07:09 -03:00
|
|
|
class OptionMenuTest(AbstractTkTest, unittest.TestCase):
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
def setUp(self):
|
2014-08-24 03:07:09 -03:00
|
|
|
super(OptionMenuTest, self).setUp()
|
|
|
|
self.textvar = tkinter.StringVar(self.root)
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
del self.textvar
|
2014-08-24 03:07:09 -03:00
|
|
|
super(OptionMenuTest, self).tearDown()
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_widget_destroy(self):
|
2014-08-24 03:07:09 -03:00
|
|
|
var = tkinter.StringVar(self.root)
|
|
|
|
optmenu = ttk.OptionMenu(self.root, var)
|
2009-01-28 09:09:03 -04:00
|
|
|
name = var._name
|
|
|
|
optmenu.update_idletasks()
|
|
|
|
optmenu.destroy()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(optmenu.tk.globalgetvar(name), var.get())
|
2009-01-28 09:09:03 -04:00
|
|
|
del var
|
2014-08-21 04:00:41 -03:00
|
|
|
self.assertRaises(tkinter.TclError, optmenu.tk.globalgetvar, name)
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_initialization(self):
|
2014-08-21 04:00:41 -03:00
|
|
|
self.assertRaises(tkinter.TclError,
|
2014-08-24 03:07:09 -03:00
|
|
|
ttk.OptionMenu, self.root, self.textvar, invalid='thing')
|
2009-01-28 09:09:03 -04:00
|
|
|
|
2014-08-24 03:07:09 -03:00
|
|
|
optmenu = ttk.OptionMenu(self.root, self.textvar, 'b', 'a', 'b')
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(optmenu._variable.get(), 'b')
|
2009-01-28 09:09:03 -04:00
|
|
|
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(optmenu['menu'])
|
|
|
|
self.assertTrue(optmenu['textvariable'])
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
optmenu.destroy()
|
|
|
|
|
|
|
|
|
|
|
|
def test_menu(self):
|
|
|
|
items = ('a', 'b', 'c')
|
|
|
|
default = 'a'
|
2014-08-24 03:07:09 -03:00
|
|
|
optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
|
2009-01-28 09:09:03 -04:00
|
|
|
found_default = False
|
|
|
|
for i in range(len(items)):
|
|
|
|
value = optmenu['menu'].entrycget(i, 'value')
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(value, items[i])
|
2009-01-28 09:09:03 -04:00
|
|
|
if value == default:
|
|
|
|
found_default = True
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(found_default)
|
2009-01-28 09:09:03 -04:00
|
|
|
optmenu.destroy()
|
|
|
|
|
|
|
|
# default shouldn't be in menu if it is not part of values
|
|
|
|
default = 'd'
|
2014-08-24 03:07:09 -03:00
|
|
|
optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
|
2009-01-28 09:09:03 -04:00
|
|
|
curr = None
|
|
|
|
i = 0
|
|
|
|
while True:
|
|
|
|
last, curr = curr, optmenu['menu'].entryconfigure(i, 'value')
|
|
|
|
if last == curr:
|
|
|
|
# no more menu entries
|
|
|
|
break
|
2013-11-16 18:43:03 -04:00
|
|
|
self.assertNotEqual(curr, default)
|
2009-01-28 09:09:03 -04:00
|
|
|
i += 1
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(i, len(items))
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
# check that variable is updated correctly
|
|
|
|
optmenu.pack()
|
|
|
|
optmenu.wait_visibility()
|
|
|
|
optmenu['menu'].invoke(0)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(optmenu._variable.get(), items[0])
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
# changing to an invalid index shouldn't change the variable
|
2014-08-21 04:00:41 -03:00
|
|
|
self.assertRaises(tkinter.TclError, optmenu['menu'].invoke, -1)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(optmenu._variable.get(), items[0])
|
2009-01-28 09:09:03 -04:00
|
|
|
|
|
|
|
optmenu.destroy()
|
|
|
|
|
|
|
|
# specifying a callback
|
|
|
|
success = []
|
|
|
|
def cb_test(item):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(item, items[1])
|
2009-01-28 09:09:03 -04:00
|
|
|
success.append(True)
|
2014-08-24 03:07:09 -03:00
|
|
|
optmenu = ttk.OptionMenu(self.root, self.textvar, 'a', command=cb_test,
|
2009-01-28 09:09:03 -04:00
|
|
|
*items)
|
|
|
|
optmenu['menu'].invoke(1)
|
|
|
|
if not success:
|
|
|
|
self.fail("Menu callback not invoked")
|
|
|
|
|
|
|
|
optmenu.destroy()
|
|
|
|
|
|
|
|
|
|
|
|
tests_gui = (LabeledScaleTest, OptionMenuTest)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
run_unittest(*tests_gui)
|