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