| 1 |
import unittest |
| 2 |
import tkinter |
| 3 |
from tkinter import ttk, TclError |
| 4 |
from test.support import requires, gc_collect |
| 5 |
import sys |
| 6 |
|
| 7 |
from test.test_ttk_textonly import MockTclObj |
| 8 |
from test.test_tkinter.support import setUpModule # noqa: F401 |
| 9 |
from test.test_tkinter.support import ( |
| 10 |
AbstractTkTest, requires_tk, tk_version, get_tk_patchlevel, |
| 11 |
simulate_mouse_click, AbstractDefaultRootTest) |
| 12 |
from test.test_tkinter.widget_tests import (add_configure_tests, |
| 13 |
AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests) |
| 14 |
|
| 15 |
requires('gui') |
| 16 |
|
| 17 |
|
| 18 |
class StandardTtkOptionsTests(StandardOptionsTests): |
| 19 |
|
| 20 |
def test_configure_class(self): |
| 21 |
widget = self.create() |
| 22 |
self.assertEqual(widget['class'], '') |
| 23 |
errmsg='attempt to change read-only option' |
| 24 |
if get_tk_patchlevel(self.root) < (8, 6, 0, 'beta', 3): |
| 25 |
errmsg='Attempt to change read-only option' |
| 26 |
self.checkInvalidParam(widget, 'class', 'Foo', errmsg=errmsg) |
| 27 |
widget2 = self.create(class_='Foo') |
| 28 |
self.assertEqual(widget2['class'], 'Foo') |
| 29 |
|
| 30 |
def test_configure_padding(self): |
| 31 |
widget = self.create() |
| 32 |
if get_tk_patchlevel(self.root) < (8, 6, 14): |
| 33 |
def padding_conv(value): |
| 34 |
self.assertIsInstance(value, tuple) |
| 35 |
return tuple(map(str, value)) |
| 36 |
else: |
| 37 |
padding_conv = None |
| 38 |
self.checkParam(widget, 'padding', 0, expected=(0,), conv=padding_conv) |
| 39 |
self.checkParam(widget, 'padding', 5, expected=(5,), conv=padding_conv) |
| 40 |
self.checkParam(widget, 'padding', (5, 6), |
| 41 |
expected=(5, 6), conv=padding_conv) |
| 42 |
self.checkParam(widget, 'padding', (5, 6, 7), |
| 43 |
expected=(5, 6, 7), conv=padding_conv) |
| 44 |
self.checkParam(widget, 'padding', (5, 6, 7, 8), |
| 45 |
expected=(5, 6, 7, 8), conv=padding_conv) |
| 46 |
self.checkParam(widget, 'padding', ('5p', '6p', '7p', '8p')) |
| 47 |
self.checkParam(widget, 'padding', (), expected='') |
| 48 |
|
| 49 |
def test_configure_state(self): |
| 50 |
widget = self.create() |
| 51 |
self.checkParams(widget, 'state', 'active', 'disabled', 'readonly') |
| 52 |
|
| 53 |
def test_configure_style(self): |
| 54 |
widget = self.create() |
| 55 |
self.assertEqual(widget['style'], '') |
| 56 |
errmsg = 'Layout Foo not found' |
| 57 |
if hasattr(self, 'default_orient'): |
| 58 |
errmsg = ('Layout %s.Foo not found' % |
| 59 |
getattr(self, 'default_orient').title()) |
| 60 |
self.checkInvalidParam(widget, 'style', 'Foo', |
| 61 |
errmsg=errmsg) |
| 62 |
widget2 = self.create(class_='Foo') |
| 63 |
self.assertEqual(widget2['class'], 'Foo') |
| 64 |
# XXX |
| 65 |
|
| 66 |
def test_configure_relief(self): |
| 67 |
widget = self.create() |
| 68 |
self.checkReliefParam(widget, 'relief', |
| 69 |
allow_empty=(tk_version >= (8, 7))) |
| 70 |
|
| 71 |
|
| 72 |
class WidgetTest(AbstractTkTest, unittest.TestCase): |
| 73 |
"""Tests methods available in every ttk widget.""" |
| 74 |
|
| 75 |
def setUp(self): |
| 76 |
super().setUp() |
| 77 |
self.widget = ttk.Button(self.root, width=0, text="Text") |
| 78 |
self.widget.pack() |
| 79 |
|
| 80 |
def test_identify(self): |
| 81 |
self.widget.update() |
| 82 |
self.assertEqual(self.widget.identify( |
| 83 |
int(self.widget.winfo_width() / 2), |
| 84 |
int(self.widget.winfo_height() / 2) |
| 85 |
), "label") |
| 86 |
self.assertEqual(self.widget.identify(-1, -1), "") |
| 87 |
|
| 88 |
self.assertRaises(tkinter.TclError, self.widget.identify, None, 5) |
| 89 |
self.assertRaises(tkinter.TclError, self.widget.identify, 5, None) |
| 90 |
self.assertRaises(tkinter.TclError, self.widget.identify, 5, '') |
| 91 |
|
| 92 |
def test_widget_state(self): |
| 93 |
# XXX not sure about the portability of all these tests |
| 94 |
self.assertEqual(self.widget.state(), ()) |
| 95 |
self.assertEqual(self.widget.instate(['!disabled']), True) |
| 96 |
|
| 97 |
# changing from !disabled to disabled |
| 98 |
self.assertEqual(self.widget.state(['disabled']), ('!disabled', )) |
| 99 |
# no state change |
| 100 |
self.assertEqual(self.widget.state(['disabled']), ()) |
| 101 |
# change back to !disable but also active |
| 102 |
self.assertEqual(self.widget.state(['!disabled', 'active']), |
| 103 |
('!active', 'disabled')) |
| 104 |
# no state changes, again |
| 105 |
self.assertEqual(self.widget.state(['!disabled', 'active']), ()) |
| 106 |
self.assertEqual(self.widget.state(['active', '!disabled']), ()) |
| 107 |
|
| 108 |
def test_cb(arg1, **kw): |
| 109 |
return arg1, kw |
| 110 |
self.assertEqual(self.widget.instate(['!disabled'], |
| 111 |
test_cb, "hi", **{"msg": "there"}), |
| 112 |
('hi', {'msg': 'there'})) |
| 113 |
|
| 114 |
# attempt to set invalid statespec |
| 115 |
currstate = self.widget.state() |
| 116 |
self.assertRaises(tkinter.TclError, self.widget.instate, |
| 117 |
['badstate']) |
| 118 |
self.assertRaises(tkinter.TclError, self.widget.instate, |
| 119 |
['disabled', 'badstate']) |
| 120 |
# verify that widget didn't change its state |
| 121 |
self.assertEqual(currstate, self.widget.state()) |
| 122 |
|
| 123 |
# ensuring that passing None as state doesn't modify current state |
| 124 |
self.widget.state(['active', '!disabled']) |
| 125 |
self.assertEqual(self.widget.state(), ('active', )) |
| 126 |
|
| 127 |
|
| 128 |
class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests): |
| 129 |
_rounds_pixels = False |
| 130 |
_clipped = {} |
| 131 |
|
| 132 |
|
| 133 |
@add_configure_tests(StandardTtkOptionsTests) |
| 134 |
class FrameTest(AbstractToplevelTest, unittest.TestCase): |
| 135 |
OPTIONS = ( |
| 136 |
'borderwidth', 'class', 'cursor', 'height', |
| 137 |
'padding', 'relief', 'style', 'takefocus', |
| 138 |
'width', |
| 139 |
) |
| 140 |
|
| 141 |
def create(self, **kwargs): |
| 142 |
return ttk.Frame(self.root, **kwargs) |
| 143 |
|
| 144 |
|
| 145 |
@add_configure_tests(StandardTtkOptionsTests) |
| 146 |
class LabelFrameTest(AbstractToplevelTest, unittest.TestCase): |
| 147 |
OPTIONS = ( |
| 148 |
'borderwidth', 'class', 'cursor', 'height', |
| 149 |
'labelanchor', 'labelwidget', |
| 150 |
'padding', 'relief', 'style', 'takefocus', |
| 151 |
'text', 'underline', 'width', |
| 152 |
) |
| 153 |
|
| 154 |
def create(self, **kwargs): |
| 155 |
return ttk.LabelFrame(self.root, **kwargs) |
| 156 |
|
| 157 |
def test_configure_labelanchor(self): |
| 158 |
widget = self.create() |
| 159 |
self.checkEnumParam(widget, 'labelanchor', |
| 160 |
'e', 'en', 'es', 'n', 'ne', 'nw', 's', 'se', 'sw', 'w', 'wn', 'ws', |
| 161 |
errmsg='Bad label anchor specification {}') |
| 162 |
self.checkInvalidParam(widget, 'labelanchor', 'center') |
| 163 |
|
| 164 |
def test_configure_labelwidget(self): |
| 165 |
widget = self.create() |
| 166 |
label = ttk.Label(self.root, text='Mupp', name='foo') |
| 167 |
self.checkParam(widget, 'labelwidget', label, expected='.foo') |
| 168 |
label.destroy() |
| 169 |
|
| 170 |
|
| 171 |
class AbstractLabelTest(AbstractWidgetTest): |
| 172 |
_allow_empty_justify = True |
| 173 |
_rounds_pixels = False |
| 174 |
_clipped = {} |
| 175 |
|
| 176 |
def checkImageParam(self, widget, name): |
| 177 |
image = tkinter.PhotoImage(master=self.root, name='image1') |
| 178 |
image2 = tkinter.PhotoImage(master=self.root, name='image2') |
| 179 |
self.checkParam(widget, name, image, expected=('image1',)) |
| 180 |
self.checkParam(widget, name, 'image1', expected=('image1',)) |
| 181 |
self.checkParam(widget, name, (image,), expected=('image1',)) |
| 182 |
self.checkParam(widget, name, (image, 'active', image2), |
| 183 |
expected=('image1', 'active', 'image2')) |
| 184 |
self.checkParam(widget, name, 'image1 active image2', |
| 185 |
expected=('image1', 'active', 'image2')) |
| 186 |
if tk_version < (8, 7): |
| 187 |
errmsg = 'image "spam" doesn\'t exist' |
| 188 |
else: |
| 189 |
errmsg = 'image "spam" does not exist' |
| 190 |
self.checkInvalidParam(widget, name, 'spam', errmsg=errmsg) |
| 191 |
|
| 192 |
def test_configure_compound(self): |
| 193 |
values = ('none', 'text', 'image', 'center', 'top', 'bottom', 'left', 'right') |
| 194 |
if tk_version >= (8, 7): |
| 195 |
values += ('',) |
| 196 |
widget = self.create() |
| 197 |
self.checkEnumParam(widget, 'compound', *values, allow_empty=True) |
| 198 |
|
| 199 |
test_configure_justify = requires_tk(8, 7)(StandardOptionsTests.test_configure_justify) |
| 200 |
|
| 201 |
def test_configure_width(self): |
| 202 |
widget = self.create() |
| 203 |
self.checkParams(widget, 'width', 402, -402, 0) |
| 204 |
|
| 205 |
|
| 206 |
@add_configure_tests(StandardTtkOptionsTests) |
| 207 |
class LabelTest(AbstractLabelTest, unittest.TestCase): |
| 208 |
OPTIONS = ( |
| 209 |
'anchor', 'background', 'borderwidth', |
| 210 |
'class', 'compound', 'cursor', 'font', 'foreground', |
| 211 |
'image', 'justify', 'padding', 'relief', 'state', 'style', |
| 212 |
'takefocus', 'text', 'textvariable', |
| 213 |
'underline', 'width', 'wraplength', |
| 214 |
) |
| 215 |
_conv_pixels = False |
| 216 |
_allow_empty_justify = tk_version >= (8, 7) |
| 217 |
|
| 218 |
def create(self, **kwargs): |
| 219 |
return ttk.Label(self.root, **kwargs) |
| 220 |
|
| 221 |
test_configure_justify = StandardOptionsTests.test_configure_justify |
| 222 |
|
| 223 |
|
| 224 |
@add_configure_tests(StandardTtkOptionsTests) |
| 225 |
class ButtonTest(AbstractLabelTest, unittest.TestCase): |
| 226 |
OPTIONS = ( |
| 227 |
'class', 'command', 'compound', 'cursor', 'default', |
| 228 |
'image', 'justify', 'padding', 'state', 'style', |
| 229 |
'takefocus', 'text', 'textvariable', |
| 230 |
'underline', 'width', |
| 231 |
) |
| 232 |
|
| 233 |
def create(self, **kwargs): |
| 234 |
return ttk.Button(self.root, **kwargs) |
| 235 |
|
| 236 |
def test_configure_default(self): |
| 237 |
widget = self.create() |
| 238 |
values = ('normal', 'active', 'disabled') |
| 239 |
self.checkEnumParam(widget, 'default', *values, |
| 240 |
sort=tk_version >= (8, 7)) |
| 241 |
|
| 242 |
def test_invoke(self): |
| 243 |
success = [] |
| 244 |
btn = ttk.Button(self.root, command=lambda: success.append(1)) |
| 245 |
btn.invoke() |
| 246 |
self.assertTrue(success) |
| 247 |
|
| 248 |
|
| 249 |
@add_configure_tests(StandardTtkOptionsTests) |
| 250 |
class CheckbuttonTest(AbstractLabelTest, unittest.TestCase): |
| 251 |
OPTIONS = ( |
| 252 |
'class', 'command', 'compound', 'cursor', |
| 253 |
'image', 'justify', |
| 254 |
'offvalue', 'onvalue', |
| 255 |
'padding', 'state', 'style', |
| 256 |
'takefocus', 'text', 'textvariable', |
| 257 |
'underline', 'variable', 'width', |
| 258 |
) |
| 259 |
|
| 260 |
def create(self, **kwargs): |
| 261 |
return ttk.Checkbutton(self.root, **kwargs) |
| 262 |
|
| 263 |
def test_configure_offvalue(self): |
| 264 |
widget = self.create() |
| 265 |
self.checkParams(widget, 'offvalue', 1, 2.3, '', 'any string') |
| 266 |
|
| 267 |
def test_configure_onvalue(self): |
| 268 |
widget = self.create() |
| 269 |
self.checkParams(widget, 'onvalue', 1, 2.3, '', 'any string') |
| 270 |
|
| 271 |
def test_invoke(self): |
| 272 |
success = [] |
| 273 |
def cb_test(): |
| 274 |
success.append(1) |
| 275 |
return "cb test called" |
| 276 |
|
| 277 |
cbtn = ttk.Checkbutton(self.root, command=cb_test) |
| 278 |
# the variable automatically created by ttk.Checkbutton is actually |
| 279 |
# undefined till we invoke the Checkbutton |
| 280 |
self.assertEqual(cbtn.state(), ('alternate', )) |
| 281 |
self.assertRaises(tkinter.TclError, cbtn.tk.globalgetvar, |
| 282 |
cbtn['variable']) |
| 283 |
|
| 284 |
res = cbtn.invoke() |
| 285 |
self.assertEqual(res, "cb test called") |
| 286 |
self.assertEqual(cbtn['onvalue'], |
| 287 |
cbtn.tk.globalgetvar(cbtn['variable'])) |
| 288 |
self.assertTrue(success) |
| 289 |
|
| 290 |
cbtn['command'] = '' |
| 291 |
res = cbtn.invoke() |
| 292 |
if tk_version >= (8, 7) and self.wantobjects: |
| 293 |
self.assertEqual(res, ()) |
| 294 |
else: |
| 295 |
self.assertEqual(str(res), '') |
| 296 |
self.assertLessEqual(len(success), 1) |
| 297 |
self.assertEqual(cbtn['offvalue'], |
| 298 |
cbtn.tk.globalgetvar(cbtn['variable'])) |
| 299 |
|
| 300 |
def test_unique_variables(self): |
| 301 |
frames = [] |
| 302 |
buttons = [] |
| 303 |
for i in range(2): |
| 304 |
f = ttk.Frame(self.root) |
| 305 |
f.pack() |
| 306 |
frames.append(f) |
| 307 |
for j in 'AB': |
| 308 |
b = ttk.Checkbutton(f, text=j) |
| 309 |
b.pack() |
| 310 |
buttons.append(b) |
| 311 |
variables = [str(b['variable']) for b in buttons] |
| 312 |
self.assertEqual(len(set(variables)), 4, variables) |
| 313 |
|
| 314 |
def test_unique_variables2(self): |
| 315 |
buttons = [] |
| 316 |
f = ttk.Frame(self.root) |
| 317 |
f.pack() |
| 318 |
f = ttk.Frame(self.root) |
| 319 |
f.pack() |
| 320 |
for j in 'AB': |
| 321 |
b = tkinter.Checkbutton(f, text=j) |
| 322 |
b.pack() |
| 323 |
buttons.append(b) |
| 324 |
# Should be larger than the number of all previously created |
| 325 |
# tkinter.Checkbutton widgets: |
| 326 |
for j in range(100): |
| 327 |
b = ttk.Checkbutton(f, text=str(j)) |
| 328 |
b.pack() |
| 329 |
buttons.append(b) |
| 330 |
names = [str(b) for b in buttons] |
| 331 |
self.assertEqual(len(set(names)), len(buttons), names) |
| 332 |
variables = [str(b['variable']) for b in buttons] |
| 333 |
self.assertEqual(len(set(variables)), len(buttons), variables) |
| 334 |
|
| 335 |
|
| 336 |
@add_configure_tests(IntegerSizeTests, StandardTtkOptionsTests) |
| 337 |
class EntryTest(AbstractWidgetTest, unittest.TestCase): |
| 338 |
OPTIONS = ( |
| 339 |
'background', 'class', 'cursor', |
| 340 |
'exportselection', 'font', 'foreground', |
| 341 |
'invalidcommand', 'justify', |
| 342 |
'placeholder', 'placeholderforeground', |
| 343 |
'show', 'state', 'style', 'takefocus', 'textvariable', |
| 344 |
'validate', 'validatecommand', 'width', 'xscrollcommand', |
| 345 |
) |
| 346 |
_rounds_pixels = False |
| 347 |
_clipped = {} |
| 348 |
# bpo-27313: macOS Tk/Tcl may or may not report 'Entry.field'. |
| 349 |
IDENTIFY_AS = {'Entry.field', 'textarea'} |
| 350 |
|
| 351 |
def setUp(self): |
| 352 |
super().setUp() |
| 353 |
self.entry = self.create() |
| 354 |
|
| 355 |
def create(self, **kwargs): |
| 356 |
return ttk.Entry(self.root, **kwargs) |
| 357 |
|
| 358 |
def test_configure_invalidcommand(self): |
| 359 |
widget = self.create() |
| 360 |
self.checkCommandParam(widget, 'invalidcommand') |
| 361 |
|
| 362 |
def test_configure_show(self): |
| 363 |
widget = self.create() |
| 364 |
self.checkParam(widget, 'show', '*') |
| 365 |
self.checkParam(widget, 'show', '') |
| 366 |
self.checkParam(widget, 'show', ' ') |
| 367 |
|
| 368 |
def test_configure_validate(self): |
| 369 |
widget = self.create() |
| 370 |
self.checkEnumParam(widget, 'validate', |
| 371 |
'all', 'key', 'focus', 'focusin', 'focusout', 'none') |
| 372 |
|
| 373 |
def test_configure_validatecommand(self): |
| 374 |
widget = self.create() |
| 375 |
self.checkCommandParam(widget, 'validatecommand') |
| 376 |
|
| 377 |
def test_bbox(self): |
| 378 |
self.assertIsBoundingBox(self.entry.bbox(0)) |
| 379 |
self.assertRaises(tkinter.TclError, self.entry.bbox, 'noindex') |
| 380 |
self.assertRaises(tkinter.TclError, self.entry.bbox, None) |
| 381 |
|
| 382 |
def test_identify(self): |
| 383 |
if (tk_version >= (9, 0) and sys.platform == 'darwin' |
| 384 |
and isinstance(self.entry, ttk.Combobox)): |
| 385 |
self.skipTest('Test does not work on macOS Tk 9.') |
| 386 |
# https://core.tcl-lang.org/tk/tktview/8b49e9cfa6 |
| 387 |
self.entry.pack() |
| 388 |
self.root.update() |
| 389 |
|
| 390 |
self.assertIn(self.entry.identify(5, 5), self.IDENTIFY_AS) |
| 391 |
self.assertEqual(self.entry.identify(-1, -1), "") |
| 392 |
|
| 393 |
self.assertRaises(tkinter.TclError, self.entry.identify, None, 5) |
| 394 |
self.assertRaises(tkinter.TclError, self.entry.identify, 5, None) |
| 395 |
self.assertRaises(tkinter.TclError, self.entry.identify, 5, '') |
| 396 |
|
| 397 |
def test_validation_options(self): |
| 398 |
success = [] |
| 399 |
test_invalid = lambda: success.append(True) |
| 400 |
|
| 401 |
self.entry['validate'] = 'none' |
| 402 |
self.entry['validatecommand'] = lambda: False |
| 403 |
|
| 404 |
self.entry['invalidcommand'] = test_invalid |
| 405 |
self.entry.validate() |
| 406 |
self.assertTrue(success) |
| 407 |
|
| 408 |
self.entry['invalidcommand'] = '' |
| 409 |
self.entry.validate() |
| 410 |
self.assertEqual(len(success), 1) |
| 411 |
|
| 412 |
self.entry['invalidcommand'] = test_invalid |
| 413 |
self.entry['validatecommand'] = lambda: True |
| 414 |
self.entry.validate() |
| 415 |
self.assertEqual(len(success), 1) |
| 416 |
|
| 417 |
self.entry['validatecommand'] = '' |
| 418 |
self.entry.validate() |
| 419 |
self.assertEqual(len(success), 1) |
| 420 |
|
| 421 |
self.entry['validatecommand'] = True |
| 422 |
self.assertRaises(tkinter.TclError, self.entry.validate) |
| 423 |
|
| 424 |
def test_validation(self): |
| 425 |
validation = [] |
| 426 |
def validate(to_insert): |
| 427 |
if not 'a' <= to_insert.lower() <= 'z': |
| 428 |
validation.append(False) |
| 429 |
return False |
| 430 |
validation.append(True) |
| 431 |
return True |
| 432 |
|
| 433 |
self.entry['validate'] = 'key' |
| 434 |
self.entry['validatecommand'] = self.entry.register(validate), '%S' |
| 435 |
|
| 436 |
self.entry.insert('end', 1) |
| 437 |
self.entry.insert('end', 'a') |
| 438 |
self.assertEqual(validation, [False, True]) |
| 439 |
self.assertEqual(self.entry.get(), 'a') |
| 440 |
|
| 441 |
def test_revalidation(self): |
| 442 |
def validate(content): |
| 443 |
for letter in content: |
| 444 |
if not 'a' <= letter.lower() <= 'z': |
| 445 |
return False |
| 446 |
return True |
| 447 |
|
| 448 |
self.entry['validatecommand'] = self.entry.register(validate), '%P' |
| 449 |
|
| 450 |
self.entry.insert('end', 'avocado') |
| 451 |
self.assertEqual(self.entry.validate(), True) |
| 452 |
self.assertEqual(self.entry.state(), ()) |
| 453 |
|
| 454 |
self.entry.delete(0, 'end') |
| 455 |
self.assertEqual(self.entry.get(), '') |
| 456 |
|
| 457 |
self.entry.insert('end', 'a1b') |
| 458 |
self.assertEqual(self.entry.validate(), False) |
| 459 |
self.assertEqual(self.entry.state(), ('invalid', )) |
| 460 |
|
| 461 |
self.entry.delete(1) |
| 462 |
self.assertEqual(self.entry.validate(), True) |
| 463 |
self.assertEqual(self.entry.state(), ()) |
| 464 |
|
| 465 |
|
| 466 |
@add_configure_tests(IntegerSizeTests, StandardTtkOptionsTests) |
| 467 |
class ComboboxTest(EntryTest, unittest.TestCase): |
| 468 |
OPTIONS = ( |
| 469 |
'background', 'class', 'cursor', 'exportselection', |
| 470 |
'font', 'foreground', 'height', 'invalidcommand', |
| 471 |
'justify', 'placeholder', 'placeholderforeground', 'postcommand', |
| 472 |
'show', 'state', 'style', |
| 473 |
'takefocus', 'textvariable', |
| 474 |
'validate', 'validatecommand', 'values', |
| 475 |
'width', 'xscrollcommand', |
| 476 |
) |
| 477 |
IDENTIFY_AS = {'Combobox.button', 'textarea'} |
| 478 |
|
| 479 |
def setUp(self): |
| 480 |
super().setUp() |
| 481 |
self.combo = self.create() |
| 482 |
|
| 483 |
def create(self, **kwargs): |
| 484 |
return ttk.Combobox(self.root, **kwargs) |
| 485 |
|
| 486 |
def test_configure_height(self): |
| 487 |
widget = self.create() |
| 488 |
self.checkParams(widget, 'height', 100, 101.2, 102.6, -100, 0, '1i') |
| 489 |
|
| 490 |
def _show_drop_down_listbox(self): |
| 491 |
width = self.combo.winfo_width() |
| 492 |
x, y = width - 5, 5 |
| 493 |
if sys.platform != 'darwin': # there's no down arrow on macOS |
| 494 |
self.assertRegex(self.combo.identify(x, y), r'.*downarrow\z') |
| 495 |
self.combo.event_generate('<Button-1>', x=x, y=y) |
| 496 |
self.combo.event_generate('<ButtonRelease-1>', x=x, y=y) |
| 497 |
|
| 498 |
def test_virtual_event(self): |
| 499 |
if (tk_version >= (9, 0) and sys.platform == 'darwin' |
| 500 |
and isinstance(self.entry, ttk.Combobox)): |
| 501 |
self.skipTest('Test does not work on macOS Tk 9.') |
| 502 |
# https://core.tcl-lang.org/tk/tktview/8b49e9cfa6 |
| 503 |
success = [] |
| 504 |
|
| 505 |
self.combo['values'] = [1] |
| 506 |
self.combo.bind('<<ComboboxSelected>>', |
| 507 |
lambda evt: success.append(True)) |
| 508 |
self.combo.pack() |
| 509 |
self.combo.update() |
| 510 |
|
| 511 |
height = self.combo.winfo_height() |
| 512 |
self._show_drop_down_listbox() |
| 513 |
self.combo.update() |
| 514 |
self.combo.event_generate('<Return>') |
| 515 |
self.combo.update() |
| 516 |
|
| 517 |
self.assertTrue(success) |
| 518 |
|
| 519 |
def test_configure_postcommand(self): |
| 520 |
if (tk_version >= (9, 0) and sys.platform == 'darwin' |
| 521 |
and isinstance(self.entry, ttk.Combobox)): |
| 522 |
self.skipTest('Test does not work on macOS Tk 9.') |
| 523 |
# https://core.tcl-lang.org/tk/tktview/8b49e9cfa6 |
| 524 |
success = [] |
| 525 |
|
| 526 |
self.combo['postcommand'] = lambda: success.append(True) |
| 527 |
self.combo.pack() |
| 528 |
self.combo.update() |
| 529 |
|
| 530 |
self._show_drop_down_listbox() |
| 531 |
self.assertTrue(success) |
| 532 |
|
| 533 |
# testing postcommand removal |
| 534 |
self.combo['postcommand'] = '' |
| 535 |
self._show_drop_down_listbox() |
| 536 |
self.assertEqual(len(success), 1) |
| 537 |
|
| 538 |
def test_configure_values(self): |
| 539 |
def check_get_current(getval, currval): |
| 540 |
self.assertEqual(self.combo.get(), getval) |
| 541 |
self.assertEqual(self.combo.current(), currval) |
| 542 |
|
| 543 |
self.assertIn(self.combo['values'], ((), '')) |
| 544 |
check_get_current('', -1) |
| 545 |
|
| 546 |
self.checkParam(self.combo, 'values', 'mon tue wed thur', |
| 547 |
expected=('mon', 'tue', 'wed', 'thur')) |
| 548 |
self.checkParam(self.combo, 'values', ('mon', 'tue', 'wed', 'thur')) |
| 549 |
self.checkParam(self.combo, 'values', (42, 3.14, '', 'any string')) |
| 550 |
self.checkParam(self.combo, 'values', '') |
| 551 |
|
| 552 |
self.combo['values'] = ['a', 1, 'c'] |
| 553 |
|
| 554 |
self.combo.set('c') |
| 555 |
check_get_current('c', 2) |
| 556 |
|
| 557 |
self.combo.current(0) |
| 558 |
check_get_current('a', 0) |
| 559 |
|
| 560 |
self.combo.set('d') |
| 561 |
check_get_current('d', -1) |
| 562 |
|
| 563 |
# testing values with empty string |
| 564 |
self.combo.set('') |
| 565 |
self.combo['values'] = (1, 2, '', 3) |
| 566 |
check_get_current('', 2) |
| 567 |
|
| 568 |
# testing values with empty string set through configure |
| 569 |
self.combo.configure(values=[1, '', 2]) |
| 570 |
self.assertEqual(self.combo['values'], |
| 571 |
('1', '', '2') if self.wantobjects else |
| 572 |
'1 {} 2') |
| 573 |
|
| 574 |
# testing values with spaces |
| 575 |
self.combo['values'] = ['a b', 'a\tb', 'a\nb'] |
| 576 |
self.assertEqual(self.combo['values'], |
| 577 |
('a b', 'a\tb', 'a\nb') if self.wantobjects else |
| 578 |
'{a b} {a\tb} {a\nb}') |
| 579 |
|
| 580 |
# testing values with special characters |
| 581 |
self.combo['values'] = [r'a\tb', '"a"', '} {'] |
| 582 |
self.assertEqual(self.combo['values'], |
| 583 |
(r'a\tb', '"a"', '} {') if self.wantobjects else |
| 584 |
r'a\\tb {"a"} \}\ \{') |
| 585 |
|
| 586 |
# out of range |
| 587 |
self.assertRaises(tkinter.TclError, self.combo.current, |
| 588 |
len(self.combo['values'])) |
| 589 |
# it expects an integer (or something that can be converted to int) |
| 590 |
self.assertRaises(tkinter.TclError, self.combo.current, '') |
| 591 |
|
| 592 |
# testing creating combobox with empty string in values |
| 593 |
combo2 = ttk.Combobox(self.root, values=[1, 2, '']) |
| 594 |
self.assertEqual(combo2['values'], |
| 595 |
('1', '2', '') if self.wantobjects else '1 2 {}') |
| 596 |
combo2.destroy() |
| 597 |
|
| 598 |
|
| 599 |
@add_configure_tests(IntegerSizeTests, StandardTtkOptionsTests) |
| 600 |
class PanedWindowTest(AbstractWidgetTest, unittest.TestCase): |
| 601 |
OPTIONS = ( |
| 602 |
'class', 'cursor', 'height', |
| 603 |
'orient', 'style', 'takefocus', 'width', |
| 604 |
) |
| 605 |
_rounds_pixels = False |
| 606 |
_clipped = {} |
| 607 |
|
| 608 |
def setUp(self): |
| 609 |
super().setUp() |
| 610 |
self.paned = self.create() |
| 611 |
|
| 612 |
def create(self, **kwargs): |
| 613 |
return ttk.PanedWindow(self.root, **kwargs) |
| 614 |
|
| 615 |
def test_configure_orient(self): |
| 616 |
widget = self.create() |
| 617 |
self.assertEqual(str(widget['orient']), 'vertical') |
| 618 |
errmsg='attempt to change read-only option' |
| 619 |
if get_tk_patchlevel(self.root) < (8, 6, 0, 'beta', 3): |
| 620 |
errmsg='Attempt to change read-only option' |
| 621 |
self.checkInvalidParam(widget, 'orient', 'horizontal', |
| 622 |
errmsg=errmsg) |
| 623 |
widget2 = self.create(orient='horizontal') |
| 624 |
self.assertEqual(str(widget2['orient']), 'horizontal') |
| 625 |
|
| 626 |
def test_add(self): |
| 627 |
# attempt to add a child that is not a direct child of the paned window |
| 628 |
label = ttk.Label(self.paned) |
| 629 |
child = ttk.Label(label) |
| 630 |
self.assertRaises(tkinter.TclError, self.paned.add, child) |
| 631 |
label.destroy() |
| 632 |
child.destroy() |
| 633 |
# another attempt |
| 634 |
label = ttk.Label(self.root) |
| 635 |
child = ttk.Label(label) |
| 636 |
self.assertRaises(tkinter.TclError, self.paned.add, child) |
| 637 |
child.destroy() |
| 638 |
label.destroy() |
| 639 |
|
| 640 |
good_child = ttk.Label(self.root) |
| 641 |
self.paned.add(good_child) |
| 642 |
# re-adding a child is not accepted |
| 643 |
self.assertRaises(tkinter.TclError, self.paned.add, good_child) |
| 644 |
|
| 645 |
other_child = ttk.Label(self.paned) |
| 646 |
self.paned.add(other_child) |
| 647 |
self.assertEqual(self.paned.pane(0), self.paned.pane(1)) |
| 648 |
self.assertRaises(tkinter.TclError, self.paned.pane, 2) |
| 649 |
good_child.destroy() |
| 650 |
other_child.destroy() |
| 651 |
self.assertRaises(tkinter.TclError, self.paned.pane, 0) |
| 652 |
|
| 653 |
def test_forget(self): |
| 654 |
self.assertRaises(tkinter.TclError, self.paned.forget, None) |
| 655 |
self.assertRaises(tkinter.TclError, self.paned.forget, 0) |
| 656 |
|
| 657 |
self.paned.add(ttk.Label(self.root)) |
| 658 |
self.paned.forget(0) |
| 659 |
self.assertRaises(tkinter.TclError, self.paned.forget, 0) |
| 660 |
|
| 661 |
def test_insert(self): |
| 662 |
self.assertRaises(tkinter.TclError, self.paned.insert, None, 0) |
| 663 |
self.assertRaises(tkinter.TclError, self.paned.insert, 0, None) |
| 664 |
self.assertRaises(tkinter.TclError, self.paned.insert, 0, 0) |
| 665 |
|
| 666 |
child = ttk.Label(self.root) |
| 667 |
child2 = ttk.Label(self.root) |
| 668 |
child3 = ttk.Label(self.root) |
| 669 |
|
| 670 |
if tk_version >= (8, 7): |
| 671 |
self.paned.insert(0, child) |
| 672 |
self.assertEqual(self.paned.panes(), (str(child),)) |
| 673 |
self.paned.forget(0) |
| 674 |
else: |
| 675 |
self.assertRaises(tkinter.TclError, self.paned.insert, 0, child) |
| 676 |
|
| 677 |
self.assertEqual(self.paned.panes(), ()) |
| 678 |
self.paned.insert('end', child2) |
| 679 |
self.paned.insert(0, child) |
| 680 |
self.assertEqual(self.paned.panes(), (str(child), str(child2))) |
| 681 |
|
| 682 |
self.paned.insert(0, child2) |
| 683 |
self.assertEqual(self.paned.panes(), (str(child2), str(child))) |
| 684 |
|
| 685 |
self.paned.insert('end', child3) |
| 686 |
self.assertEqual(self.paned.panes(), |
| 687 |
(str(child2), str(child), str(child3))) |
| 688 |
|
| 689 |
# reinserting a child should move it to its current position |
| 690 |
panes = self.paned.panes() |
| 691 |
self.paned.insert('end', child3) |
| 692 |
self.assertEqual(panes, self.paned.panes()) |
| 693 |
|
| 694 |
# moving child3 to child2 position should result in child2 ending up |
| 695 |
# in previous child position and child ending up in previous child3 |
| 696 |
# position |
| 697 |
self.paned.insert(child2, child3) |
| 698 |
self.assertEqual(self.paned.panes(), |
| 699 |
(str(child3), str(child2), str(child))) |
| 700 |
|
| 701 |
def test_pane(self): |
| 702 |
self.assertRaises(tkinter.TclError, self.paned.pane, 0) |
| 703 |
|
| 704 |
child = ttk.Label(self.root) |
| 705 |
self.paned.add(child) |
| 706 |
self.assertIsInstance(self.paned.pane(0), dict) |
| 707 |
self.assertEqual(self.paned.pane(0, weight=None), |
| 708 |
0 if self.wantobjects else '0') |
| 709 |
# newer form for querying a single option |
| 710 |
self.assertEqual(self.paned.pane(0, 'weight'), |
| 711 |
0 if self.wantobjects else '0') |
| 712 |
self.assertEqual(self.paned.pane(0), self.paned.pane(str(child))) |
| 713 |
|
| 714 |
self.assertRaises(tkinter.TclError, self.paned.pane, 0, |
| 715 |
badoption='somevalue') |
| 716 |
|
| 717 |
def test_sashpos(self): |
| 718 |
self.assertRaises(tkinter.TclError, self.paned.sashpos, None) |
| 719 |
self.assertRaises(tkinter.TclError, self.paned.sashpos, '') |
| 720 |
self.assertRaises(tkinter.TclError, self.paned.sashpos, 0) |
| 721 |
|
| 722 |
child = ttk.Label(self.paned, text='a') |
| 723 |
self.paned.add(child, weight=1) |
| 724 |
self.assertRaises(tkinter.TclError, self.paned.sashpos, 0) |
| 725 |
child2 = ttk.Label(self.paned, text='b') |
| 726 |
self.paned.add(child2) |
| 727 |
self.assertRaises(tkinter.TclError, self.paned.sashpos, 1) |
| 728 |
|
| 729 |
self.paned.pack(expand=True, fill='both') |
| 730 |
|
| 731 |
curr_pos = self.paned.sashpos(0) |
| 732 |
self.paned.sashpos(0, 1000) |
| 733 |
self.assertNotEqual(curr_pos, self.paned.sashpos(0)) |
| 734 |
self.assertIsInstance(self.paned.sashpos(0), int) |
| 735 |
|
| 736 |
|
| 737 |
@add_configure_tests(StandardTtkOptionsTests) |
| 738 |
class RadiobuttonTest(AbstractLabelTest, unittest.TestCase): |
| 739 |
OPTIONS = ( |
| 740 |
'class', 'command', 'compound', 'cursor', |
| 741 |
'image', 'justify', |
| 742 |
'padding', 'state', 'style', |
| 743 |
'takefocus', 'text', 'textvariable', |
| 744 |
'underline', 'value', 'variable', 'width', |
| 745 |
) |
| 746 |
|
| 747 |
def create(self, **kwargs): |
| 748 |
return ttk.Radiobutton(self.root, **kwargs) |
| 749 |
|
| 750 |
def test_configure_value(self): |
| 751 |
widget = self.create() |
| 752 |
self.checkParams(widget, 'value', 1, 2.3, '', 'any string') |
| 753 |
|
| 754 |
def test_configure_invoke(self): |
| 755 |
success = [] |
| 756 |
def cb_test(): |
| 757 |
success.append(1) |
| 758 |
return "cb test called" |
| 759 |
|
| 760 |
myvar = tkinter.IntVar(self.root) |
| 761 |
cbtn = ttk.Radiobutton(self.root, command=cb_test, |
| 762 |
variable=myvar, value=0) |
| 763 |
cbtn2 = ttk.Radiobutton(self.root, command=cb_test, |
| 764 |
variable=myvar, value=1) |
| 765 |
|
| 766 |
if self.wantobjects: |
| 767 |
conv = lambda x: x |
| 768 |
else: |
| 769 |
conv = int |
| 770 |
|
| 771 |
res = cbtn.invoke() |
| 772 |
self.assertEqual(res, "cb test called") |
| 773 |
self.assertEqual(conv(cbtn['value']), myvar.get()) |
| 774 |
self.assertEqual(myvar.get(), |
| 775 |
conv(cbtn.tk.globalgetvar(cbtn['variable']))) |
| 776 |
self.assertTrue(success) |
| 777 |
|
| 778 |
cbtn2['command'] = '' |
| 779 |
res = cbtn2.invoke() |
| 780 |
if tk_version >= (8, 7) and self.wantobjects: |
| 781 |
self.assertEqual(res, ()) |
| 782 |
else: |
| 783 |
self.assertEqual(str(res), '') |
| 784 |
self.assertLessEqual(len(success), 1) |
| 785 |
self.assertEqual(conv(cbtn2['value']), myvar.get()) |
| 786 |
self.assertEqual(myvar.get(), |
| 787 |
conv(cbtn.tk.globalgetvar(cbtn['variable']))) |
| 788 |
|
| 789 |
self.assertEqual(str(cbtn['variable']), str(cbtn2['variable'])) |
| 790 |
|
| 791 |
|
| 792 |
class MenubuttonTest(AbstractLabelTest, unittest.TestCase): |
| 793 |
OPTIONS = ( |
| 794 |
'class', 'compound', 'cursor', 'direction', |
| 795 |
'image', 'justify', 'menu', 'padding', 'state', 'style', |
| 796 |
'takefocus', 'text', 'textvariable', |
| 797 |
'underline', 'width', |
| 798 |
) |
| 799 |
|
| 800 |
def create(self, **kwargs): |
| 801 |
return ttk.Menubutton(self.root, **kwargs) |
| 802 |
|
| 803 |
def test_configure_direction(self): |
| 804 |
widget = self.create() |
| 805 |
values = ('above', 'below', 'left', 'right', 'flush') |
| 806 |
self.checkEnumParam(widget, 'direction', *values, |
| 807 |
sort=tk_version >= (8, 7)) |
| 808 |
|
| 809 |
def test_configure_menu(self): |
| 810 |
widget = self.create() |
| 811 |
menu = tkinter.Menu(widget, name='menu') |
| 812 |
self.checkParam(widget, 'menu', menu, conv=str) |
| 813 |
menu.destroy() |
| 814 |
|
| 815 |
|
| 816 |
@add_configure_tests(StandardTtkOptionsTests) |
| 817 |
class ScaleTest(AbstractWidgetTest, unittest.TestCase): |
| 818 |
OPTIONS = ( |
| 819 |
'class', 'command', 'cursor', 'from', 'length', |
| 820 |
'orient', 'state', 'style', 'takefocus', 'to', 'value', 'variable', |
| 821 |
) |
| 822 |
_rounds_pixels = False |
| 823 |
_clipped = {} |
| 824 |
default_orient = 'horizontal' |
| 825 |
|
| 826 |
def setUp(self): |
| 827 |
super().setUp() |
| 828 |
self.scale = self.create() |
| 829 |
self.scale.pack() |
| 830 |
self.scale.update() |
| 831 |
|
| 832 |
def create(self, **kwargs): |
| 833 |
return ttk.Scale(self.root, **kwargs) |
| 834 |
|
| 835 |
def test_configure_from(self): |
| 836 |
widget = self.create() |
| 837 |
self.checkFloatParam(widget, 'from', 100, 14.9, 15.1, conv=False) |
| 838 |
|
| 839 |
def test_configure_length(self): |
| 840 |
widget = self.create() |
| 841 |
self.checkPixelsParam(widget, 'length', 130, 131.2, 135.6, '5i') |
| 842 |
|
| 843 |
test_configure_state = requires_tk(8, 6, 9)(StandardTtkOptionsTests.test_configure_state) |
| 844 |
|
| 845 |
def test_configure_to(self): |
| 846 |
widget = self.create() |
| 847 |
self.checkFloatParam(widget, 'to', 300, 14.9, 15.1, -10, conv=False) |
| 848 |
|
| 849 |
def test_configure_value(self): |
| 850 |
widget = self.create() |
| 851 |
self.checkFloatParam(widget, 'value', 300, 14.9, 15.1, -10, conv=False) |
| 852 |
|
| 853 |
def test_custom_event(self): |
| 854 |
failure = [1, 1, 1] # will need to be empty |
| 855 |
|
| 856 |
funcid = self.scale.bind('<<RangeChanged>>', lambda evt: failure.pop()) |
| 857 |
|
| 858 |
self.scale['from'] = 10 |
| 859 |
self.scale['from_'] = 10 |
| 860 |
self.scale['to'] = 3 |
| 861 |
|
| 862 |
self.assertFalse(failure) |
| 863 |
|
| 864 |
failure = [1, 1, 1] |
| 865 |
self.scale.configure(from_=2, to=5) |
| 866 |
self.scale.configure(from_=0, to=-2) |
| 867 |
self.scale.configure(to=10) |
| 868 |
|
| 869 |
self.assertFalse(failure) |
| 870 |
|
| 871 |
def test_get(self): |
| 872 |
if self.wantobjects: |
| 873 |
conv = lambda x: x |
| 874 |
else: |
| 875 |
conv = float |
| 876 |
|
| 877 |
scale_width = self.scale.winfo_width() |
| 878 |
self.assertEqual(self.scale.get(scale_width, 0), self.scale['to']) |
| 879 |
|
| 880 |
self.assertEqual(conv(self.scale.get(0, 0)), conv(self.scale['from'])) |
| 881 |
self.assertEqual(self.scale.get(), self.scale['value']) |
| 882 |
self.scale['value'] = 30 |
| 883 |
self.assertEqual(self.scale.get(), self.scale['value']) |
| 884 |
|
| 885 |
self.assertRaises(tkinter.TclError, self.scale.get, '', 0) |
| 886 |
self.assertRaises(tkinter.TclError, self.scale.get, 0, '') |
| 887 |
|
| 888 |
def test_set(self): |
| 889 |
if self.wantobjects: |
| 890 |
conv = lambda x: x |
| 891 |
else: |
| 892 |
conv = float |
| 893 |
|
| 894 |
# set restricts the max/min values according to the current range |
| 895 |
max = conv(self.scale['to']) |
| 896 |
new_max = max + 10 |
| 897 |
self.scale.set(new_max) |
| 898 |
self.assertEqual(conv(self.scale.get()), max) |
| 899 |
min = conv(self.scale['from']) |
| 900 |
self.scale.set(min - 1) |
| 901 |
self.assertEqual(conv(self.scale.get()), min) |
| 902 |
|
| 903 |
# changing directly the variable doesn't impose this limitation tho |
| 904 |
var = tkinter.DoubleVar(self.root) |
| 905 |
self.scale['variable'] = var |
| 906 |
var.set(max + 5) |
| 907 |
self.assertEqual(conv(self.scale.get()), var.get()) |
| 908 |
self.assertEqual(conv(self.scale.get()), max + 5) |
| 909 |
del var |
| 910 |
gc_collect() # For PyPy or other GCs. |
| 911 |
|
| 912 |
# the same happens with the value option |
| 913 |
self.scale['value'] = max + 10 |
| 914 |
self.assertEqual(conv(self.scale.get()), max + 10) |
| 915 |
self.assertEqual(conv(self.scale.get()), conv(self.scale['value'])) |
| 916 |
|
| 917 |
# nevertheless, note that the max/min values we can get specifying |
| 918 |
# x, y coords are the ones according to the current range |
| 919 |
self.assertEqual(conv(self.scale.get(0, 0)), min) |
| 920 |
self.assertEqual(conv(self.scale.get(self.scale.winfo_width(), 0)), max) |
| 921 |
|
| 922 |
self.assertRaises(tkinter.TclError, self.scale.set, None) |
| 923 |
|
| 924 |
|
| 925 |
@add_configure_tests(StandardTtkOptionsTests) |
| 926 |
class ProgressbarTest(AbstractWidgetTest, unittest.TestCase): |
| 927 |
OPTIONS = ( |
| 928 |
'anchor', 'class', 'cursor', 'font', 'foreground', 'justify', |
| 929 |
'orient', 'length', |
| 930 |
'mode', 'maximum', 'phase', 'text', 'wraplength', |
| 931 |
'style', 'takefocus', 'value', 'variable', |
| 932 |
) |
| 933 |
_rounds_pixels = False |
| 934 |
_clipped = {} |
| 935 |
_allow_empty_justify = True |
| 936 |
default_orient = 'horizontal' |
| 937 |
|
| 938 |
def create(self, **kwargs): |
| 939 |
return ttk.Progressbar(self.root, **kwargs) |
| 940 |
|
| 941 |
@requires_tk(8, 7) |
| 942 |
def test_configure_anchor(self): |
| 943 |
widget = self.create() |
| 944 |
self.checkEnumParam(widget, 'anchor', |
| 945 |
'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'center', '') |
| 946 |
|
| 947 |
test_configure_font = requires_tk(8, 7)(StandardOptionsTests.test_configure_font) |
| 948 |
test_configure_foreground = requires_tk(8, 7)(StandardOptionsTests.test_configure_foreground) |
| 949 |
test_configure_justify = requires_tk(8, 7)(StandardTtkOptionsTests.test_configure_justify) |
| 950 |
|
| 951 |
def test_configure_length(self): |
| 952 |
widget = self.create() |
| 953 |
self.checkPixelsParam(widget, 'length', 100.1, 56.7, '2i') |
| 954 |
|
| 955 |
def test_configure_maximum(self): |
| 956 |
widget = self.create() |
| 957 |
self.checkFloatParam(widget, 'maximum', 150.2, 77.7, 0, -10, conv=False) |
| 958 |
|
| 959 |
def test_configure_mode(self): |
| 960 |
widget = self.create() |
| 961 |
self.checkEnumParam(widget, 'mode', 'determinate', 'indeterminate') |
| 962 |
|
| 963 |
def test_configure_phase(self): |
| 964 |
# XXX |
| 965 |
pass |
| 966 |
|
| 967 |
test_configure_text = requires_tk(8, 7)(StandardOptionsTests.test_configure_text) |
| 968 |
|
| 969 |
def test_configure_value(self): |
| 970 |
widget = self.create() |
| 971 |
self.checkFloatParam(widget, 'value', 150.2, 77.7, 0, -10, |
| 972 |
conv=False) |
| 973 |
|
| 974 |
test_configure_wraplength = requires_tk(8, 7)(StandardOptionsTests.test_configure_wraplength) |
| 975 |
|
| 976 |
|
| 977 |
@unittest.skipIf(sys.platform == 'darwin', |
| 978 |
'ttk.Scrollbar is special on MacOSX') |
| 979 |
@add_configure_tests(StandardTtkOptionsTests) |
| 980 |
class ScrollbarTest(AbstractWidgetTest, unittest.TestCase): |
| 981 |
OPTIONS = ( |
| 982 |
'class', 'command', 'cursor', 'orient', 'style', 'takefocus', |
| 983 |
) |
| 984 |
_rounds_pixels = False |
| 985 |
_clipped = {} |
| 986 |
default_orient = 'vertical' |
| 987 |
|
| 988 |
def create(self, **kwargs): |
| 989 |
return ttk.Scrollbar(self.root, **kwargs) |
| 990 |
|
| 991 |
|
| 992 |
@add_configure_tests(StandardTtkOptionsTests) |
| 993 |
class NotebookTest(AbstractWidgetTest, unittest.TestCase): |
| 994 |
OPTIONS = ( |
| 995 |
'class', 'cursor', 'height', 'padding', 'style', 'takefocus', 'width', |
| 996 |
) |
| 997 |
_rounds_pixels = (tk_version < (9,0)) |
| 998 |
_converts_pixels = False |
| 999 |
_clipped = {} |
| 1000 |
|
| 1001 |
def setUp(self): |
| 1002 |
super().setUp() |
| 1003 |
self.nb = self.create(padding=0) |
| 1004 |
self.child1 = ttk.Label(self.root) |
| 1005 |
self.child2 = ttk.Label(self.root) |
| 1006 |
self.nb.add(self.child1, text='a') |
| 1007 |
self.nb.add(self.child2, text='b') |
| 1008 |
|
| 1009 |
def create(self, **kwargs): |
| 1010 |
return ttk.Notebook(self.root, **kwargs) |
| 1011 |
|
| 1012 |
def test_configure_height(self): |
| 1013 |
widget = self.create() |
| 1014 |
if get_tk_patchlevel(self.root) < (8, 6, 15): |
| 1015 |
self.checkIntegerParam(widget, 'height', 402, -402, 0) |
| 1016 |
else: |
| 1017 |
self.checkPixelsParam(widget, 'height', '10c', 402, -402, 0) |
| 1018 |
|
| 1019 |
def test_configure_width(self): |
| 1020 |
widget = self.create() |
| 1021 |
if get_tk_patchlevel(self.root) < (8, 6, 15): |
| 1022 |
self.checkIntegerParam(widget, 'width', 402, -402, 0) |
| 1023 |
else: |
| 1024 |
self.checkPixelsParam(widget, 'width', '10c', 402, -402, 0) |
| 1025 |
|
| 1026 |
def test_tab_identifiers(self): |
| 1027 |
self.nb.forget(0) |
| 1028 |
self.nb.hide(self.child2) |
| 1029 |
self.assertRaises(tkinter.TclError, self.nb.tab, self.child1) |
| 1030 |
self.assertEqual(self.nb.index('end'), 1) |
| 1031 |
self.nb.add(self.child2) |
| 1032 |
self.assertEqual(self.nb.index('end'), 1) |
| 1033 |
self.nb.select(self.child2) |
| 1034 |
|
| 1035 |
self.assertTrue(self.nb.tab('current')) |
| 1036 |
self.nb.add(self.child1, text='a') |
| 1037 |
|
| 1038 |
self.nb.pack() |
| 1039 |
self.nb.update() |
| 1040 |
if sys.platform == 'darwin': |
| 1041 |
tb_idx = "@20,5" |
| 1042 |
else: |
| 1043 |
tb_idx = "@5,5" |
| 1044 |
self.assertEqual(self.nb.tab(tb_idx), self.nb.tab('current')) |
| 1045 |
|
| 1046 |
for i in range(5, 100, 5): |
| 1047 |
try: |
| 1048 |
if self.nb.tab('@%d, 5' % i, text=None) == 'a': |
| 1049 |
break |
| 1050 |
except tkinter.TclError: |
| 1051 |
pass |
| 1052 |
|
| 1053 |
else: |
| 1054 |
self.fail("Tab with text 'a' not found") |
| 1055 |
|
| 1056 |
def test_add_and_hidden(self): |
| 1057 |
self.assertRaises(tkinter.TclError, self.nb.hide, -1) |
| 1058 |
self.assertRaises(tkinter.TclError, self.nb.hide, 'hi') |
| 1059 |
self.assertRaises(tkinter.TclError, self.nb.hide, None) |
| 1060 |
self.assertRaises(tkinter.TclError, self.nb.add, None) |
| 1061 |
self.assertRaises(tkinter.TclError, self.nb.add, ttk.Label(self.root), |
| 1062 |
unknown='option') |
| 1063 |
|
| 1064 |
tabs = self.nb.tabs() |
| 1065 |
self.nb.hide(self.child1) |
| 1066 |
self.nb.add(self.child1) |
| 1067 |
self.assertEqual(self.nb.tabs(), tabs) |
| 1068 |
|
| 1069 |
child = ttk.Label(self.root) |
| 1070 |
self.nb.add(child, text='c') |
| 1071 |
tabs = self.nb.tabs() |
| 1072 |
|
| 1073 |
curr = self.nb.index('current') |
| 1074 |
# verify that the tab gets re-added at its previous position |
| 1075 |
child2_index = self.nb.index(self.child2) |
| 1076 |
self.nb.hide(self.child2) |
| 1077 |
self.nb.add(self.child2) |
| 1078 |
self.assertEqual(self.nb.tabs(), tabs) |
| 1079 |
self.assertEqual(self.nb.index(self.child2), child2_index) |
| 1080 |
self.assertEqual(str(self.child2), self.nb.tabs()[child2_index]) |
| 1081 |
# but the tab next to it (not hidden) is the one selected now |
| 1082 |
self.assertEqual(self.nb.index('current'), curr + 1) |
| 1083 |
|
| 1084 |
def test_forget(self): |
| 1085 |
self.assertRaises(tkinter.TclError, self.nb.forget, -1) |
| 1086 |
self.assertRaises(tkinter.TclError, self.nb.forget, 'hi') |
| 1087 |
self.assertRaises(tkinter.TclError, self.nb.forget, None) |
| 1088 |
|
| 1089 |
tabs = self.nb.tabs() |
| 1090 |
child1_index = self.nb.index(self.child1) |
| 1091 |
self.nb.forget(self.child1) |
| 1092 |
self.assertNotIn(str(self.child1), self.nb.tabs()) |
| 1093 |
self.assertEqual(len(tabs) - 1, len(self.nb.tabs())) |
| 1094 |
|
| 1095 |
self.nb.add(self.child1) |
| 1096 |
self.assertEqual(self.nb.index(self.child1), 1) |
| 1097 |
self.assertNotEqual(child1_index, self.nb.index(self.child1)) |
| 1098 |
|
| 1099 |
def test_index(self): |
| 1100 |
self.assertRaises(tkinter.TclError, self.nb.index, -1) |
| 1101 |
self.assertRaises(tkinter.TclError, self.nb.index, None) |
| 1102 |
|
| 1103 |
self.assertIsInstance(self.nb.index('end'), int) |
| 1104 |
self.assertEqual(self.nb.index(self.child1), 0) |
| 1105 |
self.assertEqual(self.nb.index(self.child2), 1) |
| 1106 |
self.assertEqual(self.nb.index('end'), 2) |
| 1107 |
|
| 1108 |
def test_insert(self): |
| 1109 |
# moving tabs |
| 1110 |
tabs = self.nb.tabs() |
| 1111 |
self.nb.insert(1, tabs[0]) |
| 1112 |
self.assertEqual(self.nb.tabs(), (tabs[1], tabs[0])) |
| 1113 |
self.nb.insert(self.child1, self.child2) |
| 1114 |
self.assertEqual(self.nb.tabs(), tabs) |
| 1115 |
self.nb.insert('end', self.child1) |
| 1116 |
self.assertEqual(self.nb.tabs(), (tabs[1], tabs[0])) |
| 1117 |
self.nb.insert('end', 0) |
| 1118 |
self.assertEqual(self.nb.tabs(), tabs) |
| 1119 |
# bad moves |
| 1120 |
self.assertRaises(tkinter.TclError, self.nb.insert, 2, tabs[0]) |
| 1121 |
self.assertRaises(tkinter.TclError, self.nb.insert, -1, tabs[0]) |
| 1122 |
|
| 1123 |
# new tab |
| 1124 |
child3 = ttk.Label(self.root) |
| 1125 |
self.nb.insert(1, child3) |
| 1126 |
self.assertEqual(self.nb.tabs(), (tabs[0], str(child3), tabs[1])) |
| 1127 |
self.nb.forget(child3) |
| 1128 |
self.assertEqual(self.nb.tabs(), tabs) |
| 1129 |
self.nb.insert(self.child1, child3) |
| 1130 |
self.assertEqual(self.nb.tabs(), (str(child3), ) + tabs) |
| 1131 |
self.nb.forget(child3) |
| 1132 |
if tk_version >= (8, 7): |
| 1133 |
self.nb.insert(2, child3) |
| 1134 |
self.assertEqual(self.nb.tabs(), (*tabs, str(child3))) |
| 1135 |
else: |
| 1136 |
self.assertRaises(tkinter.TclError, self.nb.insert, 2, child3) |
| 1137 |
self.assertRaises(tkinter.TclError, self.nb.insert, -1, child3) |
| 1138 |
|
| 1139 |
# bad inserts |
| 1140 |
self.assertRaises(tkinter.TclError, self.nb.insert, 'end', None) |
| 1141 |
self.assertRaises(tkinter.TclError, self.nb.insert, None, 0) |
| 1142 |
self.assertRaises(tkinter.TclError, self.nb.insert, None, None) |
| 1143 |
|
| 1144 |
def test_select(self): |
| 1145 |
self.nb.pack() |
| 1146 |
self.nb.update() |
| 1147 |
|
| 1148 |
success = [] |
| 1149 |
tab_changed = [] |
| 1150 |
|
| 1151 |
self.child1.bind('<Unmap>', lambda evt: success.append(True)) |
| 1152 |
self.nb.bind('<<NotebookTabChanged>>', |
| 1153 |
lambda evt: tab_changed.append(True)) |
| 1154 |
|
| 1155 |
self.assertEqual(self.nb.select(), str(self.child1)) |
| 1156 |
self.nb.select(self.child2) |
| 1157 |
self.assertTrue(success) |
| 1158 |
self.assertEqual(self.nb.select(), str(self.child2)) |
| 1159 |
|
| 1160 |
self.nb.update() |
| 1161 |
self.assertTrue(tab_changed) |
| 1162 |
|
| 1163 |
def test_tab(self): |
| 1164 |
self.assertRaises(tkinter.TclError, self.nb.tab, -1) |
| 1165 |
self.assertRaises(tkinter.TclError, self.nb.tab, 'notab') |
| 1166 |
self.assertRaises(tkinter.TclError, self.nb.tab, None) |
| 1167 |
|
| 1168 |
self.assertIsInstance(self.nb.tab(self.child1), dict) |
| 1169 |
self.assertEqual(self.nb.tab(self.child1, text=None), 'a') |
| 1170 |
# newer form for querying a single option |
| 1171 |
self.assertEqual(self.nb.tab(self.child1, 'text'), 'a') |
| 1172 |
self.nb.tab(self.child1, text='abc') |
| 1173 |
self.assertEqual(self.nb.tab(self.child1, text=None), 'abc') |
| 1174 |
self.assertEqual(self.nb.tab(self.child1, 'text'), 'abc') |
| 1175 |
|
| 1176 |
def test_configure_tabs(self): |
| 1177 |
self.assertEqual(len(self.nb.tabs()), 2) |
| 1178 |
|
| 1179 |
self.nb.forget(self.child1) |
| 1180 |
self.nb.forget(self.child2) |
| 1181 |
|
| 1182 |
self.assertEqual(self.nb.tabs(), ()) |
| 1183 |
|
| 1184 |
def test_traversal(self): |
| 1185 |
self.nb.pack() |
| 1186 |
self.nb.update() |
| 1187 |
|
| 1188 |
self.nb.select(0) |
| 1189 |
|
| 1190 |
if sys.platform == 'darwin': |
| 1191 |
focus_identify_as = '' |
| 1192 |
elif sys.platform == 'win32': |
| 1193 |
focus_identify_as = 'focus' |
| 1194 |
else: |
| 1195 |
focus_identify_as = 'focus' if tk_version < (8, 7) else 'padding' |
| 1196 |
self.assertEqual(self.nb.identify(5, 5), focus_identify_as) |
| 1197 |
simulate_mouse_click(self.nb, 5, 5) |
| 1198 |
self.nb.focus_force() |
| 1199 |
self.nb.event_generate('<Control-Tab>') |
| 1200 |
self.assertEqual(self.nb.select(), str(self.child2)) |
| 1201 |
self.nb.focus_force() |
| 1202 |
self.nb.event_generate('<Shift-Control-Tab>') |
| 1203 |
self.assertEqual(self.nb.select(), str(self.child1)) |
| 1204 |
self.nb.focus_force() |
| 1205 |
self.nb.event_generate('<Shift-Control-Tab>') |
| 1206 |
self.assertEqual(self.nb.select(), str(self.child2)) |
| 1207 |
|
| 1208 |
self.nb.tab(self.child1, text='a', underline=0) |
| 1209 |
self.nb.tab(self.child2, text='e', underline=0) |
| 1210 |
self.nb.enable_traversal() |
| 1211 |
self.nb.focus_force() |
| 1212 |
self.assertEqual(self.nb.identify(5, 5), focus_identify_as) |
| 1213 |
simulate_mouse_click(self.nb, 5, 5) |
| 1214 |
# on macOS Emacs-style keyboard shortcuts are region-dependent; |
| 1215 |
# let's use the regular arrow keys instead |
| 1216 |
if sys.platform == 'darwin': |
| 1217 |
begin = '<Left>' |
| 1218 |
end = '<Right>' |
| 1219 |
else: |
| 1220 |
begin = '<Alt-a>' |
| 1221 |
end = '<Alt-e>' |
| 1222 |
self.nb.event_generate(begin) |
| 1223 |
self.assertEqual(self.nb.select(), str(self.child1)) |
| 1224 |
self.nb.event_generate(end) |
| 1225 |
self.assertEqual(self.nb.select(), str(self.child2)) |
| 1226 |
|
| 1227 |
|
| 1228 |
@add_configure_tests(IntegerSizeTests, StandardTtkOptionsTests) |
| 1229 |
class SpinboxTest(EntryTest, unittest.TestCase): |
| 1230 |
OPTIONS = ( |
| 1231 |
'background', 'class', 'command', 'cursor', 'exportselection', |
| 1232 |
'font', 'foreground', 'format', 'from', 'increment', |
| 1233 |
'invalidcommand', 'justify', |
| 1234 |
'placeholder', 'placeholderforeground', |
| 1235 |
'show', 'state', 'style', |
| 1236 |
'takefocus', 'textvariable', 'to', 'validate', 'validatecommand', |
| 1237 |
'values', 'width', 'wrap', 'xscrollcommand', |
| 1238 |
) |
| 1239 |
IDENTIFY_AS = {'Spinbox.field', 'textarea'} |
| 1240 |
|
| 1241 |
def setUp(self): |
| 1242 |
super().setUp() |
| 1243 |
self.spin = self.create() |
| 1244 |
self.spin.pack() |
| 1245 |
|
| 1246 |
def create(self, **kwargs): |
| 1247 |
return ttk.Spinbox(self.root, **kwargs) |
| 1248 |
|
| 1249 |
def _click_increment_arrow(self): |
| 1250 |
width = self.spin.winfo_width() |
| 1251 |
height = self.spin.winfo_height() |
| 1252 |
x = width - 5 |
| 1253 |
y = height//2 - 5 |
| 1254 |
self.assertRegex(self.spin.identify(x, y), r'.*uparrow\z') |
| 1255 |
self.spin.event_generate('<ButtonPress-1>', x=x, y=y) |
| 1256 |
self.spin.event_generate('<ButtonRelease-1>', x=x, y=y) |
| 1257 |
self.spin.update_idletasks() |
| 1258 |
|
| 1259 |
def _click_decrement_arrow(self): |
| 1260 |
width = self.spin.winfo_width() |
| 1261 |
height = self.spin.winfo_height() |
| 1262 |
x = width - 5 |
| 1263 |
y = height//2 + 4 |
| 1264 |
self.assertRegex(self.spin.identify(x, y), r'.*downarrow\z') |
| 1265 |
self.spin.event_generate('<ButtonPress-1>', x=x, y=y) |
| 1266 |
self.spin.event_generate('<ButtonRelease-1>', x=x, y=y) |
| 1267 |
self.spin.update_idletasks() |
| 1268 |
|
| 1269 |
def test_configure_command(self): |
| 1270 |
success = [] |
| 1271 |
|
| 1272 |
self.spin['command'] = lambda: success.append(True) |
| 1273 |
self.spin.update() |
| 1274 |
self._click_increment_arrow() |
| 1275 |
self.spin.update() |
| 1276 |
self.assertTrue(success) |
| 1277 |
|
| 1278 |
self._click_decrement_arrow() |
| 1279 |
self.assertEqual(len(success), 2) |
| 1280 |
|
| 1281 |
# testing postcommand removal |
| 1282 |
self.spin['command'] = '' |
| 1283 |
self.spin.update_idletasks() |
| 1284 |
self._click_increment_arrow() |
| 1285 |
self._click_decrement_arrow() |
| 1286 |
self.spin.update() |
| 1287 |
self.assertEqual(len(success), 2) |
| 1288 |
|
| 1289 |
def test_configure_to(self): |
| 1290 |
self.spin['from'] = 0 |
| 1291 |
self.spin['to'] = 5 |
| 1292 |
self.spin.set(4) |
| 1293 |
self.spin.update() |
| 1294 |
self._click_increment_arrow() # 5 |
| 1295 |
|
| 1296 |
self.assertEqual(self.spin.get(), '5') |
| 1297 |
|
| 1298 |
self._click_increment_arrow() # 5 |
| 1299 |
self.assertEqual(self.spin.get(), '5') |
| 1300 |
|
| 1301 |
def test_configure_from(self): |
| 1302 |
self.spin['from'] = 1 |
| 1303 |
self.spin['to'] = 10 |
| 1304 |
self.spin.set(2) |
| 1305 |
self.spin.update() |
| 1306 |
self._click_decrement_arrow() # 1 |
| 1307 |
self.assertEqual(self.spin.get(), '1') |
| 1308 |
self._click_decrement_arrow() # 1 |
| 1309 |
self.assertEqual(self.spin.get(), '1') |
| 1310 |
|
| 1311 |
def test_configure_increment(self): |
| 1312 |
self.spin['from'] = 0 |
| 1313 |
self.spin['to'] = 10 |
| 1314 |
self.spin['increment'] = 4 |
| 1315 |
self.spin.set(1) |
| 1316 |
self.spin.update() |
| 1317 |
|
| 1318 |
self._click_increment_arrow() # 5 |
| 1319 |
self.assertEqual(self.spin.get(), '5') |
| 1320 |
self.spin['increment'] = 2 |
| 1321 |
self.spin.update() |
| 1322 |
self._click_decrement_arrow() # 3 |
| 1323 |
self.assertEqual(self.spin.get(), '3') |
| 1324 |
|
| 1325 |
def test_configure_format(self): |
| 1326 |
self.spin.set(1) |
| 1327 |
self.spin['format'] = '%10.3f' |
| 1328 |
self.spin.update() |
| 1329 |
self._click_increment_arrow() |
| 1330 |
value = self.spin.get() |
| 1331 |
|
| 1332 |
self.assertEqual(len(value), 10) |
| 1333 |
self.assertEqual(value.index('.'), 6) |
| 1334 |
|
| 1335 |
self.spin['format'] = '' |
| 1336 |
self.spin.update() |
| 1337 |
self._click_increment_arrow() |
| 1338 |
value = self.spin.get() |
| 1339 |
self.assertTrue('.' not in value) |
| 1340 |
self.assertEqual(len(value), 1) |
| 1341 |
|
| 1342 |
def test_configure_wrap(self): |
| 1343 |
self.spin['to'] = 10 |
| 1344 |
self.spin['from'] = 1 |
| 1345 |
self.spin.set(1) |
| 1346 |
self.spin['wrap'] = True |
| 1347 |
self.spin.update() |
| 1348 |
|
| 1349 |
self._click_decrement_arrow() |
| 1350 |
self.assertEqual(self.spin.get(), '10') |
| 1351 |
|
| 1352 |
self._click_increment_arrow() |
| 1353 |
self.assertEqual(self.spin.get(), '1') |
| 1354 |
|
| 1355 |
self.spin['wrap'] = False |
| 1356 |
self.spin.update() |
| 1357 |
|
| 1358 |
self._click_decrement_arrow() |
| 1359 |
self.assertEqual(self.spin.get(), '1') |
| 1360 |
|
| 1361 |
def test_configure_values(self): |
| 1362 |
self.assertEqual(self.spin['values'], '') |
| 1363 |
self.checkParam(self.spin, 'values', 'mon tue wed thur', |
| 1364 |
expected=('mon', 'tue', 'wed', 'thur')) |
| 1365 |
self.checkParam(self.spin, 'values', ('mon', 'tue', 'wed', 'thur')) |
| 1366 |
self.checkParam(self.spin, 'values', (42, 3.14, '', 'any string')) |
| 1367 |
self.checkParam(self.spin, 'values', '') |
| 1368 |
|
| 1369 |
self.spin['values'] = ['a', 1, 'c'] |
| 1370 |
|
| 1371 |
# test incrementing / decrementing values |
| 1372 |
self.spin.set('a') |
| 1373 |
self.spin.update() |
| 1374 |
self._click_increment_arrow() |
| 1375 |
self.assertEqual(self.spin.get(), '1') |
| 1376 |
|
| 1377 |
self._click_decrement_arrow() |
| 1378 |
self.assertEqual(self.spin.get(), 'a') |
| 1379 |
|
| 1380 |
# testing values with empty string set through configure |
| 1381 |
self.spin.configure(values=[1, '', 2]) |
| 1382 |
self.assertEqual(self.spin['values'], |
| 1383 |
('1', '', '2') if self.wantobjects else |
| 1384 |
'1 {} 2') |
| 1385 |
|
| 1386 |
# testing values with spaces |
| 1387 |
self.spin['values'] = ['a b', 'a\tb', 'a\nb'] |
| 1388 |
self.assertEqual(self.spin['values'], |
| 1389 |
('a b', 'a\tb', 'a\nb') if self.wantobjects else |
| 1390 |
'{a b} {a\tb} {a\nb}') |
| 1391 |
|
| 1392 |
# testing values with special characters |
| 1393 |
self.spin['values'] = [r'a\tb', '"a"', '} {'] |
| 1394 |
self.assertEqual(self.spin['values'], |
| 1395 |
(r'a\tb', '"a"', '} {') if self.wantobjects else |
| 1396 |
r'a\\tb {"a"} \}\ \{') |
| 1397 |
|
| 1398 |
# testing creating spinbox with empty string in values |
| 1399 |
spin2 = ttk.Spinbox(self.root, values=[1, 2, '']) |
| 1400 |
self.assertEqual(spin2['values'], |
| 1401 |
('1', '2', '') if self.wantobjects else '1 2 {}') |
| 1402 |
spin2.destroy() |
| 1403 |
|
| 1404 |
|
| 1405 |
@add_configure_tests(StandardTtkOptionsTests) |
| 1406 |
class TreeviewTest(AbstractWidgetTest, unittest.TestCase): |
| 1407 |
OPTIONS = ( |
| 1408 |
'class', 'columns', 'cursor', 'displaycolumns', |
| 1409 |
'height', 'padding', 'selectmode', 'selecttype', 'show', 'striped', |
| 1410 |
'style', 'takefocus', 'titlecolumns', 'titleitems', |
| 1411 |
'xscrollcommand', 'yscrollcommand', |
| 1412 |
) |
| 1413 |
_rounds_pixels = False |
| 1414 |
_clipped = {} |
| 1415 |
|
| 1416 |
def setUp(self): |
| 1417 |
super().setUp() |
| 1418 |
self.tv = self.create(padding=0) |
| 1419 |
|
| 1420 |
def create(self, **kwargs): |
| 1421 |
return ttk.Treeview(self.root, **kwargs) |
| 1422 |
|
| 1423 |
def test_configure_columns(self): |
| 1424 |
widget = self.create() |
| 1425 |
self.checkParam(widget, 'columns', 'a b c', |
| 1426 |
expected=('a', 'b', 'c')) |
| 1427 |
self.checkParam(widget, 'columns', ('a', 'b', 'c')) |
| 1428 |
self.checkParam(widget, 'columns', '', |
| 1429 |
expected=() if tk_version >= (8, 7) else '') |
| 1430 |
|
| 1431 |
def test_configure_displaycolumns(self): |
| 1432 |
widget = self.create() |
| 1433 |
widget['columns'] = ('a', 'b', 'c') |
| 1434 |
self.checkParam(widget, 'displaycolumns', 'b a c', |
| 1435 |
expected=('b', 'a', 'c')) |
| 1436 |
self.checkParam(widget, 'displaycolumns', ('b', 'a', 'c')) |
| 1437 |
self.checkParam(widget, 'displaycolumns', '#all', |
| 1438 |
expected=('#all',)) |
| 1439 |
self.checkParam(widget, 'displaycolumns', (2, 1, 0)) |
| 1440 |
self.checkInvalidParam(widget, 'displaycolumns', ('a', 'b', 'd'), |
| 1441 |
errmsg='Invalid column index "?d"?') |
| 1442 |
errmsg = 'Column index "?{}"? out of bounds' |
| 1443 |
self.checkInvalidParam(widget, 'displaycolumns', (1, 2, 3), |
| 1444 |
errmsg=errmsg.format(3)) |
| 1445 |
self.checkInvalidParam(widget, 'displaycolumns', (1, -2), |
| 1446 |
errmsg=errmsg.format(-2)) |
| 1447 |
|
| 1448 |
def test_configure_height(self): |
| 1449 |
widget = self.create() |
| 1450 |
self.checkPixelsParam(widget, 'height', 100, -100, 0, '3c', |
| 1451 |
conv=False) |
| 1452 |
self.checkPixelsParam(widget, 'height', 101.2, 102.6, '3c', |
| 1453 |
conv=False) |
| 1454 |
|
| 1455 |
def test_configure_selectmode(self): |
| 1456 |
widget = self.create() |
| 1457 |
self.checkEnumParam(widget, 'selectmode', |
| 1458 |
'none', 'browse', 'extended') |
| 1459 |
|
| 1460 |
@requires_tk(8, 7) |
| 1461 |
def test_configure_selecttype(self): |
| 1462 |
widget = self.create() |
| 1463 |
self.checkEnumParam(widget, 'selecttype', 'item', 'cell') |
| 1464 |
|
| 1465 |
def test_configure_show(self): |
| 1466 |
widget = self.create() |
| 1467 |
self.checkParam(widget, 'show', 'tree headings', |
| 1468 |
expected=('tree', 'headings')) |
| 1469 |
self.checkParam(widget, 'show', ('tree', 'headings')) |
| 1470 |
self.checkParam(widget, 'show', ('headings', 'tree')) |
| 1471 |
self.checkParam(widget, 'show', 'tree', expected=('tree',)) |
| 1472 |
self.checkParam(widget, 'show', 'headings', expected=('headings',)) |
| 1473 |
|
| 1474 |
@requires_tk(8, 7) |
| 1475 |
def test_configure_striped(self): |
| 1476 |
widget = self.create() |
| 1477 |
self.checkBooleanParam(widget, 'striped') |
| 1478 |
|
| 1479 |
@requires_tk(8, 7) |
| 1480 |
def test_configure_titlecolumns(self): |
| 1481 |
widget = self.create() |
| 1482 |
self.checkIntegerParam(widget, 'titlecolumns', 0, 1, 5) |
| 1483 |
self.checkInvalidParam(widget, 'titlecolumns', -2) |
| 1484 |
|
| 1485 |
@requires_tk(8, 7) |
| 1486 |
def test_configure_titleitems(self): |
| 1487 |
widget = self.create() |
| 1488 |
self.checkIntegerParam(widget, 'titleitems', 0, 1, 5) |
| 1489 |
self.checkInvalidParam(widget, 'titleitems', -2) |
| 1490 |
|
| 1491 |
def test_bbox(self): |
| 1492 |
self.tv.pack() |
| 1493 |
self.assertEqual(self.tv.bbox(''), '') |
| 1494 |
self.tv.update() |
| 1495 |
|
| 1496 |
item_id = self.tv.insert('', 'end') |
| 1497 |
children = self.tv.get_children() |
| 1498 |
self.assertTrue(children) |
| 1499 |
|
| 1500 |
bbox = self.tv.bbox(children[0]) |
| 1501 |
self.assertIsBoundingBox(bbox) |
| 1502 |
|
| 1503 |
# compare width in bboxes |
| 1504 |
self.tv['columns'] = ['test'] |
| 1505 |
self.tv.column('test', width=50) |
| 1506 |
bbox_column0 = self.tv.bbox(children[0], 0) |
| 1507 |
root_width = self.tv.column('#0', width=None) |
| 1508 |
if not self.wantobjects: |
| 1509 |
root_width = int(root_width) |
| 1510 |
self.assertEqual(bbox_column0[0], bbox[0] + root_width) |
| 1511 |
|
| 1512 |
# verify that bbox of a closed item is the empty string |
| 1513 |
child1 = self.tv.insert(item_id, 'end') |
| 1514 |
self.assertEqual(self.tv.bbox(child1), '') |
| 1515 |
|
| 1516 |
def test_children(self): |
| 1517 |
# no children yet, should get an empty tuple |
| 1518 |
self.assertEqual(self.tv.get_children(), ()) |
| 1519 |
|
| 1520 |
item_id = self.tv.insert('', 'end') |
| 1521 |
self.assertIsInstance(self.tv.get_children(), tuple) |
| 1522 |
self.assertEqual(self.tv.get_children()[0], item_id) |
| 1523 |
|
| 1524 |
# add item_id and child3 as children of child2 |
| 1525 |
child2 = self.tv.insert('', 'end') |
| 1526 |
child3 = self.tv.insert('', 'end') |
| 1527 |
self.tv.set_children(child2, item_id, child3) |
| 1528 |
self.assertEqual(self.tv.get_children(child2), (item_id, child3)) |
| 1529 |
|
| 1530 |
# child3 has child2 as parent, thus trying to set child2 as a children |
| 1531 |
# of child3 should result in an error |
| 1532 |
self.assertRaises(tkinter.TclError, |
| 1533 |
self.tv.set_children, child3, child2) |
| 1534 |
|
| 1535 |
# remove child2 children |
| 1536 |
self.tv.set_children(child2) |
| 1537 |
self.assertEqual(self.tv.get_children(child2), ()) |
| 1538 |
|
| 1539 |
# remove root's children |
| 1540 |
self.tv.set_children('') |
| 1541 |
self.assertEqual(self.tv.get_children(), ()) |
| 1542 |
|
| 1543 |
def test_column(self): |
| 1544 |
# return a dict with all options/values |
| 1545 |
self.assertIsInstance(self.tv.column('#0'), dict) |
| 1546 |
# return a single value of the given option |
| 1547 |
if self.wantobjects: |
| 1548 |
self.assertIsInstance(self.tv.column('#0', width=None), int) |
| 1549 |
# set a new value for an option |
| 1550 |
self.tv.column('#0', width=10) |
| 1551 |
# testing new way to get option value |
| 1552 |
self.assertEqual(self.tv.column('#0', 'width'), |
| 1553 |
10 if self.wantobjects else '10') |
| 1554 |
self.assertEqual(self.tv.column('#0', width=None), |
| 1555 |
10 if self.wantobjects else '10') |
| 1556 |
# check read-only option |
| 1557 |
self.assertRaises(tkinter.TclError, self.tv.column, '#0', id='X') |
| 1558 |
|
| 1559 |
self.assertRaises(tkinter.TclError, self.tv.column, 'invalid') |
| 1560 |
invalid_kws = [ |
| 1561 |
{'unknown_option': 'some value'}, {'stretch': 'wrong'}, |
| 1562 |
{'anchor': 'wrong'}, {'width': 'wrong'}, {'minwidth': 'wrong'} |
| 1563 |
] |
| 1564 |
for kw in invalid_kws: |
| 1565 |
self.assertRaises(tkinter.TclError, self.tv.column, '#0', |
| 1566 |
**kw) |
| 1567 |
|
| 1568 |
def test_delete(self): |
| 1569 |
self.assertRaises(tkinter.TclError, self.tv.delete, '#0') |
| 1570 |
|
| 1571 |
item_id = self.tv.insert('', 'end') |
| 1572 |
item2 = self.tv.insert(item_id, 'end') |
| 1573 |
self.assertEqual(self.tv.get_children(), (item_id, )) |
| 1574 |
self.assertEqual(self.tv.get_children(item_id), (item2, )) |
| 1575 |
|
| 1576 |
self.tv.delete(item_id) |
| 1577 |
self.assertFalse(self.tv.get_children()) |
| 1578 |
|
| 1579 |
# reattach should fail |
| 1580 |
self.assertRaises(tkinter.TclError, |
| 1581 |
self.tv.reattach, item_id, '', 'end') |
| 1582 |
|
| 1583 |
# test multiple item delete |
| 1584 |
item1 = self.tv.insert('', 'end') |
| 1585 |
item2 = self.tv.insert('', 'end') |
| 1586 |
self.assertEqual(self.tv.get_children(), (item1, item2)) |
| 1587 |
|
| 1588 |
self.tv.delete(item1, item2) |
| 1589 |
self.assertFalse(self.tv.get_children()) |
| 1590 |
|
| 1591 |
def test_detach_reattach(self): |
| 1592 |
item_id = self.tv.insert('', 'end') |
| 1593 |
item2 = self.tv.insert(item_id, 'end') |
| 1594 |
|
| 1595 |
# calling detach without items is valid, although it does nothing |
| 1596 |
prev = self.tv.get_children() |
| 1597 |
self.tv.detach() # this should do nothing |
| 1598 |
self.assertEqual(prev, self.tv.get_children()) |
| 1599 |
|
| 1600 |
self.assertEqual(self.tv.get_children(), (item_id, )) |
| 1601 |
self.assertEqual(self.tv.get_children(item_id), (item2, )) |
| 1602 |
|
| 1603 |
# detach item with children |
| 1604 |
self.tv.detach(item_id) |
| 1605 |
self.assertFalse(self.tv.get_children()) |
| 1606 |
|
| 1607 |
# reattach item with children |
| 1608 |
self.tv.reattach(item_id, '', 'end') |
| 1609 |
self.assertEqual(self.tv.get_children(), (item_id, )) |
| 1610 |
self.assertEqual(self.tv.get_children(item_id), (item2, )) |
| 1611 |
|
| 1612 |
# move a children to the root |
| 1613 |
self.tv.move(item2, '', 'end') |
| 1614 |
self.assertEqual(self.tv.get_children(), (item_id, item2)) |
| 1615 |
self.assertEqual(self.tv.get_children(item_id), ()) |
| 1616 |
|
| 1617 |
# bad values |
| 1618 |
self.assertRaises(tkinter.TclError, |
| 1619 |
self.tv.reattach, 'nonexistent', '', 'end') |
| 1620 |
self.assertRaises(tkinter.TclError, |
| 1621 |
self.tv.detach, 'nonexistent') |
| 1622 |
self.assertRaises(tkinter.TclError, |
| 1623 |
self.tv.reattach, item2, 'otherparent', 'end') |
| 1624 |
self.assertRaises(tkinter.TclError, |
| 1625 |
self.tv.reattach, item2, '', 'invalid') |
| 1626 |
|
| 1627 |
# multiple detach |
| 1628 |
self.tv.detach(item_id, item2) |
| 1629 |
self.assertEqual(self.tv.get_children(), ()) |
| 1630 |
self.assertEqual(self.tv.get_children(item_id), ()) |
| 1631 |
|
| 1632 |
def test_exists(self): |
| 1633 |
self.assertEqual(self.tv.exists('something'), False) |
| 1634 |
self.assertEqual(self.tv.exists(''), True) |
| 1635 |
self.assertEqual(self.tv.exists({}), False) |
| 1636 |
|
| 1637 |
# the following will make a tk.call equivalent to |
| 1638 |
# tk.call(treeview, "exists") which should result in an error |
| 1639 |
# in the tcl interpreter since tk requires an item. |
| 1640 |
self.assertRaises(tkinter.TclError, self.tv.exists, None) |
| 1641 |
|
| 1642 |
def test_focus(self): |
| 1643 |
# nothing is focused right now |
| 1644 |
self.assertEqual(self.tv.focus(), '') |
| 1645 |
|
| 1646 |
item1 = self.tv.insert('', 'end') |
| 1647 |
self.tv.focus(item1) |
| 1648 |
self.assertEqual(self.tv.focus(), item1) |
| 1649 |
|
| 1650 |
self.tv.delete(item1) |
| 1651 |
self.assertEqual(self.tv.focus(), '') |
| 1652 |
|
| 1653 |
# try focusing inexistent item |
| 1654 |
self.assertRaises(tkinter.TclError, self.tv.focus, 'hi') |
| 1655 |
|
| 1656 |
def test_heading(self): |
| 1657 |
# check a dict is returned |
| 1658 |
self.assertIsInstance(self.tv.heading('#0'), dict) |
| 1659 |
|
| 1660 |
# check a value is returned |
| 1661 |
self.tv.heading('#0', text='hi') |
| 1662 |
self.assertEqual(self.tv.heading('#0', 'text'), 'hi') |
| 1663 |
self.assertEqual(self.tv.heading('#0', text=None), 'hi') |
| 1664 |
|
| 1665 |
# invalid option |
| 1666 |
self.assertRaises(tkinter.TclError, self.tv.heading, '#0', |
| 1667 |
background=None) |
| 1668 |
# invalid value |
| 1669 |
self.assertRaises(tkinter.TclError, self.tv.heading, '#0', |
| 1670 |
anchor=1) |
| 1671 |
|
| 1672 |
def test_heading_callback(self): |
| 1673 |
def simulate_heading_click(x, y): |
| 1674 |
if tk_version >= (8, 6): |
| 1675 |
self.assertEqual(self.tv.identify_column(x), '#0') |
| 1676 |
self.assertEqual(self.tv.identify_region(x, y), 'heading') |
| 1677 |
simulate_mouse_click(self.tv, x, y) |
| 1678 |
self.tv.update() |
| 1679 |
|
| 1680 |
success = [] # no success for now |
| 1681 |
|
| 1682 |
self.tv.pack() |
| 1683 |
self.tv.heading('#0', command=lambda: success.append(True)) |
| 1684 |
self.tv.column('#0', width=100) |
| 1685 |
self.tv.update() |
| 1686 |
|
| 1687 |
# assuming that the coords (5, 5) fall into heading #0 |
| 1688 |
simulate_heading_click(5, 5) |
| 1689 |
if not success: |
| 1690 |
self.fail("The command associated to the treeview heading wasn't " |
| 1691 |
"invoked.") |
| 1692 |
|
| 1693 |
success = [] |
| 1694 |
commands = self.tv.master._tclCommands |
| 1695 |
self.tv.heading('#0', command=str(self.tv.heading('#0', command=None))) |
| 1696 |
self.assertEqual(commands, self.tv.master._tclCommands) |
| 1697 |
simulate_heading_click(5, 5) |
| 1698 |
if not success: |
| 1699 |
self.fail("The command associated to the treeview heading wasn't " |
| 1700 |
"invoked.") |
| 1701 |
|
| 1702 |
# XXX The following raises an error in a tcl interpreter, but not in |
| 1703 |
# Python |
| 1704 |
#self.tv.heading('#0', command='I dont exist') |
| 1705 |
#simulate_heading_click(5, 5) |
| 1706 |
|
| 1707 |
def test_index(self): |
| 1708 |
# item 'what' doesn't exist |
| 1709 |
self.assertRaises(tkinter.TclError, self.tv.index, 'what') |
| 1710 |
|
| 1711 |
self.assertEqual(self.tv.index(''), 0) |
| 1712 |
|
| 1713 |
item1 = self.tv.insert('', 'end') |
| 1714 |
item2 = self.tv.insert('', 'end') |
| 1715 |
c1 = self.tv.insert(item1, 'end') |
| 1716 |
c2 = self.tv.insert(item1, 'end') |
| 1717 |
self.assertEqual(self.tv.index(item1), 0) |
| 1718 |
self.assertEqual(self.tv.index(c1), 0) |
| 1719 |
self.assertEqual(self.tv.index(c2), 1) |
| 1720 |
self.assertEqual(self.tv.index(item2), 1) |
| 1721 |
|
| 1722 |
self.tv.move(item2, '', 0) |
| 1723 |
self.assertEqual(self.tv.index(item2), 0) |
| 1724 |
self.assertEqual(self.tv.index(item1), 1) |
| 1725 |
|
| 1726 |
# check that index still works even after its parent and siblings |
| 1727 |
# have been detached |
| 1728 |
self.tv.detach(item1) |
| 1729 |
self.assertEqual(self.tv.index(c2), 1) |
| 1730 |
self.tv.detach(c1) |
| 1731 |
self.assertEqual(self.tv.index(c2), 0) |
| 1732 |
|
| 1733 |
# but it fails after item has been deleted |
| 1734 |
self.tv.delete(item1) |
| 1735 |
self.assertRaises(tkinter.TclError, self.tv.index, c2) |
| 1736 |
|
| 1737 |
def test_insert_item(self): |
| 1738 |
# parent 'none' doesn't exist |
| 1739 |
self.assertRaises(tkinter.TclError, self.tv.insert, 'none', 'end') |
| 1740 |
|
| 1741 |
# open values |
| 1742 |
self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', |
| 1743 |
open='') |
| 1744 |
self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', |
| 1745 |
open='please') |
| 1746 |
self.assertFalse(self.tv.delete(self.tv.insert('', 'end', open=True))) |
| 1747 |
self.assertFalse(self.tv.delete(self.tv.insert('', 'end', open=False))) |
| 1748 |
|
| 1749 |
# invalid index |
| 1750 |
self.assertRaises(tkinter.TclError, self.tv.insert, '', 'middle') |
| 1751 |
|
| 1752 |
# trying to duplicate item id is invalid |
| 1753 |
itemid = self.tv.insert('', 'end', 'first-item') |
| 1754 |
self.assertEqual(itemid, 'first-item') |
| 1755 |
self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', |
| 1756 |
'first-item') |
| 1757 |
self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', |
| 1758 |
MockTclObj('first-item')) |
| 1759 |
|
| 1760 |
# unicode values |
| 1761 |
value = '\xe1ba' |
| 1762 |
item = self.tv.insert('', 'end', values=(value, )) |
| 1763 |
self.assertEqual(self.tv.item(item, 'values'), |
| 1764 |
(value,) if self.wantobjects else value) |
| 1765 |
self.assertEqual(self.tv.item(item, values=None), |
| 1766 |
(value,) if self.wantobjects else value) |
| 1767 |
|
| 1768 |
self.tv.item(item, values=self.root.splitlist(self.tv.item(item, values=None))) |
| 1769 |
self.assertEqual(self.tv.item(item, values=None), |
| 1770 |
(value,) if self.wantobjects else value) |
| 1771 |
|
| 1772 |
self.assertIsInstance(self.tv.item(item), dict) |
| 1773 |
|
| 1774 |
# erase item values |
| 1775 |
self.tv.item(item, values='') |
| 1776 |
self.assertFalse(self.tv.item(item, values=None)) |
| 1777 |
|
| 1778 |
# item tags |
| 1779 |
item = self.tv.insert('', 'end', tags=[1, 2, value]) |
| 1780 |
self.assertEqual(self.tv.item(item, tags=None), |
| 1781 |
('1', '2', value) if self.wantobjects else |
| 1782 |
'1 2 %s' % value) |
| 1783 |
self.tv.item(item, tags=[]) |
| 1784 |
self.assertFalse(self.tv.item(item, tags=None)) |
| 1785 |
self.tv.item(item, tags=(1, 2)) |
| 1786 |
self.assertEqual(self.tv.item(item, tags=None), |
| 1787 |
('1', '2') if self.wantobjects else '1 2') |
| 1788 |
|
| 1789 |
# values with spaces |
| 1790 |
item = self.tv.insert('', 'end', values=('a b c', |
| 1791 |
'%s %s' % (value, value))) |
| 1792 |
self.assertEqual(self.tv.item(item, values=None), |
| 1793 |
('a b c', '%s %s' % (value, value)) if self.wantobjects else |
| 1794 |
'{a b c} {%s %s}' % (value, value)) |
| 1795 |
|
| 1796 |
# text |
| 1797 |
self.assertEqual(self.tv.item( |
| 1798 |
self.tv.insert('', 'end', text="Label here"), text=None), |
| 1799 |
"Label here") |
| 1800 |
self.assertEqual(self.tv.item( |
| 1801 |
self.tv.insert('', 'end', text=value), text=None), |
| 1802 |
value) |
| 1803 |
|
| 1804 |
# test for values which are not None |
| 1805 |
itemid = self.tv.insert('', 'end', 0) |
| 1806 |
self.assertEqual(itemid, '0') |
| 1807 |
itemid = self.tv.insert('', 'end', 0.0) |
| 1808 |
self.assertEqual(itemid, '0.0') |
| 1809 |
# this is because False resolves to 0 and element with 0 iid is already present |
| 1810 |
self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', False) |
| 1811 |
self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', '') |
| 1812 |
|
| 1813 |
def test_selection(self): |
| 1814 |
self.assertRaises(TypeError, self.tv.selection, 'spam') |
| 1815 |
# item 'none' doesn't exist |
| 1816 |
self.assertRaises(tkinter.TclError, self.tv.selection_set, 'none') |
| 1817 |
self.assertRaises(tkinter.TclError, self.tv.selection_add, 'none') |
| 1818 |
self.assertRaises(tkinter.TclError, self.tv.selection_remove, 'none') |
| 1819 |
self.assertRaises(tkinter.TclError, self.tv.selection_toggle, 'none') |
| 1820 |
|
| 1821 |
item1 = self.tv.insert('', 'end') |
| 1822 |
item2 = self.tv.insert('', 'end') |
| 1823 |
c1 = self.tv.insert(item1, 'end') |
| 1824 |
c2 = self.tv.insert(item1, 'end') |
| 1825 |
c3 = self.tv.insert(item1, 'end') |
| 1826 |
self.assertEqual(self.tv.selection(), ()) |
| 1827 |
|
| 1828 |
self.tv.selection_set(c1, item2) |
| 1829 |
self.assertEqual(self.tv.selection(), (c1, item2)) |
| 1830 |
self.tv.selection_set(c2) |
| 1831 |
self.assertEqual(self.tv.selection(), (c2,)) |
| 1832 |
|
| 1833 |
self.tv.selection_add(c1, item2) |
| 1834 |
self.assertEqual(self.tv.selection(), (c1, c2, item2)) |
| 1835 |
self.tv.selection_add(item1) |
| 1836 |
self.assertEqual(self.tv.selection(), (item1, c1, c2, item2)) |
| 1837 |
self.tv.selection_add() |
| 1838 |
self.assertEqual(self.tv.selection(), (item1, c1, c2, item2)) |
| 1839 |
|
| 1840 |
self.tv.selection_remove(item1, c3) |
| 1841 |
self.assertEqual(self.tv.selection(), (c1, c2, item2)) |
| 1842 |
self.tv.selection_remove(c2) |
| 1843 |
self.assertEqual(self.tv.selection(), (c1, item2)) |
| 1844 |
self.tv.selection_remove() |
| 1845 |
self.assertEqual(self.tv.selection(), (c1, item2)) |
| 1846 |
|
| 1847 |
self.tv.selection_toggle(c1, c3) |
| 1848 |
self.assertEqual(self.tv.selection(), (c3, item2)) |
| 1849 |
self.tv.selection_toggle(item2) |
| 1850 |
self.assertEqual(self.tv.selection(), (c3,)) |
| 1851 |
self.tv.selection_toggle() |
| 1852 |
self.assertEqual(self.tv.selection(), (c3,)) |
| 1853 |
|
| 1854 |
self.tv.insert('', 'end', id='with spaces') |
| 1855 |
self.tv.selection_set('with spaces') |
| 1856 |
self.assertEqual(self.tv.selection(), ('with spaces',)) |
| 1857 |
|
| 1858 |
self.tv.insert('', 'end', id='{brace') |
| 1859 |
self.tv.selection_set('{brace') |
| 1860 |
self.assertEqual(self.tv.selection(), ('{brace',)) |
| 1861 |
|
| 1862 |
self.tv.insert('', 'end', id='unicode\u20ac') |
| 1863 |
self.tv.selection_set('unicode\u20ac') |
| 1864 |
self.assertEqual(self.tv.selection(), ('unicode\u20ac',)) |
| 1865 |
|
| 1866 |
self.tv.insert('', 'end', id=b'bytes\xe2\x82\xac') |
| 1867 |
self.tv.selection_set(b'bytes\xe2\x82\xac') |
| 1868 |
self.assertEqual(self.tv.selection(), ('bytes\xe2\x82\xac',)) |
| 1869 |
|
| 1870 |
self.tv.selection_set() |
| 1871 |
self.assertEqual(self.tv.selection(), ()) |
| 1872 |
|
| 1873 |
# Old interface |
| 1874 |
self.tv.selection_set((c1, item2)) |
| 1875 |
self.assertEqual(self.tv.selection(), (c1, item2)) |
| 1876 |
self.tv.selection_add((c1, item1)) |
| 1877 |
self.assertEqual(self.tv.selection(), (item1, c1, item2)) |
| 1878 |
self.tv.selection_remove((item1, c3)) |
| 1879 |
self.assertEqual(self.tv.selection(), (c1, item2)) |
| 1880 |
self.tv.selection_toggle((c1, c3)) |
| 1881 |
self.assertEqual(self.tv.selection(), (c3, item2)) |
| 1882 |
|
| 1883 |
def test_set(self): |
| 1884 |
self.tv['columns'] = ['A', 'B'] |
| 1885 |
item = self.tv.insert('', 'end', values=['a', 'b']) |
| 1886 |
self.assertEqual(self.tv.set(item), {'A': 'a', 'B': 'b'}) |
| 1887 |
|
| 1888 |
self.tv.set(item, 'B', 'a') |
| 1889 |
self.assertEqual(self.tv.item(item, values=None), |
| 1890 |
('a', 'a') if self.wantobjects else 'a a') |
| 1891 |
|
| 1892 |
self.tv['columns'] = ['B'] |
| 1893 |
self.assertEqual(self.tv.set(item), {'B': 'a'}) |
| 1894 |
|
| 1895 |
self.tv.set(item, 'B', 'b') |
| 1896 |
self.assertEqual(self.tv.set(item, column='B'), 'b') |
| 1897 |
self.assertEqual(self.tv.item(item, values=None), |
| 1898 |
('b', 'a') if self.wantobjects else 'b a') |
| 1899 |
|
| 1900 |
self.tv.set(item, 'B', 123) |
| 1901 |
self.assertEqual(self.tv.set(item, 'B'), |
| 1902 |
123 if self.wantobjects else '123') |
| 1903 |
self.assertEqual(self.tv.item(item, values=None), |
| 1904 |
(123, 'a') if self.wantobjects else '123 a') |
| 1905 |
self.assertEqual(self.tv.set(item), |
| 1906 |
{'B': 123} if self.wantobjects else {'B': '123'}) |
| 1907 |
|
| 1908 |
# inexistent column |
| 1909 |
self.assertRaises(tkinter.TclError, self.tv.set, item, 'A') |
| 1910 |
self.assertRaises(tkinter.TclError, self.tv.set, item, 'A', 'b') |
| 1911 |
|
| 1912 |
# inexistent item |
| 1913 |
self.assertRaises(tkinter.TclError, self.tv.set, 'notme') |
| 1914 |
|
| 1915 |
def test_tag_bind(self): |
| 1916 |
events = [] |
| 1917 |
item1 = self.tv.insert('', 'end', tags=['call']) |
| 1918 |
item2 = self.tv.insert('', 'end', tags=['call']) |
| 1919 |
self.tv.tag_bind('call', '<ButtonPress-1>', |
| 1920 |
lambda evt: events.append(1)) |
| 1921 |
self.tv.tag_bind('call', '<ButtonRelease-1>', |
| 1922 |
lambda evt: events.append(2)) |
| 1923 |
|
| 1924 |
self.tv.pack() |
| 1925 |
self.tv.update() |
| 1926 |
|
| 1927 |
pos_y = set() |
| 1928 |
found = set() |
| 1929 |
for i in range(0, 100, 10): |
| 1930 |
if len(found) == 2: # item1 and item2 already found |
| 1931 |
break |
| 1932 |
item_id = self.tv.identify_row(i) |
| 1933 |
if item_id and item_id not in found: |
| 1934 |
pos_y.add(i) |
| 1935 |
found.add(item_id) |
| 1936 |
|
| 1937 |
self.assertEqual(len(pos_y), 2) # item1 and item2 y pos |
| 1938 |
for y in pos_y: |
| 1939 |
simulate_mouse_click(self.tv, 0, y) |
| 1940 |
|
| 1941 |
# by now there should be 4 things in the events list, since each |
| 1942 |
# item had a bind for two events that were simulated above |
| 1943 |
self.assertEqual(len(events), 4) |
| 1944 |
for evt in zip(events[::2], events[1::2]): |
| 1945 |
self.assertEqual(evt, (1, 2)) |
| 1946 |
|
| 1947 |
def test_tag_configure(self): |
| 1948 |
# Just testing parameter passing for now |
| 1949 |
self.assertRaises(TypeError, self.tv.tag_configure) |
| 1950 |
self.assertRaises(tkinter.TclError, self.tv.tag_configure, |
| 1951 |
'test', sky='blue') |
| 1952 |
self.tv.tag_configure('test', foreground='blue') |
| 1953 |
self.assertEqual(str(self.tv.tag_configure('test', 'foreground')), |
| 1954 |
'blue') |
| 1955 |
self.assertEqual(str(self.tv.tag_configure('test', foreground=None)), |
| 1956 |
'blue') |
| 1957 |
self.assertIsInstance(self.tv.tag_configure('test'), dict) |
| 1958 |
|
| 1959 |
def test_tag_has(self): |
| 1960 |
item1 = self.tv.insert('', 'end', text='Item 1', tags=['tag1']) |
| 1961 |
item2 = self.tv.insert('', 'end', text='Item 2', tags=['tag2']) |
| 1962 |
self.assertRaises(TypeError, self.tv.tag_has) |
| 1963 |
self.assertRaises(TclError, self.tv.tag_has, 'tag1', 'non-existing') |
| 1964 |
self.assertTrue(self.tv.tag_has('tag1', item1)) |
| 1965 |
self.assertFalse(self.tv.tag_has('tag1', item2)) |
| 1966 |
self.assertFalse(self.tv.tag_has('tag2', item1)) |
| 1967 |
self.assertTrue(self.tv.tag_has('tag2', item2)) |
| 1968 |
self.assertFalse(self.tv.tag_has('tag3', item1)) |
| 1969 |
self.assertFalse(self.tv.tag_has('tag3', item2)) |
| 1970 |
self.assertEqual(self.tv.tag_has('tag1'), (item1,)) |
| 1971 |
self.assertEqual(self.tv.tag_has('tag2'), (item2,)) |
| 1972 |
self.assertEqual(self.tv.tag_has('tag3'), ()) |
| 1973 |
|
| 1974 |
|
| 1975 |
@add_configure_tests(StandardTtkOptionsTests) |
| 1976 |
class SeparatorTest(AbstractWidgetTest, unittest.TestCase): |
| 1977 |
OPTIONS = ( |
| 1978 |
'class', 'cursor', 'orient', 'style', 'takefocus', |
| 1979 |
# 'state'? |
| 1980 |
) |
| 1981 |
_rounds_pixels = False |
| 1982 |
_clipped = {} |
| 1983 |
default_orient = 'horizontal' |
| 1984 |
|
| 1985 |
def create(self, **kwargs): |
| 1986 |
return ttk.Separator(self.root, **kwargs) |
| 1987 |
|
| 1988 |
|
| 1989 |
@add_configure_tests(StandardTtkOptionsTests) |
| 1990 |
class SizegripTest(AbstractWidgetTest, unittest.TestCase): |
| 1991 |
OPTIONS = ( |
| 1992 |
'class', 'cursor', 'style', 'takefocus', |
| 1993 |
# 'state'? |
| 1994 |
) |
| 1995 |
_rounds_pixels = False |
| 1996 |
_clipped = {} |
| 1997 |
|
| 1998 |
def create(self, **kwargs): |
| 1999 |
return ttk.Sizegrip(self.root, **kwargs) |
| 2000 |
|
| 2001 |
|
| 2002 |
class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase): |
| 2003 |
|
| 2004 |
def test_frame(self): |
| 2005 |
self._test_widget(ttk.Frame) |
| 2006 |
|
| 2007 |
def test_label(self): |
| 2008 |
self._test_widget(ttk.Label) |
| 2009 |
|
| 2010 |
|
| 2011 |
if __name__ == "__main__": |
| 2012 |
unittest.main() |