| 1 |
import unittest |
| 2 |
import tkinter |
| 3 |
from tkinter import TclError |
| 4 |
import os |
| 5 |
from test.support import requires |
| 6 |
|
| 7 |
from test.test_tkinter.support import setUpModule # noqa: F401 |
| 8 |
from test.test_tkinter.support import (requires_tk, tk_version, |
| 9 |
get_tk_patchlevel, widget_eq, |
| 10 |
AbstractDefaultRootTest) |
| 11 |
|
| 12 |
from test.test_tkinter.widget_tests import ( |
| 13 |
add_configure_tests, |
| 14 |
AbstractWidgetTest, |
| 15 |
StandardOptionsTests, |
| 16 |
IntegerSizeTests, |
| 17 |
PixelSizeTests) |
| 18 |
|
| 19 |
requires('gui') |
| 20 |
|
| 21 |
|
| 22 |
EXPECTED_SCREEN_DISTANCE_ERRMSG = '(bad|expected) screen distance (but got )?"{}"' |
| 23 |
EXPECTED_SCREEN_DISTANCE_OR_EMPTY_ERRMSG = '(bad|expected) screen distance (or "" but got )?"{}"' |
| 24 |
|
| 25 |
def float_round(x): |
| 26 |
return float(round(x)) |
| 27 |
|
| 28 |
class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests): |
| 29 |
_no_round = {'padx', 'pady'} |
| 30 |
if tk_version < (8, 7): |
| 31 |
_clipped = {'highlightthickness'} |
| 32 |
else: |
| 33 |
_clipped = {'borderwidth', 'height', 'highlightthickness', 'padx', |
| 34 |
'pady', 'width'} |
| 35 |
|
| 36 |
def test_configure_class(self): |
| 37 |
widget = self.create() |
| 38 |
self.assertEqual(widget['class'], |
| 39 |
widget.__class__.__name__.title()) |
| 40 |
self.checkInvalidParam(widget, 'class', 'Foo', |
| 41 |
errmsg="can't modify -class option after widget is created") |
| 42 |
widget2 = self.create(class_='Foo') |
| 43 |
self.assertEqual(widget2['class'], 'Foo') |
| 44 |
|
| 45 |
def test_configure_colormap(self): |
| 46 |
widget = self.create() |
| 47 |
self.assertEqual(widget['colormap'], '') |
| 48 |
self.checkInvalidParam(widget, 'colormap', 'new', |
| 49 |
errmsg="can't modify -colormap option after widget is created") |
| 50 |
widget2 = self.create(colormap='new') |
| 51 |
self.assertEqual(widget2['colormap'], 'new') |
| 52 |
|
| 53 |
def test_configure_container(self): |
| 54 |
widget = self.create() |
| 55 |
self.assertEqual(widget['container'], 0 if self.wantobjects else '0') |
| 56 |
self.checkInvalidParam(widget, 'container', 1, |
| 57 |
errmsg="can't modify -container option after widget is created") |
| 58 |
widget2 = self.create(container=True) |
| 59 |
self.assertEqual(widget2['container'], 1 if self.wantobjects else '1') |
| 60 |
|
| 61 |
def test_configure_visual(self): |
| 62 |
widget = self.create() |
| 63 |
self.assertEqual(widget['visual'], '') |
| 64 |
self.checkInvalidParam(widget, 'visual', 'default', |
| 65 |
errmsg="can't modify -visual option after widget is created") |
| 66 |
widget2 = self.create(visual='default') |
| 67 |
self.assertEqual(widget2['visual'], 'default') |
| 68 |
|
| 69 |
|
| 70 |
@add_configure_tests(StandardOptionsTests) |
| 71 |
class ToplevelTest(AbstractToplevelTest, unittest.TestCase): |
| 72 |
OPTIONS = ( |
| 73 |
'background', 'backgroundimage', 'borderwidth', |
| 74 |
'class', 'colormap', 'container', 'cursor', 'height', |
| 75 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 76 |
'menu', 'padx', 'pady', 'relief', 'screen', |
| 77 |
'takefocus', 'tile', 'use', 'visual', 'width', |
| 78 |
) |
| 79 |
|
| 80 |
def create(self, **kwargs): |
| 81 |
return tkinter.Toplevel(self.root, **kwargs) |
| 82 |
|
| 83 |
def test_configure_menu(self): |
| 84 |
widget = self.create() |
| 85 |
menu = tkinter.Menu(self.root) |
| 86 |
self.checkParam(widget, 'menu', menu, eq=widget_eq) |
| 87 |
self.checkParam(widget, 'menu', '') |
| 88 |
|
| 89 |
def test_configure_screen(self): |
| 90 |
widget = self.create() |
| 91 |
if widget._windowingsystem != 'x11': |
| 92 |
self.skipTest('Not using Tk for X11') |
| 93 |
self.assertEqual(widget['screen'], '') |
| 94 |
try: |
| 95 |
display = os.environ['DISPLAY'] |
| 96 |
except KeyError: |
| 97 |
self.skipTest('No $DISPLAY set.') |
| 98 |
self.checkInvalidParam(widget, 'screen', display, |
| 99 |
errmsg="can't modify -screen option after widget is created") |
| 100 |
widget2 = self.create(screen=display) |
| 101 |
self.assertEqual(widget2['screen'], display) |
| 102 |
|
| 103 |
def test_configure_use(self): |
| 104 |
widget = self.create() |
| 105 |
self.assertEqual(widget['use'], '') |
| 106 |
parent = self.create(container=True) |
| 107 |
wid = hex(parent.winfo_id()) |
| 108 |
with self.subTest(wid=wid): |
| 109 |
widget2 = self.create(use=wid) |
| 110 |
self.assertEqual(widget2['use'], wid) |
| 111 |
|
| 112 |
|
| 113 |
@add_configure_tests(StandardOptionsTests) |
| 114 |
class FrameTest(AbstractToplevelTest, unittest.TestCase): |
| 115 |
OPTIONS = ( |
| 116 |
'background', 'backgroundimage', 'borderwidth', |
| 117 |
'class', 'colormap', 'container', 'cursor', 'height', |
| 118 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 119 |
'padx', 'pady', 'relief', 'takefocus', 'tile', 'visual', 'width', |
| 120 |
) |
| 121 |
|
| 122 |
def create(self, **kwargs): |
| 123 |
return tkinter.Frame(self.root, **kwargs) |
| 124 |
|
| 125 |
|
| 126 |
@add_configure_tests(StandardOptionsTests) |
| 127 |
class LabelFrameTest(AbstractToplevelTest, unittest.TestCase): |
| 128 |
OPTIONS = ( |
| 129 |
'background', 'borderwidth', |
| 130 |
'class', 'colormap', 'container', 'cursor', |
| 131 |
'font', 'foreground', 'height', |
| 132 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 133 |
'labelanchor', 'labelwidget', 'padx', 'pady', 'relief', |
| 134 |
'takefocus', 'text', 'visual', 'width', |
| 135 |
) |
| 136 |
|
| 137 |
def create(self, **kwargs): |
| 138 |
return tkinter.LabelFrame(self.root, **kwargs) |
| 139 |
|
| 140 |
def test_configure_labelanchor(self): |
| 141 |
widget = self.create() |
| 142 |
self.checkEnumParam(widget, 'labelanchor', |
| 143 |
'e', 'en', 'es', 'n', 'ne', 'nw', |
| 144 |
's', 'se', 'sw', 'w', 'wn', 'ws') |
| 145 |
self.checkInvalidParam(widget, 'labelanchor', 'center') |
| 146 |
|
| 147 |
def test_configure_labelwidget(self): |
| 148 |
widget = self.create() |
| 149 |
label = tkinter.Label(self.root, text='Mupp', name='foo') |
| 150 |
self.checkParam(widget, 'labelwidget', label, expected='.foo') |
| 151 |
label.destroy() |
| 152 |
|
| 153 |
# Label, Button, Checkbutton, Radiobutton, MenuButton |
| 154 |
class AbstractLabelTest(AbstractWidgetTest, IntegerSizeTests): |
| 155 |
_rounds_pixels = False |
| 156 |
if tk_version < (8, 7): |
| 157 |
_clipped = {} |
| 158 |
elif tk_version < (9, 0): |
| 159 |
_clipped = {'borderwidth', 'height', 'highlightthickness', 'padx', 'pady', 'width'} |
| 160 |
else: |
| 161 |
_clipped = {'borderwidth', 'height', 'highlightthickness', |
| 162 |
'insertborderwidth', 'padx', 'pady', 'width'} |
| 163 |
|
| 164 |
def setUp(self): |
| 165 |
super().setUp() |
| 166 |
if tk_version[:2] == (9, 0) and get_tk_patchlevel(self.root) < (9, 0, 2): |
| 167 |
self._clipped = self._clipped - {'height', 'width'} |
| 168 |
|
| 169 |
|
| 170 |
@add_configure_tests(StandardOptionsTests) |
| 171 |
class LabelTest(AbstractLabelTest, unittest.TestCase): |
| 172 |
OPTIONS = ( |
| 173 |
'activebackground', 'activeforeground', 'anchor', |
| 174 |
'background', 'bitmap', 'borderwidth', 'compound', 'cursor', |
| 175 |
'disabledforeground', 'font', 'foreground', 'height', |
| 176 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 177 |
'image', 'justify', 'padx', 'pady', 'relief', 'state', |
| 178 |
'takefocus', 'text', 'textvariable', |
| 179 |
'underline', 'width', 'wraplength', |
| 180 |
) |
| 181 |
|
| 182 |
def create(self, **kwargs): |
| 183 |
return tkinter.Label(self.root, **kwargs) |
| 184 |
|
| 185 |
|
| 186 |
@add_configure_tests(StandardOptionsTests) |
| 187 |
class ButtonTest(AbstractLabelTest, unittest.TestCase): |
| 188 |
OPTIONS = ( |
| 189 |
'activebackground', 'activeforeground', 'anchor', |
| 190 |
'background', 'bitmap', 'borderwidth', |
| 191 |
'command', 'compound', 'cursor', 'default', |
| 192 |
'disabledforeground', 'font', 'foreground', 'height', |
| 193 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 194 |
'image', 'justify', 'overrelief', 'padx', 'pady', 'relief', |
| 195 |
'repeatdelay', 'repeatinterval', |
| 196 |
'state', 'takefocus', 'text', 'textvariable', |
| 197 |
'underline', 'width', 'wraplength') |
| 198 |
if tk_version < (8, 7): |
| 199 |
_clipped = {} |
| 200 |
else: |
| 201 |
_clipped = {'borderwidth', 'height', 'highlightthickness', |
| 202 |
'padx', 'pady', 'width'} |
| 203 |
|
| 204 |
def create(self, **kwargs): |
| 205 |
return tkinter.Button(self.root, **kwargs) |
| 206 |
|
| 207 |
def test_configure_default(self): |
| 208 |
widget = self.create() |
| 209 |
self.checkEnumParam(widget, 'default', 'active', 'disabled', 'normal') |
| 210 |
|
| 211 |
|
| 212 |
@add_configure_tests(StandardOptionsTests) |
| 213 |
class CheckbuttonTest(AbstractLabelTest, unittest.TestCase): |
| 214 |
OPTIONS = ( |
| 215 |
'activebackground', 'activeforeground', 'anchor', |
| 216 |
'background', 'bitmap', 'borderwidth', |
| 217 |
'command', 'compound', 'cursor', |
| 218 |
'disabledforeground', 'font', 'foreground', 'height', |
| 219 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 220 |
'image', 'indicatoron', 'justify', |
| 221 |
'offrelief', 'offvalue', 'onvalue', 'overrelief', |
| 222 |
'padx', 'pady', 'relief', 'selectcolor', 'selectimage', 'state', |
| 223 |
'takefocus', 'text', 'textvariable', |
| 224 |
'tristateimage', 'tristatevalue', |
| 225 |
'underline', 'variable', 'width', 'wraplength', |
| 226 |
) |
| 227 |
|
| 228 |
def create(self, **kwargs): |
| 229 |
return tkinter.Checkbutton(self.root, **kwargs) |
| 230 |
|
| 231 |
|
| 232 |
def test_configure_offvalue(self): |
| 233 |
widget = self.create() |
| 234 |
self.checkParams(widget, 'offvalue', 1, 2.3, '', 'any string') |
| 235 |
|
| 236 |
def test_configure_onvalue(self): |
| 237 |
widget = self.create() |
| 238 |
self.checkParams(widget, 'onvalue', 1, 2.3, '', 'any string') |
| 239 |
|
| 240 |
def test_unique_variables(self): |
| 241 |
frames = [] |
| 242 |
buttons = [] |
| 243 |
for i in range(2): |
| 244 |
f = tkinter.Frame(self.root) |
| 245 |
f.pack() |
| 246 |
frames.append(f) |
| 247 |
for j in 'AB': |
| 248 |
b = tkinter.Checkbutton(f, text=j) |
| 249 |
b.pack() |
| 250 |
buttons.append(b) |
| 251 |
variables = [str(b['variable']) for b in buttons] |
| 252 |
self.assertEqual(len(set(variables)), 4, variables) |
| 253 |
|
| 254 |
def test_same_name(self): |
| 255 |
f1 = tkinter.Frame(self.root) |
| 256 |
f2 = tkinter.Frame(self.root) |
| 257 |
b1 = tkinter.Checkbutton(f1, name='test', text='Test1') |
| 258 |
b2 = tkinter.Checkbutton(f2, name='test', text='Test2') |
| 259 |
|
| 260 |
v = tkinter.IntVar(self.root, name='test') |
| 261 |
b1.select() |
| 262 |
self.assertEqual(v.get(), 1) |
| 263 |
b2.deselect() |
| 264 |
self.assertEqual(v.get(), 0) |
| 265 |
|
| 266 |
@add_configure_tests(StandardOptionsTests) |
| 267 |
class RadiobuttonTest(AbstractLabelTest, unittest.TestCase): |
| 268 |
OPTIONS = ( |
| 269 |
'activebackground', 'activeforeground', 'anchor', |
| 270 |
'background', 'bitmap', 'borderwidth', |
| 271 |
'command', 'compound', 'cursor', |
| 272 |
'disabledforeground', 'font', 'foreground', 'height', |
| 273 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 274 |
'image', 'indicatoron', 'justify', 'offrelief', 'overrelief', |
| 275 |
'padx', 'pady', 'relief', 'selectcolor', 'selectimage', 'state', |
| 276 |
'takefocus', 'text', 'textvariable', |
| 277 |
'tristateimage', 'tristatevalue', |
| 278 |
'underline', 'value', 'variable', 'width', 'wraplength', |
| 279 |
) |
| 280 |
|
| 281 |
def create(self, **kwargs): |
| 282 |
return tkinter.Radiobutton(self.root, **kwargs) |
| 283 |
|
| 284 |
def test_configure_value(self): |
| 285 |
widget = self.create() |
| 286 |
self.checkParams(widget, 'value', 1, 2.3, '', 'any string') |
| 287 |
|
| 288 |
|
| 289 |
@add_configure_tests(StandardOptionsTests) |
| 290 |
class MenubuttonTest(AbstractLabelTest, unittest.TestCase): |
| 291 |
OPTIONS = ( |
| 292 |
'activebackground', 'activeforeground', 'anchor', |
| 293 |
'background', 'bitmap', 'borderwidth', |
| 294 |
'compound', 'cursor', 'direction', |
| 295 |
'disabledforeground', 'font', 'foreground', 'height', |
| 296 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 297 |
'image', 'indicatoron', 'justify', 'menu', |
| 298 |
'padx', 'pady', 'relief', 'state', |
| 299 |
'takefocus', 'text', 'textvariable', |
| 300 |
'underline', 'width', 'wraplength', |
| 301 |
) |
| 302 |
_rounds_pixels = (tk_version < (9, 0)) |
| 303 |
if tk_version < (8, 7): |
| 304 |
_clipped = {'highlightthickness', 'padx', 'pady'} |
| 305 |
elif tk_version < (9, 0): |
| 306 |
_clipped = {'borderwidth', 'highlightthickness', 'padx', 'pady'} |
| 307 |
else: |
| 308 |
_clipped = {'borderwidth', 'highlightthickness', 'insertborderwidth', 'padx', 'pady'} |
| 309 |
|
| 310 |
def setUp(self): |
| 311 |
super().setUp() |
| 312 |
if tk_version[:2] == (9, 0) and get_tk_patchlevel(self.root) < (9, 0, 1): |
| 313 |
self._clipped = self._clipped - {'borderwidth'} |
| 314 |
|
| 315 |
def create(self, **kwargs): |
| 316 |
return tkinter.Menubutton(self.root, **kwargs) |
| 317 |
|
| 318 |
def test_configure_direction(self): |
| 319 |
widget = self.create() |
| 320 |
self.checkEnumParam(widget, 'direction', |
| 321 |
'above', 'below', 'flush', 'left', 'right') |
| 322 |
|
| 323 |
def test_configure_height(self): |
| 324 |
widget = self.create() |
| 325 |
if tk_version < (8, 7) or (tk_version[:2] == (9, 0) and get_tk_patchlevel(self.root) < (9, 0, 1)): |
| 326 |
conv = str |
| 327 |
else: |
| 328 |
conv = False |
| 329 |
self.checkIntegerParam(widget, 'height', 100, -100, 0, conv=conv) |
| 330 |
|
| 331 |
def test_configure_image(self): |
| 332 |
widget = self.create() |
| 333 |
image = tkinter.PhotoImage(master=self.root, name='image1') |
| 334 |
self.checkParam(widget, 'image', image, conv=str) |
| 335 |
if tk_version < (8, 7): |
| 336 |
errmsg = 'image "spam" doesn\'t exist' |
| 337 |
else: |
| 338 |
errmsg = 'image "spam" does not exist' |
| 339 |
with self.assertRaises(tkinter.TclError) as cm: |
| 340 |
widget['image'] = 'spam' |
| 341 |
if errmsg is not None: |
| 342 |
self.assertEqual(str(cm.exception), errmsg) |
| 343 |
with self.assertRaises(tkinter.TclError) as cm: |
| 344 |
widget.configure({'image': 'spam'}) |
| 345 |
if errmsg is not None: |
| 346 |
self.assertEqual(str(cm.exception), errmsg) |
| 347 |
|
| 348 |
def test_configure_menu(self): |
| 349 |
widget = self.create() |
| 350 |
menu = tkinter.Menu(widget, name='menu') |
| 351 |
self.checkParam(widget, 'menu', menu, eq=widget_eq) |
| 352 |
menu.destroy() |
| 353 |
|
| 354 |
def test_configure_width(self): |
| 355 |
widget = self.create() |
| 356 |
if tk_version < (8, 7) or (tk_version[:2] == (9, 0) and get_tk_patchlevel(self.root) < (9, 0, 1)): |
| 357 |
conv = str |
| 358 |
else: |
| 359 |
conv = False |
| 360 |
self.checkIntegerParam(widget, 'width', 402, -402, 0, conv=conv) |
| 361 |
|
| 362 |
|
| 363 |
class OptionMenuTest(MenubuttonTest, unittest.TestCase): |
| 364 |
|
| 365 |
def create(self, default='b', values=('a', 'b', 'c'), **kwargs): |
| 366 |
return tkinter.OptionMenu(self.root, None, default, *values, **kwargs) |
| 367 |
|
| 368 |
def test_bad_kwarg(self): |
| 369 |
with self.assertRaisesRegex(TclError, r"^unknown option -image$"): |
| 370 |
tkinter.OptionMenu(self.root, None, 'b', image='') |
| 371 |
|
| 372 |
def test_specify_name(self): |
| 373 |
widget = tkinter.OptionMenu(self.root, None, ':)', name="option_menu") |
| 374 |
self.assertEqual(str(widget), ".option_menu") |
| 375 |
self.assertIs(self.root.children["option_menu"], widget) |
| 376 |
|
| 377 |
@add_configure_tests(IntegerSizeTests, StandardOptionsTests) |
| 378 |
class EntryTest(AbstractWidgetTest, unittest.TestCase): |
| 379 |
if tk_version < (8, 7): |
| 380 |
_clipped = {'highlightthickness'} |
| 381 |
else: |
| 382 |
_clipped = {'borderwidth', 'highlightthickness', 'insertborderwidth', |
| 383 |
'insertwidth', 'selectborderwidth'} |
| 384 |
|
| 385 |
OPTIONS = ( |
| 386 |
'background', 'borderwidth', 'cursor', |
| 387 |
'disabledbackground', 'disabledforeground', |
| 388 |
'exportselection', 'font', 'foreground', |
| 389 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 390 |
'insertbackground', 'insertborderwidth', |
| 391 |
'insertofftime', 'insertontime', 'insertwidth', |
| 392 |
'invalidcommand', 'justify', 'placeholder', 'placeholderforeground', |
| 393 |
'readonlybackground', 'relief', |
| 394 |
'selectbackground', 'selectborderwidth', 'selectforeground', |
| 395 |
'show', 'state', 'takefocus', 'textvariable', |
| 396 |
'validate', 'validatecommand', 'width', 'xscrollcommand', |
| 397 |
) |
| 398 |
|
| 399 |
def create(self, **kwargs): |
| 400 |
return tkinter.Entry(self.root, **kwargs) |
| 401 |
|
| 402 |
def test_configure_disabledbackground(self): |
| 403 |
widget = self.create() |
| 404 |
self.checkColorParam(widget, 'disabledbackground') |
| 405 |
|
| 406 |
def test_configure_insertborderwidth(self): |
| 407 |
widget = self.create(insertwidth=100) |
| 408 |
self.checkPixelsParam(widget, 'insertborderwidth', |
| 409 |
0, 1.3, 2.6, 6, -2, '10p') |
| 410 |
# insertborderwidth is bounded above by a half of insertwidth. |
| 411 |
expected = 100 // 2 if tk_version < (8, 7) else 60 |
| 412 |
self.checkParam(widget, 'insertborderwidth', 60, expected=expected) |
| 413 |
|
| 414 |
def test_configure_insertwidth(self): |
| 415 |
widget = self.create() |
| 416 |
self.checkPixelsParam(widget, 'insertwidth', 1.3, 3.6, 0.9, '10p') |
| 417 |
if tk_version < (8, 7): |
| 418 |
self.checkParam(widget, 'insertwidth', 0, expected=2) |
| 419 |
self.checkParam(widget, 'insertwidth', 0.1, expected=2) |
| 420 |
self.checkParam(widget, 'insertwidth', -2, expected=2) |
| 421 |
else: |
| 422 |
self.checkPixelsParam(widget, 'insertwidth', 0, 0.1, -2) |
| 423 |
|
| 424 |
def test_configure_invalidcommand(self): |
| 425 |
widget = self.create() |
| 426 |
self.checkCommandParam(widget, 'invalidcommand') |
| 427 |
self.checkCommandParam(widget, 'invcmd') |
| 428 |
|
| 429 |
def test_configure_readonlybackground(self): |
| 430 |
widget = self.create() |
| 431 |
self.checkColorParam(widget, 'readonlybackground') |
| 432 |
|
| 433 |
def test_configure_show(self): |
| 434 |
widget = self.create() |
| 435 |
self.checkParam(widget, 'show', '*') |
| 436 |
self.checkParam(widget, 'show', '') |
| 437 |
self.checkParam(widget, 'show', ' ') |
| 438 |
|
| 439 |
def test_configure_state(self): |
| 440 |
widget = self.create() |
| 441 |
self.checkEnumParam(widget, 'state', |
| 442 |
'disabled', 'normal', 'readonly') |
| 443 |
|
| 444 |
def test_configure_validate(self): |
| 445 |
widget = self.create() |
| 446 |
self.checkEnumParam(widget, 'validate', |
| 447 |
'all', 'key', 'focus', 'focusin', 'focusout', 'none') |
| 448 |
|
| 449 |
def test_configure_validatecommand(self): |
| 450 |
widget = self.create() |
| 451 |
self.checkCommandParam(widget, 'validatecommand') |
| 452 |
self.checkCommandParam(widget, 'vcmd') |
| 453 |
|
| 454 |
def test_selection_methods(self): |
| 455 |
widget = self.create() |
| 456 |
widget.insert(0, '12345') |
| 457 |
self.assertFalse(widget.selection_present()) |
| 458 |
widget.selection_range(0, 'end') |
| 459 |
self.assertEqual(widget.selection_get(), '12345') |
| 460 |
self.assertTrue(widget.selection_present()) |
| 461 |
widget.selection_from(1) |
| 462 |
widget.selection_to(2) |
| 463 |
self.assertEqual(widget.selection_get(), '2') |
| 464 |
widget.selection_range(3, 4) |
| 465 |
self.assertEqual(widget.selection_get(), '4') |
| 466 |
widget.selection_clear() |
| 467 |
self.assertFalse(widget.selection_present()) |
| 468 |
widget.selection_range(0, 'end') |
| 469 |
widget.selection_adjust(4) |
| 470 |
self.assertEqual(widget.selection_get(), '1234') |
| 471 |
widget.selection_adjust(1) |
| 472 |
self.assertEqual(widget.selection_get(), '234') |
| 473 |
widget.selection_adjust(5) |
| 474 |
self.assertEqual(widget.selection_get(), '2345') |
| 475 |
widget.selection_adjust(0) |
| 476 |
self.assertEqual(widget.selection_get(), '12345') |
| 477 |
widget.selection_adjust(0) |
| 478 |
|
| 479 |
|
| 480 |
@add_configure_tests(StandardOptionsTests) |
| 481 |
class SpinboxTest(EntryTest, unittest.TestCase): |
| 482 |
OPTIONS = ( |
| 483 |
'activebackground', 'background', 'borderwidth', |
| 484 |
'buttonbackground', 'buttoncursor', 'buttondownrelief', 'buttonuprelief', |
| 485 |
'command', 'cursor', 'disabledbackground', 'disabledforeground', |
| 486 |
'exportselection', 'font', 'foreground', 'format', 'from', |
| 487 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 488 |
'increment', |
| 489 |
'insertbackground', 'insertborderwidth', |
| 490 |
'insertofftime', 'insertontime', 'insertwidth', |
| 491 |
'invalidcommand', 'justify', 'placeholder', 'placeholderforeground', |
| 492 |
'relief', 'readonlybackground', 'repeatdelay', 'repeatinterval', |
| 493 |
'selectbackground', 'selectborderwidth', 'selectforeground', |
| 494 |
'state', 'takefocus', 'textvariable', 'to', |
| 495 |
'validate', 'validatecommand', 'values', |
| 496 |
'width', 'wrap', 'xscrollcommand', |
| 497 |
) |
| 498 |
|
| 499 |
def create(self, **kwargs): |
| 500 |
return tkinter.Spinbox(self.root, **kwargs) |
| 501 |
|
| 502 |
test_configure_show = None |
| 503 |
|
| 504 |
def test_configure_buttonbackground(self): |
| 505 |
widget = self.create() |
| 506 |
self.checkColorParam(widget, 'buttonbackground') |
| 507 |
|
| 508 |
def test_configure_buttoncursor(self): |
| 509 |
widget = self.create() |
| 510 |
self.checkCursorParam(widget, 'buttoncursor') |
| 511 |
|
| 512 |
def test_configure_buttondownrelief(self): |
| 513 |
widget = self.create() |
| 514 |
self.checkReliefParam(widget, 'buttondownrelief') |
| 515 |
|
| 516 |
def test_configure_buttonuprelief(self): |
| 517 |
widget = self.create() |
| 518 |
self.checkReliefParam(widget, 'buttonuprelief') |
| 519 |
|
| 520 |
def test_configure_format(self): |
| 521 |
widget = self.create() |
| 522 |
self.checkParam(widget, 'format', '%2f') |
| 523 |
self.checkParam(widget, 'format', '%2.2f') |
| 524 |
self.checkParam(widget, 'format', '%.2f') |
| 525 |
self.checkParam(widget, 'format', '%2.f') |
| 526 |
self.checkInvalidParam(widget, 'format', '%2e-1f') |
| 527 |
self.checkInvalidParam(widget, 'format', '2.2') |
| 528 |
self.checkInvalidParam(widget, 'format', '%2.-2f') |
| 529 |
self.checkParam(widget, 'format', '%-2.02f') |
| 530 |
self.checkParam(widget, 'format', '% 2.02f') |
| 531 |
self.checkParam(widget, 'format', '% -2.200f') |
| 532 |
self.checkParam(widget, 'format', '%09.200f') |
| 533 |
self.checkInvalidParam(widget, 'format', '%d') |
| 534 |
|
| 535 |
def test_configure_from(self): |
| 536 |
widget = self.create() |
| 537 |
self.checkParam(widget, 'to', 100.0) |
| 538 |
self.checkFloatParam(widget, 'from', -10, 10.2, 11.7) |
| 539 |
if tk_version >= (8, 7): |
| 540 |
self.checkFloatParam(widget, 'from', 200, expected=100) |
| 541 |
else: |
| 542 |
self.checkInvalidParam( |
| 543 |
widget, 'from', 200, |
| 544 |
errmsg='-to value must be greater than -from value') |
| 545 |
|
| 546 |
def test_configure_increment(self): |
| 547 |
widget = self.create() |
| 548 |
self.checkFloatParam(widget, 'increment', -1, 1, 10.2, 12.8, 0) |
| 549 |
|
| 550 |
def test_configure_to(self): |
| 551 |
widget = self.create() |
| 552 |
self.checkParam(widget, 'from', -100.0) |
| 553 |
self.checkFloatParam(widget, 'to', -10, 10.2, 11.7) |
| 554 |
if tk_version >= (8, 7): |
| 555 |
self.checkFloatParam(widget, 'to', -200, expected=-100) |
| 556 |
else: |
| 557 |
self.checkInvalidParam( |
| 558 |
widget, 'to', -200, |
| 559 |
errmsg='-to value must be greater than -from value') |
| 560 |
|
| 561 |
def test_configure_values(self): |
| 562 |
# XXX |
| 563 |
widget = self.create() |
| 564 |
self.assertEqual(widget['values'], '') |
| 565 |
if tk_version < (8, 7) or (tk_version[:2] == (9, 0) and get_tk_patchlevel(self.root) < (9, 0, 1)): |
| 566 |
expected = 'mon tue wed thur' |
| 567 |
else: |
| 568 |
expected = ('mon', 'tue', 'wed', 'thur') |
| 569 |
self.checkParam(widget, 'values', 'mon tue wed thur', |
| 570 |
expected=expected) |
| 571 |
self.checkParam(widget, 'values', ('mon', 'tue', 'wed', 'thur'), |
| 572 |
expected=expected) |
| 573 |
|
| 574 |
if tk_version < (8, 7) or (tk_version[:2] == (9, 0) and get_tk_patchlevel(self.root) < (9, 0, 1)): |
| 575 |
expected = '42 3.14 {} {any string}' |
| 576 |
else: |
| 577 |
expected = (42, 3.14, '', 'any string') |
| 578 |
self.checkParam(widget, 'values', (42, 3.14, '', 'any string'), |
| 579 |
expected=expected) |
| 580 |
|
| 581 |
self.checkParam(widget, 'values', '') |
| 582 |
|
| 583 |
def test_configure_wrap(self): |
| 584 |
widget = self.create() |
| 585 |
self.checkBooleanParam(widget, 'wrap') |
| 586 |
|
| 587 |
def test_bbox(self): |
| 588 |
widget = self.create() |
| 589 |
self.assertIsBoundingBox(widget.bbox(0)) |
| 590 |
self.assertRaises(tkinter.TclError, widget.bbox, 'noindex') |
| 591 |
self.assertRaises(tkinter.TclError, widget.bbox, None) |
| 592 |
self.assertRaises(TypeError, widget.bbox) |
| 593 |
self.assertRaises(TypeError, widget.bbox, 0, 1) |
| 594 |
|
| 595 |
def test_selection_methods(self): |
| 596 |
widget = self.create() |
| 597 |
widget.insert(0, '12345') |
| 598 |
self.assertFalse(widget.selection_present()) |
| 599 |
widget.selection_range(0, 'end') |
| 600 |
self.assertEqual(widget.selection_get(), '12345') |
| 601 |
self.assertTrue(widget.selection_present()) |
| 602 |
widget.selection_from(1) |
| 603 |
widget.selection_to(2) |
| 604 |
self.assertEqual(widget.selection_get(), '2') |
| 605 |
widget.selection_range(3, 4) |
| 606 |
self.assertEqual(widget.selection_get(), '4') |
| 607 |
widget.selection_clear() |
| 608 |
self.assertFalse(widget.selection_present()) |
| 609 |
widget.selection_range(0, 'end') |
| 610 |
widget.selection_adjust(4) |
| 611 |
self.assertEqual(widget.selection_get(), '1234') |
| 612 |
widget.selection_adjust(1) |
| 613 |
self.assertEqual(widget.selection_get(), '234') |
| 614 |
widget.selection_adjust(5) |
| 615 |
self.assertEqual(widget.selection_get(), '2345') |
| 616 |
widget.selection_adjust(0) |
| 617 |
self.assertEqual(widget.selection_get(), '12345') |
| 618 |
|
| 619 |
def test_selection_element(self): |
| 620 |
widget = self.create() |
| 621 |
self.assertEqual(widget.selection_element(), "none") |
| 622 |
widget.selection_element("buttonup") |
| 623 |
self.assertEqual(widget.selection_element(), "buttonup") |
| 624 |
widget.selection_element("buttondown") |
| 625 |
self.assertEqual(widget.selection_element(), "buttondown") |
| 626 |
|
| 627 |
|
| 628 |
@add_configure_tests(StandardOptionsTests) |
| 629 |
class TextTest(AbstractWidgetTest, unittest.TestCase): |
| 630 |
OPTIONS = ( |
| 631 |
'autoseparators', 'background', 'blockcursor', 'borderwidth', |
| 632 |
'cursor', 'endline', 'exportselection', |
| 633 |
'font', 'foreground', 'height', |
| 634 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 635 |
'inactiveselectbackground', 'insertbackground', 'insertborderwidth', |
| 636 |
'insertofftime', 'insertontime', 'insertunfocussed', 'insertwidth', |
| 637 |
'maxundo', 'padx', 'pady', 'relief', |
| 638 |
'selectbackground', 'selectborderwidth', 'selectforeground', |
| 639 |
'setgrid', 'spacing1', 'spacing2', 'spacing3', 'startline', 'state', |
| 640 |
'tabs', 'tabstyle', 'takefocus', 'undo', 'width', 'wrap', |
| 641 |
'xscrollcommand', 'yscrollcommand', |
| 642 |
) |
| 643 |
_no_round = {'selectborderwidth'} |
| 644 |
if tk_version < (9, 0): |
| 645 |
_clipped = {'highlightthickness', 'spacing1', 'spacing2', 'spacing3'} |
| 646 |
else: |
| 647 |
_clipped = {'borderwidth', 'height', 'highlightthickness', |
| 648 |
'insertborderwidth', 'insertwidth', 'padx', 'pady', |
| 649 |
'selectborderwidth', 'spacing1', 'spacing2', 'spacing3'} |
| 650 |
|
| 651 |
def setUp(self): |
| 652 |
super().setUp() |
| 653 |
if tk_version[:2] == (9, 0) and get_tk_patchlevel(self.root) < (9, 0, 2): |
| 654 |
self._clipped = self._clipped - {'borderwidth', 'height', 'padx', 'pady'} |
| 655 |
if tk_version[:2] == (9, 0) and get_tk_patchlevel(self.root) < (9, 0, 1): |
| 656 |
self._clipped = self._clipped - {'insertborderwidth', 'insertwidth', 'selectborderwidth'} |
| 657 |
|
| 658 |
def create(self, **kwargs): |
| 659 |
return tkinter.Text(self.root, **kwargs) |
| 660 |
|
| 661 |
def test_configure_autoseparators(self): |
| 662 |
widget = self.create() |
| 663 |
self.checkBooleanParam(widget, 'autoseparators') |
| 664 |
|
| 665 |
def test_configure_blockcursor(self): |
| 666 |
widget = self.create() |
| 667 |
self.checkBooleanParam(widget, 'blockcursor') |
| 668 |
|
| 669 |
def test_configure_endline(self): |
| 670 |
widget = self.create() |
| 671 |
text = '\n'.join('Line %d' for i in range(100)) |
| 672 |
widget.insert('end', text) |
| 673 |
self.checkParam(widget, 'endline', 200, expected='') |
| 674 |
self.checkParam(widget, 'endline', -10, expected='') |
| 675 |
self.checkInvalidParam(widget, 'endline', 'spam', |
| 676 |
errmsg='expected integer but got "spam"') |
| 677 |
self.checkParam(widget, 'endline', 50) |
| 678 |
self.checkParam(widget, 'startline', 15) |
| 679 |
self.checkInvalidParam(widget, 'endline', 10, |
| 680 |
errmsg='-startline must be less than or equal to -endline') |
| 681 |
|
| 682 |
def test_configure_height(self): |
| 683 |
widget = self.create() |
| 684 |
self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, '3c') |
| 685 |
if tk_version < (9, 0): |
| 686 |
self.checkParam(widget, 'height', 0, expected=1) |
| 687 |
self.checkParam(widget, 'height', -100, expected=1) |
| 688 |
else: |
| 689 |
self.checkPixelsParam(widget, 'height', 0, -100) |
| 690 |
|
| 691 |
def test_configure_maxundo(self): |
| 692 |
widget = self.create() |
| 693 |
self.checkIntegerParam(widget, 'maxundo', 0, 5, -1) |
| 694 |
|
| 695 |
def test_configure_inactiveselectbackground(self): |
| 696 |
widget = self.create() |
| 697 |
self.checkColorParam(widget, 'inactiveselectbackground') |
| 698 |
|
| 699 |
@requires_tk(8, 6) |
| 700 |
def test_configure_insertunfocussed(self): |
| 701 |
widget = self.create() |
| 702 |
self.checkEnumParam(widget, 'insertunfocussed', |
| 703 |
'hollow', 'none', 'solid') |
| 704 |
|
| 705 |
def test_configure_spacing1(self): |
| 706 |
widget = self.create() |
| 707 |
self.checkPixelsParam(widget, 'spacing1', 20, 21.4, 22.6, -5, '0.5c') |
| 708 |
|
| 709 |
def test_configure_spacing2(self): |
| 710 |
widget = self.create() |
| 711 |
self.checkPixelsParam(widget, 'spacing2', 5, 6.4, 7.6, -1, '0.1c') |
| 712 |
|
| 713 |
def test_configure_spacing3(self): |
| 714 |
widget = self.create() |
| 715 |
self.checkPixelsParam(widget, 'spacing3', 20, 21.4, 22.6, -10, '0.5c') |
| 716 |
|
| 717 |
def test_configure_startline(self): |
| 718 |
widget = self.create() |
| 719 |
text = '\n'.join('Line %d' for i in range(100)) |
| 720 |
widget.insert('end', text) |
| 721 |
self.checkParam(widget, 'startline', 200, expected='') |
| 722 |
self.checkParam(widget, 'startline', -10, expected='') |
| 723 |
self.checkInvalidParam(widget, 'startline', 'spam', |
| 724 |
errmsg='expected integer but got "spam"') |
| 725 |
self.checkParam(widget, 'startline', 10) |
| 726 |
self.checkParam(widget, 'endline', 50) |
| 727 |
self.checkInvalidParam(widget, 'startline', 70, |
| 728 |
errmsg='-startline must be less than or equal to -endline') |
| 729 |
|
| 730 |
def test_configure_state(self): |
| 731 |
widget = self.create() |
| 732 |
self.checkEnumParam(widget, 'state', 'disabled', 'normal') |
| 733 |
|
| 734 |
def test_configure_tabs(self): |
| 735 |
widget = self.create() |
| 736 |
self.checkParam(widget, 'tabs', (10.2, 20.7, '1i', '2i')) |
| 737 |
self.checkParam(widget, 'tabs', '10.2 20.7 1i 2i', |
| 738 |
expected=(10.2, 20.7, '1i', '2i') |
| 739 |
if get_tk_patchlevel(self.root) >= (8, 6, 14) |
| 740 |
else ('10.2', '20.7', '1i', '2i')) |
| 741 |
self.checkParam(widget, 'tabs', '2c left 4c 6c center', |
| 742 |
expected=('2c', 'left', '4c', '6c', 'center')) |
| 743 |
self.checkInvalidParam(widget, 'tabs', 'spam', |
| 744 |
errmsg=EXPECTED_SCREEN_DISTANCE_ERRMSG.format('spam')) |
| 745 |
|
| 746 |
def test_configure_tabstyle(self): |
| 747 |
widget = self.create() |
| 748 |
self.checkEnumParam(widget, 'tabstyle', 'tabular', 'wordprocessor') |
| 749 |
|
| 750 |
def test_configure_undo(self): |
| 751 |
widget = self.create() |
| 752 |
self.checkBooleanParam(widget, 'undo') |
| 753 |
|
| 754 |
def test_configure_width(self): |
| 755 |
widget = self.create() |
| 756 |
self.checkIntegerParam(widget, 'width', 402) |
| 757 |
self.checkParam(widget, 'width', -402, expected=1) |
| 758 |
self.checkParam(widget, 'width', 0, expected=1) |
| 759 |
|
| 760 |
def test_configure_wrap(self): |
| 761 |
widget = self.create() |
| 762 |
self.checkEnumParam(widget, 'wrap', 'char', 'none', 'word') |
| 763 |
|
| 764 |
def test_bbox(self): |
| 765 |
widget = self.create() |
| 766 |
self.assertIsBoundingBox(widget.bbox('1.1')) |
| 767 |
self.assertIsNone(widget.bbox('end')) |
| 768 |
self.assertRaises(tkinter.TclError, widget.bbox, 'noindex') |
| 769 |
self.assertRaises(tkinter.TclError, widget.bbox, None) |
| 770 |
self.assertRaises(TypeError, widget.bbox) |
| 771 |
self.assertRaises(TypeError, widget.bbox, '1.1', 'end') |
| 772 |
|
| 773 |
|
| 774 |
@add_configure_tests(PixelSizeTests, StandardOptionsTests) |
| 775 |
class CanvasTest(AbstractWidgetTest, unittest.TestCase): |
| 776 |
OPTIONS = ( |
| 777 |
'background', 'borderwidth', |
| 778 |
'closeenough', 'confine', 'cursor', 'height', |
| 779 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 780 |
'insertbackground', 'insertborderwidth', |
| 781 |
'insertofftime', 'insertontime', 'insertwidth', |
| 782 |
'offset', 'relief', 'scrollregion', |
| 783 |
'selectbackground', 'selectborderwidth', 'selectforeground', |
| 784 |
'state', 'takefocus', |
| 785 |
'xscrollcommand', 'xscrollincrement', |
| 786 |
'yscrollcommand', 'yscrollincrement', 'width', |
| 787 |
) |
| 788 |
if tk_version < (8, 7): |
| 789 |
_clipped = {'highlightthickness'} |
| 790 |
else: |
| 791 |
_clipped = {'borderwidth', 'height', 'highlightthickness', |
| 792 |
'insertborderwidth', 'insertwidth', 'selectborderwidth', |
| 793 |
'width', 'xscrollincrement', 'yscrollincrement'} |
| 794 |
_stringify = True |
| 795 |
|
| 796 |
def setUp(self): |
| 797 |
super().setUp() |
| 798 |
if tk_version[:2] == (9, 0) and get_tk_patchlevel(self.root) < (9, 0, 1): |
| 799 |
self._rounds_pixels = True |
| 800 |
self._no_round = {'borderwidth', 'height', 'highlightthickness', |
| 801 |
'width', 'xscrollincrement', 'yscrollincrement'} |
| 802 |
self._clipped = self._clipped - {'insertborderwidth', 'insertwidth', 'selectborderwidth'} |
| 803 |
|
| 804 |
def create(self, **kwargs): |
| 805 |
return tkinter.Canvas(self.root, **kwargs) |
| 806 |
|
| 807 |
def test_configure_closeenough(self): |
| 808 |
widget = self.create() |
| 809 |
self.checkFloatParam(widget, 'closeenough', 24, 2.4, 3.6, -3, |
| 810 |
conv=float) |
| 811 |
|
| 812 |
def test_configure_confine(self): |
| 813 |
widget = self.create() |
| 814 |
self.checkBooleanParam(widget, 'confine') |
| 815 |
|
| 816 |
def test_configure_offset(self): |
| 817 |
widget = self.create() |
| 818 |
self.assertEqual(widget['offset'], '0,0') |
| 819 |
self.checkParams(widget, 'offset', |
| 820 |
'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'center') |
| 821 |
self.checkParam(widget, 'offset', '10,20') |
| 822 |
self.checkParam(widget, 'offset', '#5,6') |
| 823 |
self.checkInvalidParam(widget, 'offset', 'spam') |
| 824 |
|
| 825 |
def test_configure_scrollregion(self): |
| 826 |
widget = self.create() |
| 827 |
self.checkParam(widget, 'scrollregion', '0 0 200 150') |
| 828 |
self.checkParam(widget, 'scrollregion', (0, 0, 200, 150), |
| 829 |
expected='0 0 200 150') |
| 830 |
self.checkParam(widget, 'scrollregion', '') |
| 831 |
self.checkInvalidParam(widget, 'scrollregion', 'spam', |
| 832 |
errmsg='bad scrollRegion "spam"') |
| 833 |
self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200, 'spam')) |
| 834 |
self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200)) |
| 835 |
self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200, 150, 0)) |
| 836 |
|
| 837 |
def test_configure_state(self): |
| 838 |
widget = self.create() |
| 839 |
self.checkEnumParam(widget, 'state', 'disabled', 'normal', |
| 840 |
errmsg='bad state value "{}": must be normal or disabled') |
| 841 |
|
| 842 |
def test_configure_xscrollincrement(self): |
| 843 |
widget = self.create() |
| 844 |
self.checkPixelsParam(widget, 'xscrollincrement', |
| 845 |
40, 0, 41.2, 43.6, -40, '0.5i') |
| 846 |
|
| 847 |
def test_configure_yscrollincrement(self): |
| 848 |
widget = self.create() |
| 849 |
self.checkPixelsParam(widget, 'yscrollincrement', |
| 850 |
10, 0, 11.2, 13.6, -10, '0.1i') |
| 851 |
|
| 852 |
def _test_option_joinstyle(self, c, factory): |
| 853 |
for joinstyle in 'bevel', 'miter', 'round': |
| 854 |
i = factory(joinstyle=joinstyle) |
| 855 |
self.assertEqual(c.itemcget(i, 'joinstyle'), joinstyle) |
| 856 |
self.assertRaises(TclError, factory, joinstyle='spam') |
| 857 |
|
| 858 |
def _test_option_smooth(self, c, factory): |
| 859 |
for smooth in 1, True, '1', 'true', 'yes', 'on': |
| 860 |
i = factory(smooth=smooth) |
| 861 |
self.assertEqual(c.itemcget(i, 'smooth'), 'true') |
| 862 |
for smooth in 0, False, '0', 'false', 'no', 'off': |
| 863 |
i = factory(smooth=smooth) |
| 864 |
self.assertEqual(c.itemcget(i, 'smooth'), '0') |
| 865 |
i = factory(smooth=True, splinestep=30) |
| 866 |
self.assertEqual(c.itemcget(i, 'smooth'), 'true') |
| 867 |
self.assertEqual(c.itemcget(i, 'splinestep'), '30') |
| 868 |
i = factory(smooth='raw', splinestep=30) |
| 869 |
self.assertEqual(c.itemcget(i, 'smooth'), 'raw') |
| 870 |
self.assertEqual(c.itemcget(i, 'splinestep'), '30') |
| 871 |
self.assertRaises(TclError, factory, smooth='spam') |
| 872 |
|
| 873 |
def test_create_rectangle(self): |
| 874 |
c = self.create() |
| 875 |
i1 = c.create_rectangle(20, 30, 60, 10) |
| 876 |
self.assertEqual(c.coords(i1), [20.0, 10.0, 60.0, 30.0]) |
| 877 |
self.assertEqual(c.bbox(i1), (19, 9, 61, 31)) |
| 878 |
|
| 879 |
i2 = c.create_rectangle([21, 31, 61, 11]) |
| 880 |
self.assertEqual(c.coords(i2), [21.0, 11.0, 61.0, 31.0]) |
| 881 |
self.assertEqual(c.bbox(i2), (20, 10, 62, 32)) |
| 882 |
|
| 883 |
i3 = c.create_rectangle((22, 32), (62, 12)) |
| 884 |
self.assertEqual(c.coords(i3), [22.0, 12.0, 62.0, 32.0]) |
| 885 |
self.assertEqual(c.bbox(i3), (21, 11, 63, 33)) |
| 886 |
|
| 887 |
i4 = c.create_rectangle([(23, 33), (63, 13)]) |
| 888 |
self.assertEqual(c.coords(i4), [23.0, 13.0, 63.0, 33.0]) |
| 889 |
self.assertEqual(c.bbox(i4), (22, 12, 64, 34)) |
| 890 |
|
| 891 |
self.assertRaises(TclError, c.create_rectangle, 20, 30, 60) |
| 892 |
self.assertRaises(TclError, c.create_rectangle, [20, 30, 60]) |
| 893 |
self.assertRaises(TclError, c.create_rectangle, 20, 30, 40, 50, 60, 10) |
| 894 |
self.assertRaises(TclError, c.create_rectangle, [20, 30, 40, 50, 60, 10]) |
| 895 |
self.assertRaises(TclError, c.create_rectangle, 20, 30) |
| 896 |
self.assertRaises(TclError, c.create_rectangle, [20, 30]) |
| 897 |
self.assertRaises(IndexError, c.create_rectangle) |
| 898 |
self.assertRaises(IndexError, c.create_rectangle, []) |
| 899 |
|
| 900 |
def test_create_line(self): |
| 901 |
c = self.create() |
| 902 |
i1 = c.create_line(20, 30, 40, 50, 60, 10) |
| 903 |
self.assertEqual(c.coords(i1), [20.0, 30.0, 40.0, 50.0, 60.0, 10.0]) |
| 904 |
self.assertEqual(c.bbox(i1), (18, 8, 62, 52)) |
| 905 |
self.assertEqual(c.itemcget(i1, 'arrow'), 'none') |
| 906 |
self.assertEqual(c.itemcget(i1, 'arrowshape'), '8 10 3') |
| 907 |
self.assertEqual(c.itemcget(i1, 'capstyle'), 'butt') |
| 908 |
self.assertEqual(c.itemcget(i1, 'joinstyle'), 'round') |
| 909 |
self.assertEqual(c.itemcget(i1, 'smooth'), '0') |
| 910 |
self.assertEqual(c.itemcget(i1, 'splinestep'), '12') |
| 911 |
|
| 912 |
i2 = c.create_line([21, 31, 41, 51, 61, 11]) |
| 913 |
self.assertEqual(c.coords(i2), [21.0, 31.0, 41.0, 51.0, 61.0, 11.0]) |
| 914 |
self.assertEqual(c.bbox(i2), (19, 9, 63, 53)) |
| 915 |
|
| 916 |
i3 = c.create_line((22, 32), (42, 52), (62, 12)) |
| 917 |
self.assertEqual(c.coords(i3), [22.0, 32.0, 42.0, 52.0, 62.0, 12.0]) |
| 918 |
self.assertEqual(c.bbox(i3), (20, 10, 64, 54)) |
| 919 |
|
| 920 |
i4 = c.create_line([(23, 33), (43, 53), (63, 13)]) |
| 921 |
self.assertEqual(c.coords(i4), [23.0, 33.0, 43.0, 53.0, 63.0, 13.0]) |
| 922 |
self.assertEqual(c.bbox(i4), (21, 11, 65, 55)) |
| 923 |
|
| 924 |
self.assertRaises(TclError, c.create_line, 20, 30, 60) |
| 925 |
self.assertRaises(TclError, c.create_line, [20, 30, 60]) |
| 926 |
self.assertRaises(TclError, c.create_line, 20, 30) |
| 927 |
self.assertRaises(TclError, c.create_line, [20, 30]) |
| 928 |
self.assertRaises(IndexError, c.create_line) |
| 929 |
self.assertRaises(IndexError, c.create_line, []) |
| 930 |
|
| 931 |
for arrow in 'none', 'first', 'last', 'both': |
| 932 |
i = c.create_line(20, 30, 60, 10, arrow=arrow) |
| 933 |
self.assertEqual(c.itemcget(i, 'arrow'), arrow) |
| 934 |
i = c.create_line(20, 30, 60, 10, arrow='first', arrowshape=[10, 15, 5]) |
| 935 |
self.assertEqual(c.itemcget(i, 'arrowshape'), '10 15 5') |
| 936 |
self.assertRaises(TclError, c.create_line, 20, 30, 60, 10, arrow='spam') |
| 937 |
|
| 938 |
for capstyle in 'butt', 'projecting', 'round': |
| 939 |
i = c.create_line(20, 30, 60, 10, capstyle=capstyle) |
| 940 |
self.assertEqual(c.itemcget(i, 'capstyle'), capstyle) |
| 941 |
self.assertRaises(TclError, c.create_line, 20, 30, 60, 10, capstyle='spam') |
| 942 |
|
| 943 |
self._test_option_joinstyle(c, |
| 944 |
lambda **kwargs: c.create_line(20, 30, 40, 50, 60, 10, **kwargs)) |
| 945 |
self._test_option_smooth(c, |
| 946 |
lambda **kwargs: c.create_line(20, 30, 60, 10, **kwargs)) |
| 947 |
|
| 948 |
def test_create_polygon(self): |
| 949 |
c = self.create() |
| 950 |
# In Tk < 8.7 polygons are filled, but has no outline by default. |
| 951 |
# This affects its size, so always explicitly specify outline. |
| 952 |
i1 = c.create_polygon(20, 30, 40, 50, 60, 10, outline='red') |
| 953 |
self.assertEqual(c.coords(i1), [20.0, 30.0, 40.0, 50.0, 60.0, 10.0]) |
| 954 |
self.assertEqual(c.bbox(i1), (18, 8, 62, 52)) |
| 955 |
self.assertEqual(c.itemcget(i1, 'joinstyle'), 'round') |
| 956 |
self.assertEqual(c.itemcget(i1, 'smooth'), '0') |
| 957 |
self.assertEqual(c.itemcget(i1, 'splinestep'), '12') |
| 958 |
|
| 959 |
i2 = c.create_polygon([21, 31, 41, 51, 61, 11], outline='red') |
| 960 |
self.assertEqual(c.coords(i2), [21.0, 31.0, 41.0, 51.0, 61.0, 11.0]) |
| 961 |
self.assertEqual(c.bbox(i2), (19, 9, 63, 53)) |
| 962 |
|
| 963 |
i3 = c.create_polygon((22, 32), (42, 52), (62, 12), outline='red') |
| 964 |
self.assertEqual(c.coords(i3), [22.0, 32.0, 42.0, 52.0, 62.0, 12.0]) |
| 965 |
self.assertEqual(c.bbox(i3), (20, 10, 64, 54)) |
| 966 |
|
| 967 |
i4 = c.create_polygon([(23, 33), (43, 53), (63, 13)], outline='red') |
| 968 |
self.assertEqual(c.coords(i4), [23.0, 33.0, 43.0, 53.0, 63.0, 13.0]) |
| 969 |
self.assertEqual(c.bbox(i4), (21, 11, 65, 55)) |
| 970 |
|
| 971 |
self.assertRaises(TclError, c.create_polygon, 20, 30, 60) |
| 972 |
self.assertRaises(TclError, c.create_polygon, [20, 30, 60]) |
| 973 |
self.assertRaises(IndexError, c.create_polygon) |
| 974 |
self.assertRaises(IndexError, c.create_polygon, []) |
| 975 |
|
| 976 |
self._test_option_joinstyle(c, |
| 977 |
lambda **kwargs: c.create_polygon(20, 30, 40, 50, 60, 10, **kwargs)) |
| 978 |
self._test_option_smooth(c, |
| 979 |
lambda **kwargs: c.create_polygon(20, 30, 40, 50, 60, 10, **kwargs)) |
| 980 |
|
| 981 |
def test_coords(self): |
| 982 |
c = self.create() |
| 983 |
i = c.create_line(20, 30, 40, 50, 60, 10, tags='x') |
| 984 |
self.assertEqual(c.coords(i), [20.0, 30.0, 40.0, 50.0, 60.0, 10.0]) |
| 985 |
self.assertEqual(c.coords('x'), [20.0, 30.0, 40.0, 50.0, 60.0, 10.0]) |
| 986 |
self.assertEqual(c.bbox(i), (18, 8, 62, 52)) |
| 987 |
|
| 988 |
c.coords(i, 50, 60, 70, 80, 90, 40) |
| 989 |
self.assertEqual(c.coords(i), [50.0, 60.0, 70.0, 80.0, 90.0, 40.0]) |
| 990 |
self.assertEqual(c.bbox(i), (48, 38, 92, 82)) |
| 991 |
|
| 992 |
c.coords(i, [21, 31, 41, 51, 61, 11]) |
| 993 |
self.assertEqual(c.coords(i), [21.0, 31.0, 41.0, 51.0, 61.0, 11.0]) |
| 994 |
|
| 995 |
c.coords(i, (22, 32), (42, 52), (62, 12)) |
| 996 |
self.assertEqual(c.coords(i), [22.0, 32.0, 42.0, 52.0, 62.0, 12.0]) |
| 997 |
|
| 998 |
c.coords(i, [(23, 33), (43, 53), (63, 13)]) |
| 999 |
self.assertEqual(c.coords(i), [23.0, 33.0, 43.0, 53.0, 63.0, 13.0]) |
| 1000 |
|
| 1001 |
c.coords(i, 20, 30, 60, 10) |
| 1002 |
self.assertEqual(c.coords(i), [20.0, 30.0, 60.0, 10.0]) |
| 1003 |
self.assertEqual(c.bbox(i), (18, 8, 62, 32)) |
| 1004 |
|
| 1005 |
self.assertRaises(TclError, c.coords, i, 20, 30, 60) |
| 1006 |
self.assertRaises(TclError, c.coords, i, [20, 30, 60]) |
| 1007 |
self.assertRaises(TclError, c.coords, i, 20, 30) |
| 1008 |
self.assertRaises(TclError, c.coords, i, [20, 30]) |
| 1009 |
|
| 1010 |
c.coords(i, '20', '30c', '60i', '10p') |
| 1011 |
coords = c.coords(i) |
| 1012 |
self.assertIsInstance(coords, list) |
| 1013 |
self.assertEqual(len(coords), 4) |
| 1014 |
self.assertEqual(coords[0], 20) |
| 1015 |
for i in range(4): |
| 1016 |
self.assertIsInstance(coords[i], float) |
| 1017 |
|
| 1018 |
@requires_tk(8, 6) |
| 1019 |
def test_moveto(self): |
| 1020 |
widget = self.create() |
| 1021 |
i1 = widget.create_rectangle(1, 1, 20, 20, tags='group') |
| 1022 |
i2 = widget.create_rectangle(30, 30, 50, 70, tags='group') |
| 1023 |
x1, y1, _, _ = widget.bbox(i1) |
| 1024 |
x2, y2, _, _ = widget.bbox(i2) |
| 1025 |
widget.moveto('group', 200, 100) |
| 1026 |
x1_2, y1_2, _, _ = widget.bbox(i1) |
| 1027 |
x2_2, y2_2, _, _ = widget.bbox(i2) |
| 1028 |
self.assertEqual(x1_2, 200) |
| 1029 |
self.assertEqual(y1_2, 100) |
| 1030 |
self.assertEqual(x2 - x1, x2_2 - x1_2) |
| 1031 |
self.assertEqual(y2 - y1, y2_2 - y1_2) |
| 1032 |
widget.tag_lower(i2, i1) |
| 1033 |
widget.moveto('group', y=50) |
| 1034 |
x1_3, y1_3, _, _ = widget.bbox(i1) |
| 1035 |
x2_3, y2_3, _, _ = widget.bbox(i2) |
| 1036 |
self.assertEqual(y2_3, 50) |
| 1037 |
self.assertEqual(x2_3, x2_2) |
| 1038 |
self.assertEqual(x2_2 - x1_2, x2_3 - x1_3) |
| 1039 |
self.assertEqual(y2_2 - y1_2, y2_3 - y1_3) |
| 1040 |
|
| 1041 |
|
| 1042 |
@add_configure_tests(IntegerSizeTests, StandardOptionsTests) |
| 1043 |
class ListboxTest(AbstractWidgetTest, unittest.TestCase): |
| 1044 |
OPTIONS = ( |
| 1045 |
'activestyle', 'background', 'borderwidth', 'cursor', |
| 1046 |
'disabledforeground', 'exportselection', |
| 1047 |
'font', 'foreground', 'height', |
| 1048 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 1049 |
'justify', 'listvariable', 'relief', |
| 1050 |
'selectbackground', 'selectborderwidth', 'selectforeground', |
| 1051 |
'selectmode', 'setgrid', 'state', |
| 1052 |
'takefocus', 'width', 'xscrollcommand', 'yscrollcommand', |
| 1053 |
) |
| 1054 |
if tk_version < (8, 7): |
| 1055 |
_clipped = {'highlightthickness'} |
| 1056 |
else: |
| 1057 |
_clipped = {'borderwidth', 'highlightthickness', 'selectborderwidth'} |
| 1058 |
|
| 1059 |
def create(self, **kwargs): |
| 1060 |
return tkinter.Listbox(self.root, **kwargs) |
| 1061 |
|
| 1062 |
def test_configure_activestyle(self): |
| 1063 |
widget = self.create() |
| 1064 |
self.checkEnumParam(widget, 'activestyle', |
| 1065 |
'dotbox', 'none', 'underline') |
| 1066 |
|
| 1067 |
test_configure_justify = requires_tk(8, 6, 5)(StandardOptionsTests.test_configure_justify) |
| 1068 |
|
| 1069 |
def test_configure_listvariable(self): |
| 1070 |
widget = self.create() |
| 1071 |
var = tkinter.DoubleVar(self.root) |
| 1072 |
self.checkVariableParam(widget, 'listvariable', var) |
| 1073 |
|
| 1074 |
def test_configure_selectmode(self): |
| 1075 |
widget = self.create() |
| 1076 |
self.checkParam(widget, 'selectmode', 'single') |
| 1077 |
self.checkParam(widget, 'selectmode', 'browse') |
| 1078 |
self.checkParam(widget, 'selectmode', 'multiple') |
| 1079 |
self.checkParam(widget, 'selectmode', 'extended') |
| 1080 |
|
| 1081 |
def test_configure_state(self): |
| 1082 |
widget = self.create() |
| 1083 |
self.checkEnumParam(widget, 'state', 'disabled', 'normal') |
| 1084 |
|
| 1085 |
def test_itemconfigure(self): |
| 1086 |
widget = self.create() |
| 1087 |
with self.assertRaisesRegex(TclError, 'item number "0" out of range'): |
| 1088 |
widget.itemconfigure(0) |
| 1089 |
colors = 'red orange yellow green blue white violet'.split() |
| 1090 |
widget.insert('end', *colors) |
| 1091 |
for i, color in enumerate(colors): |
| 1092 |
widget.itemconfigure(i, background=color) |
| 1093 |
with self.assertRaises(TypeError): |
| 1094 |
widget.itemconfigure() |
| 1095 |
with self.assertRaisesRegex(TclError, 'bad listbox index "red"'): |
| 1096 |
widget.itemconfigure('red') |
| 1097 |
if get_tk_patchlevel(self.root) >= (8, 6, 14): |
| 1098 |
prefix = ('background', '', '', '') |
| 1099 |
else: |
| 1100 |
prefix = ('background', 'background', 'Background', '') |
| 1101 |
self.assertEqual(widget.itemconfigure(0, 'background'), |
| 1102 |
(*prefix, 'red')) |
| 1103 |
self.assertEqual(widget.itemconfigure('end', 'background'), |
| 1104 |
(*prefix, 'violet')) |
| 1105 |
self.assertEqual(widget.itemconfigure('@0,0', 'background'), |
| 1106 |
(*prefix, 'red')) |
| 1107 |
|
| 1108 |
d = widget.itemconfigure(0) |
| 1109 |
self.assertIsInstance(d, dict) |
| 1110 |
for k, v in d.items(): |
| 1111 |
self.assertIn(len(v), (2, 5)) |
| 1112 |
if len(v) == 5: |
| 1113 |
self.assertEqual(v, widget.itemconfigure(0, k)) |
| 1114 |
self.assertEqual(v[4], widget.itemcget(0, k)) |
| 1115 |
|
| 1116 |
def check_itemconfigure(self, name, value): |
| 1117 |
widget = self.create() |
| 1118 |
widget.insert('end', 'a', 'b', 'c', 'd') |
| 1119 |
widget.itemconfigure(0, **{name: value}) |
| 1120 |
self.assertEqual(widget.itemconfigure(0, name)[4], value) |
| 1121 |
self.assertEqual(widget.itemcget(0, name), value) |
| 1122 |
with self.assertRaisesRegex(TclError, 'unknown color name "spam"'): |
| 1123 |
widget.itemconfigure(0, **{name: 'spam'}) |
| 1124 |
|
| 1125 |
def test_itemconfigure_background(self): |
| 1126 |
self.check_itemconfigure('background', '#ff0000') |
| 1127 |
|
| 1128 |
def test_itemconfigure_bg(self): |
| 1129 |
self.check_itemconfigure('bg', '#ff0000') |
| 1130 |
|
| 1131 |
def test_itemconfigure_fg(self): |
| 1132 |
self.check_itemconfigure('fg', '#110022') |
| 1133 |
|
| 1134 |
def test_itemconfigure_foreground(self): |
| 1135 |
self.check_itemconfigure('foreground', '#110022') |
| 1136 |
|
| 1137 |
def test_itemconfigure_selectbackground(self): |
| 1138 |
self.check_itemconfigure('selectbackground', '#110022') |
| 1139 |
|
| 1140 |
def test_itemconfigure_selectforeground(self): |
| 1141 |
self.check_itemconfigure('selectforeground', '#654321') |
| 1142 |
|
| 1143 |
def test_box(self): |
| 1144 |
lb = self.create() |
| 1145 |
lb.insert(0, *('el%d' % i for i in range(8))) |
| 1146 |
lb.pack() |
| 1147 |
self.assertIsBoundingBox(lb.bbox(0)) |
| 1148 |
self.assertIsNone(lb.bbox(-1)) |
| 1149 |
self.assertIsNone(lb.bbox(10)) |
| 1150 |
self.assertRaises(TclError, lb.bbox, 'noindex') |
| 1151 |
self.assertRaises(TclError, lb.bbox, None) |
| 1152 |
self.assertRaises(TypeError, lb.bbox) |
| 1153 |
self.assertRaises(TypeError, lb.bbox, 0, 1) |
| 1154 |
|
| 1155 |
def test_curselection(self): |
| 1156 |
lb = self.create() |
| 1157 |
lb.insert(0, *('el%d' % i for i in range(8))) |
| 1158 |
lb.selection_clear(0, tkinter.END) |
| 1159 |
lb.selection_set(2, 4) |
| 1160 |
lb.selection_set(6) |
| 1161 |
self.assertEqual(lb.curselection(), (2, 3, 4, 6)) |
| 1162 |
self.assertRaises(TypeError, lb.curselection, 0) |
| 1163 |
|
| 1164 |
def test_get(self): |
| 1165 |
lb = self.create() |
| 1166 |
lb.insert(0, *('el%d' % i for i in range(8))) |
| 1167 |
self.assertEqual(lb.get(0), 'el0') |
| 1168 |
self.assertEqual(lb.get(3), 'el3') |
| 1169 |
self.assertEqual(lb.get('end'), 'el7') |
| 1170 |
self.assertEqual(lb.get(8), '') |
| 1171 |
self.assertEqual(lb.get(-1), '') |
| 1172 |
self.assertEqual(lb.get(3, 5), ('el3', 'el4', 'el5')) |
| 1173 |
self.assertEqual(lb.get(5, 'end'), ('el5', 'el6', 'el7')) |
| 1174 |
self.assertEqual(lb.get(5, 0), ()) |
| 1175 |
self.assertEqual(lb.get(0, 0), ('el0',)) |
| 1176 |
self.assertRaises(TclError, lb.get, 'noindex') |
| 1177 |
self.assertRaises(TclError, lb.get, None) |
| 1178 |
self.assertRaises(TypeError, lb.get) |
| 1179 |
self.assertRaises(TclError, lb.get, 'end', 'noindex') |
| 1180 |
self.assertRaises(TypeError, lb.get, 1, 2, 3) |
| 1181 |
self.assertRaises(TclError, lb.get, 2.4) |
| 1182 |
|
| 1183 |
|
| 1184 |
@add_configure_tests(PixelSizeTests, StandardOptionsTests) |
| 1185 |
class ScaleTest(AbstractWidgetTest, unittest.TestCase): |
| 1186 |
OPTIONS = ( |
| 1187 |
'activebackground', 'background', 'bigincrement', 'borderwidth', |
| 1188 |
'command', 'cursor', 'digits', 'font', 'foreground', 'from', |
| 1189 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 1190 |
'label', 'length', 'orient', 'relief', |
| 1191 |
'repeatdelay', 'repeatinterval', |
| 1192 |
'resolution', 'showvalue', 'sliderlength', 'sliderrelief', 'state', |
| 1193 |
'takefocus', 'tickinterval', 'to', 'troughcolor', 'variable', 'width', |
| 1194 |
) |
| 1195 |
_clipped = {'highlightthickness'} |
| 1196 |
default_orient = 'vertical' |
| 1197 |
|
| 1198 |
def create(self, **kwargs): |
| 1199 |
return tkinter.Scale(self.root, **kwargs) |
| 1200 |
|
| 1201 |
def test_configure_bigincrement(self): |
| 1202 |
widget = self.create() |
| 1203 |
self.checkFloatParam(widget, 'bigincrement', 12.4, 23.6, -5) |
| 1204 |
|
| 1205 |
def test_configure_digits(self): |
| 1206 |
widget = self.create() |
| 1207 |
self.checkIntegerParam(widget, 'digits', 5, 0) |
| 1208 |
|
| 1209 |
def test_configure_from(self): |
| 1210 |
widget = self.create() |
| 1211 |
conv = float if get_tk_patchlevel(self.root) >= (8, 6, 10) else float_round |
| 1212 |
self.checkFloatParam(widget, 'from', 100, 14.9, 15.1, conv=conv) |
| 1213 |
|
| 1214 |
def test_configure_label(self): |
| 1215 |
widget = self.create() |
| 1216 |
self.checkParam(widget, 'label', 'any string') |
| 1217 |
self.checkParam(widget, 'label', '') |
| 1218 |
|
| 1219 |
def test_configure_length(self): |
| 1220 |
widget = self.create() |
| 1221 |
self.checkPixelsParam(widget, 'length', 130, 131.2, 135.6, '5i') |
| 1222 |
|
| 1223 |
def test_configure_resolution(self): |
| 1224 |
widget = self.create() |
| 1225 |
self.checkFloatParam(widget, 'resolution', 4.2, 0, 6.7, -2) |
| 1226 |
|
| 1227 |
def test_configure_showvalue(self): |
| 1228 |
widget = self.create() |
| 1229 |
self.checkBooleanParam(widget, 'showvalue') |
| 1230 |
|
| 1231 |
def test_configure_sliderlength(self): |
| 1232 |
widget = self.create() |
| 1233 |
self.checkPixelsParam(widget, 'sliderlength', |
| 1234 |
10, 11.2, 15.6, -3, '3m') |
| 1235 |
|
| 1236 |
def test_configure_sliderrelief(self): |
| 1237 |
widget = self.create() |
| 1238 |
self.checkReliefParam(widget, 'sliderrelief') |
| 1239 |
|
| 1240 |
def test_configure_tickinterval(self): |
| 1241 |
widget = self.create() |
| 1242 |
self.checkFloatParam(widget, 'tickinterval', 1, 4.3, 7.6, 0, |
| 1243 |
conv=float_round) |
| 1244 |
self.checkParam(widget, 'tickinterval', -2, expected=2, |
| 1245 |
conv=float_round) |
| 1246 |
|
| 1247 |
def test_configure_to(self): |
| 1248 |
widget = self.create() |
| 1249 |
self.checkFloatParam(widget, 'to', 300, 14.9, 15.1, -10, |
| 1250 |
conv=float_round) |
| 1251 |
|
| 1252 |
|
| 1253 |
@add_configure_tests(PixelSizeTests, StandardOptionsTests) |
| 1254 |
class ScrollbarTest(AbstractWidgetTest, unittest.TestCase): |
| 1255 |
OPTIONS = ( |
| 1256 |
'activebackground', 'activerelief', |
| 1257 |
'background', 'borderwidth', |
| 1258 |
'command', 'cursor', 'elementborderwidth', |
| 1259 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 1260 |
'jump', 'orient', 'relief', |
| 1261 |
'repeatdelay', 'repeatinterval', |
| 1262 |
'takefocus', 'troughcolor', 'width', |
| 1263 |
) |
| 1264 |
if tk_version < (8, 7): |
| 1265 |
_clipped = {'highlightthickness'} |
| 1266 |
elif tk_version < (9, 0): |
| 1267 |
_clipped = {'borderwidth', 'elementborderwidth', 'highlightthickness'} |
| 1268 |
else: |
| 1269 |
_clipped = {'borderwidth', 'elementborderwidth', 'highlightthickness', 'width'} |
| 1270 |
_clipped_to_default = {'elementborderwidth'} |
| 1271 |
_stringify = True |
| 1272 |
default_orient = 'vertical' |
| 1273 |
|
| 1274 |
def create(self, **kwargs): |
| 1275 |
return tkinter.Scrollbar(self.root, **kwargs) |
| 1276 |
|
| 1277 |
def test_configure_elementborderwidth(self): |
| 1278 |
widget = self.create() |
| 1279 |
self.checkPixelsParam(widget, 'elementborderwidth', 4.3, 5.6, -2, '1m') |
| 1280 |
|
| 1281 |
def test_configure_orient(self): |
| 1282 |
widget = self.create() |
| 1283 |
self.checkEnumParam(widget, 'orient', 'vertical', 'horizontal', |
| 1284 |
fullname='orientation', allow_empty=True) |
| 1285 |
|
| 1286 |
def test_activate(self): |
| 1287 |
sb = self.create() |
| 1288 |
for e in ('arrow1', 'slider', 'arrow2'): |
| 1289 |
sb.activate(e) |
| 1290 |
self.assertEqual(sb.activate(), e) |
| 1291 |
sb.activate('') |
| 1292 |
self.assertIsNone(sb.activate()) |
| 1293 |
self.assertRaises(TypeError, sb.activate, 'arrow1', 'arrow2') |
| 1294 |
|
| 1295 |
def test_set(self): |
| 1296 |
sb = self.create() |
| 1297 |
sb.set(0.2, 0.4) |
| 1298 |
self.assertEqual(sb.get(), (0.2, 0.4)) |
| 1299 |
self.assertRaises(TclError, sb.set, 'abc', 'def') |
| 1300 |
self.assertRaises(TclError, sb.set, 0.6, 'def') |
| 1301 |
self.assertRaises(TclError, sb.set, 0.6, None) |
| 1302 |
self.assertRaises(TypeError, sb.set, 0.6) |
| 1303 |
self.assertRaises(TypeError, sb.set, 0.6, 0.7, 0.8) |
| 1304 |
|
| 1305 |
|
| 1306 |
@add_configure_tests(PixelSizeTests, StandardOptionsTests) |
| 1307 |
class PanedWindowTest(AbstractWidgetTest, unittest.TestCase): |
| 1308 |
OPTIONS = ( |
| 1309 |
'background', 'borderwidth', 'cursor', |
| 1310 |
'handlepad', 'handlesize', 'height', |
| 1311 |
'opaqueresize', 'orient', |
| 1312 |
'proxybackground', 'proxyborderwidth', 'proxyrelief', |
| 1313 |
'relief', |
| 1314 |
'sashcursor', 'sashpad', 'sashrelief', 'sashwidth', |
| 1315 |
'showhandle', 'width', |
| 1316 |
) |
| 1317 |
_no_round = {'handlesize', 'height', 'proxyborderwidth', 'sashwidth', |
| 1318 |
'selectborderwidth', 'width'} |
| 1319 |
_clipped = {} |
| 1320 |
default_orient = 'horizontal' |
| 1321 |
|
| 1322 |
def create(self, **kwargs): |
| 1323 |
return tkinter.PanedWindow(self.root, **kwargs) |
| 1324 |
|
| 1325 |
def test_configure_handlepad(self): |
| 1326 |
widget = self.create() |
| 1327 |
self.checkPixelsParam(widget, 'handlepad', 5, 6.4, 7.6, -3, '1m') |
| 1328 |
|
| 1329 |
def test_configure_handlesize(self): |
| 1330 |
widget = self.create() |
| 1331 |
self.checkPixelsParam(widget, 'handlesize', 8, 9.4, 10.6, -3, '2m') |
| 1332 |
|
| 1333 |
def test_configure_opaqueresize(self): |
| 1334 |
widget = self.create() |
| 1335 |
self.checkBooleanParam(widget, 'opaqueresize') |
| 1336 |
|
| 1337 |
@requires_tk(8, 6, 5) |
| 1338 |
def test_configure_proxybackground(self): |
| 1339 |
widget = self.create() |
| 1340 |
self.checkColorParam(widget, 'proxybackground') |
| 1341 |
|
| 1342 |
@requires_tk(8, 6, 5) |
| 1343 |
def test_configure_proxyborderwidth(self): |
| 1344 |
widget = self.create() |
| 1345 |
self.checkPixelsParam(widget, 'proxyborderwidth', |
| 1346 |
0, 1.3, 2.9, 6, -2, '10p') |
| 1347 |
|
| 1348 |
@requires_tk(8, 6, 5) |
| 1349 |
def test_configure_proxyrelief(self): |
| 1350 |
widget = self.create() |
| 1351 |
self.checkReliefParam(widget, 'proxyrelief', |
| 1352 |
allow_empty=(tk_version >= (8, 7))) |
| 1353 |
|
| 1354 |
def test_configure_sashcursor(self): |
| 1355 |
widget = self.create() |
| 1356 |
self.checkCursorParam(widget, 'sashcursor') |
| 1357 |
|
| 1358 |
def test_configure_sashpad(self): |
| 1359 |
widget = self.create() |
| 1360 |
self.checkPixelsParam(widget, 'sashpad', 8, 1.3, 2.6, -2, '2m') |
| 1361 |
|
| 1362 |
def test_configure_sashrelief(self): |
| 1363 |
widget = self.create() |
| 1364 |
self.checkReliefParam(widget, 'sashrelief') |
| 1365 |
|
| 1366 |
def test_configure_sashwidth(self): |
| 1367 |
widget = self.create() |
| 1368 |
self.checkPixelsParam(widget, 'sashwidth', 10, 11.1, 15.6, -3, '1m') |
| 1369 |
|
| 1370 |
def test_configure_showhandle(self): |
| 1371 |
widget = self.create() |
| 1372 |
self.checkBooleanParam(widget, 'showhandle') |
| 1373 |
|
| 1374 |
def create2(self): |
| 1375 |
p = self.create() |
| 1376 |
b = tkinter.Button(p) |
| 1377 |
c = tkinter.Button(p) |
| 1378 |
p.add(b) |
| 1379 |
p.add(c) |
| 1380 |
return p, b, c |
| 1381 |
|
| 1382 |
def test_paneconfigure(self): |
| 1383 |
p, b, c = self.create2() |
| 1384 |
self.assertRaises(TypeError, p.paneconfigure) |
| 1385 |
d = p.paneconfigure(b) |
| 1386 |
self.assertIsInstance(d, dict) |
| 1387 |
for k, v in d.items(): |
| 1388 |
self.assertEqual(len(v), 5) |
| 1389 |
self.assertEqual(v, p.paneconfigure(b, k)) |
| 1390 |
self.assertEqual(v[4], p.panecget(b, k)) |
| 1391 |
|
| 1392 |
def check_paneconfigure(self, p, b, name, value, expected): |
| 1393 |
if not self.wantobjects: |
| 1394 |
expected = str(expected) |
| 1395 |
p.paneconfigure(b, **{name: value}) |
| 1396 |
self.assertEqual(p.paneconfigure(b, name)[4], expected) |
| 1397 |
self.assertEqual(p.panecget(b, name), expected) |
| 1398 |
|
| 1399 |
def check_paneconfigure_bad(self, p, b, name, msg): |
| 1400 |
with self.assertRaisesRegex(TclError, msg): |
| 1401 |
p.paneconfigure(b, **{name: 'badValue'}) |
| 1402 |
|
| 1403 |
def test_paneconfigure_after(self): |
| 1404 |
p, b, c = self.create2() |
| 1405 |
self.check_paneconfigure(p, b, 'after', c, str(c)) |
| 1406 |
self.check_paneconfigure_bad(p, b, 'after', |
| 1407 |
'bad window path name "badValue"') |
| 1408 |
|
| 1409 |
def test_paneconfigure_before(self): |
| 1410 |
p, b, c = self.create2() |
| 1411 |
self.check_paneconfigure(p, b, 'before', c, str(c)) |
| 1412 |
self.check_paneconfigure_bad(p, b, 'before', |
| 1413 |
'bad window path name "badValue"') |
| 1414 |
|
| 1415 |
def test_paneconfigure_height(self): |
| 1416 |
p, b, c = self.create2() |
| 1417 |
self.check_paneconfigure(p, b, 'height', 10, 10) |
| 1418 |
self.check_paneconfigure_bad(p, b, 'height', |
| 1419 |
EXPECTED_SCREEN_DISTANCE_OR_EMPTY_ERRMSG.format('badValue')) |
| 1420 |
|
| 1421 |
def test_paneconfigure_hide(self): |
| 1422 |
p, b, c = self.create2() |
| 1423 |
self.check_paneconfigure(p, b, 'hide', False, 0) |
| 1424 |
self.check_paneconfigure_bad(p, b, 'hide', |
| 1425 |
'expected boolean value but got "badValue"') |
| 1426 |
|
| 1427 |
def test_paneconfigure_minsize(self): |
| 1428 |
p, b, c = self.create2() |
| 1429 |
self.check_paneconfigure(p, b, 'minsize', 10, 10) |
| 1430 |
self.check_paneconfigure_bad(p, b, 'minsize', |
| 1431 |
EXPECTED_SCREEN_DISTANCE_ERRMSG.format('badValue')) |
| 1432 |
|
| 1433 |
def test_paneconfigure_padx(self): |
| 1434 |
p, b, c = self.create2() |
| 1435 |
self.check_paneconfigure(p, b, 'padx', 1.3, 1 if tk_version < (9, 0) else 1.3) |
| 1436 |
self.check_paneconfigure_bad(p, b, 'padx', |
| 1437 |
EXPECTED_SCREEN_DISTANCE_ERRMSG.format('badValue')) |
| 1438 |
|
| 1439 |
def test_paneconfigure_pady(self): |
| 1440 |
p, b, c = self.create2() |
| 1441 |
self.check_paneconfigure(p, b, 'pady', 1.3, 1 if tk_version < (9, 0) else 1.3) |
| 1442 |
self.check_paneconfigure_bad(p, b, 'pady', |
| 1443 |
EXPECTED_SCREEN_DISTANCE_ERRMSG.format('badValue')) |
| 1444 |
|
| 1445 |
def test_paneconfigure_sticky(self): |
| 1446 |
p, b, c = self.create2() |
| 1447 |
self.check_paneconfigure(p, b, 'sticky', 'nsew', 'nesw') |
| 1448 |
self.check_paneconfigure_bad(p, b, 'sticky', |
| 1449 |
'bad stickyness value "badValue": must ' |
| 1450 |
'be a string containing zero or more of ' |
| 1451 |
'n, e, s, and w') |
| 1452 |
|
| 1453 |
def test_paneconfigure_stretch(self): |
| 1454 |
p, b, c = self.create2() |
| 1455 |
self.check_paneconfigure(p, b, 'stretch', 'alw', 'always') |
| 1456 |
self.check_paneconfigure_bad(p, b, 'stretch', |
| 1457 |
'bad stretch "badValue": must be ' |
| 1458 |
'always, first, last, middle, or never') |
| 1459 |
|
| 1460 |
def test_paneconfigure_width(self): |
| 1461 |
p, b, c = self.create2() |
| 1462 |
self.check_paneconfigure(p, b, 'width', 10, 10) |
| 1463 |
self.check_paneconfigure_bad(p, b, 'width', |
| 1464 |
EXPECTED_SCREEN_DISTANCE_OR_EMPTY_ERRMSG.format('badValue')) |
| 1465 |
|
| 1466 |
|
| 1467 |
@add_configure_tests(StandardOptionsTests) |
| 1468 |
class MenuTest(AbstractWidgetTest, unittest.TestCase): |
| 1469 |
OPTIONS = ( |
| 1470 |
'activebackground', 'activeborderwidth', 'activeforeground', |
| 1471 |
'activerelief', 'background', 'borderwidth', 'cursor', |
| 1472 |
'disabledforeground', 'font', 'foreground', |
| 1473 |
'postcommand', 'relief', 'selectcolor', 'takefocus', |
| 1474 |
'tearoff', 'tearoffcommand', 'title', 'type', |
| 1475 |
) |
| 1476 |
_rounds_pixels = False |
| 1477 |
_clipped = {} |
| 1478 |
|
| 1479 |
def create(self, **kwargs): |
| 1480 |
return tkinter.Menu(self.root, **kwargs) |
| 1481 |
|
| 1482 |
def test_indexcommand_none(self): |
| 1483 |
widget = self.create() |
| 1484 |
i = widget.index('none') |
| 1485 |
self.assertIsNone(i) |
| 1486 |
|
| 1487 |
test_configure_activerelief = requires_tk(8, 7)(StandardOptionsTests.test_configure_activerelief) |
| 1488 |
|
| 1489 |
def test_configure_postcommand(self): |
| 1490 |
widget = self.create() |
| 1491 |
self.checkCommandParam(widget, 'postcommand') |
| 1492 |
|
| 1493 |
def test_configure_tearoff(self): |
| 1494 |
widget = self.create() |
| 1495 |
self.checkBooleanParam(widget, 'tearoff') |
| 1496 |
|
| 1497 |
def test_configure_tearoffcommand(self): |
| 1498 |
widget = self.create() |
| 1499 |
self.checkCommandParam(widget, 'tearoffcommand') |
| 1500 |
|
| 1501 |
def test_configure_title(self): |
| 1502 |
widget = self.create() |
| 1503 |
self.checkParam(widget, 'title', 'any string') |
| 1504 |
|
| 1505 |
def test_configure_type(self): |
| 1506 |
widget = self.create() |
| 1507 |
values = ('normal', 'tearoff', 'menubar') |
| 1508 |
self.checkEnumParam(widget, 'type', *values, |
| 1509 |
allow_empty=tk_version < (8, 7), |
| 1510 |
sort=tk_version >= (8, 7)) |
| 1511 |
|
| 1512 |
def test_entryconfigure(self): |
| 1513 |
m1 = self.create() |
| 1514 |
m1.add_command(label='test') |
| 1515 |
self.assertRaises(TypeError, m1.entryconfigure) |
| 1516 |
with self.assertRaisesRegex(TclError, 'bad menu entry index "foo"'): |
| 1517 |
m1.entryconfigure('foo') |
| 1518 |
d = m1.entryconfigure(1) |
| 1519 |
self.assertIsInstance(d, dict) |
| 1520 |
for k, v in d.items(): |
| 1521 |
self.assertIsInstance(k, str) |
| 1522 |
self.assertIsInstance(v, tuple) |
| 1523 |
self.assertEqual(len(v), 5) |
| 1524 |
self.assertEqual(v[0], k) |
| 1525 |
self.assertEqual(m1.entrycget(1, k), v[4]) |
| 1526 |
m1.destroy() |
| 1527 |
|
| 1528 |
def test_entryconfigure_label(self): |
| 1529 |
m1 = self.create() |
| 1530 |
m1.add_command(label='test') |
| 1531 |
self.assertEqual(m1.entrycget(1, 'label'), 'test') |
| 1532 |
m1.entryconfigure(1, label='changed') |
| 1533 |
self.assertEqual(m1.entrycget(1, 'label'), 'changed') |
| 1534 |
|
| 1535 |
def test_entryconfigure_variable(self): |
| 1536 |
m1 = self.create() |
| 1537 |
v1 = tkinter.BooleanVar(self.root) |
| 1538 |
v2 = tkinter.BooleanVar(self.root) |
| 1539 |
m1.add_checkbutton(variable=v1, onvalue=True, offvalue=False, |
| 1540 |
label='Nonsense') |
| 1541 |
self.assertEqual(str(m1.entrycget(1, 'variable')), str(v1)) |
| 1542 |
m1.entryconfigure(1, variable=v2) |
| 1543 |
self.assertEqual(str(m1.entrycget(1, 'variable')), str(v2)) |
| 1544 |
|
| 1545 |
|
| 1546 |
@add_configure_tests(PixelSizeTests, StandardOptionsTests) |
| 1547 |
class MessageTest(AbstractWidgetTest, unittest.TestCase): |
| 1548 |
OPTIONS = ( |
| 1549 |
'anchor', 'aspect', 'background', 'borderwidth', |
| 1550 |
'cursor', 'font', 'foreground', |
| 1551 |
'highlightbackground', 'highlightcolor', 'highlightthickness', |
| 1552 |
'justify', 'padx', 'pady', 'relief', |
| 1553 |
'takefocus', 'text', 'textvariable', 'width', |
| 1554 |
) |
| 1555 |
_no_round = {'padx', 'pady'} |
| 1556 |
if tk_version < (8, 7): |
| 1557 |
_clipped = {'highlightthickness'} |
| 1558 |
else: |
| 1559 |
_clipped = {'borderwidth', 'highlightthickness', 'padx', 'pady', 'width'} |
| 1560 |
_clipped_to_default = {'padx', 'pady'} |
| 1561 |
|
| 1562 |
def create(self, **kwargs): |
| 1563 |
return tkinter.Message(self.root, **kwargs) |
| 1564 |
|
| 1565 |
def test_configure_aspect(self): |
| 1566 |
widget = self.create() |
| 1567 |
self.checkIntegerParam(widget, 'aspect', 250, 0, -300) |
| 1568 |
|
| 1569 |
|
| 1570 |
class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase): |
| 1571 |
|
| 1572 |
def test_frame(self): |
| 1573 |
self._test_widget(tkinter.Frame) |
| 1574 |
|
| 1575 |
def test_label(self): |
| 1576 |
self._test_widget(tkinter.Label) |
| 1577 |
|
| 1578 |
|
| 1579 |
if __name__ == '__main__': |
| 1580 |
unittest.main() |