| 1 |
import functools |
| 2 |
import unittest |
| 3 |
import tkinter |
| 4 |
import enum |
| 5 |
from test import support |
| 6 |
from tkinter.test.support import AbstractTkTest, AbstractDefaultRootTest |
| 7 |
|
| 8 |
support.requires('gui') |
| 9 |
|
| 10 |
class MiscTest(AbstractTkTest, unittest.TestCase): |
| 11 |
|
| 12 |
def test_all(self): |
| 13 |
self.assertIn("Widget", tkinter.__all__) |
| 14 |
|
| 15 |
self.assertIn("CASCADE", tkinter.__all__) |
| 16 |
self.assertIsNotNone(tkinter.CASCADE) |
| 17 |
|
| 18 |
self.assertNotIn("re", tkinter.__all__) |
| 19 |
self.assertNotIn("sys", tkinter.__all__) |
| 20 |
self.assertNotIn("constants", tkinter.__all__) |
| 21 |
|
| 22 |
self.assertNotIn("_tkerror", tkinter.__all__) |
| 23 |
|
| 24 |
self.assertNotIn("wantobjects", tkinter.__all__) |
| 25 |
|
| 26 |
def test_repr(self): |
| 27 |
t = tkinter.Toplevel(self.root, name='top') |
| 28 |
f = tkinter.Frame(t, name='child') |
| 29 |
self.assertEqual(repr(f), '<tkinter.Frame object .top.child>') |
| 30 |
|
| 31 |
def test_generated_names(self): |
| 32 |
t = tkinter.Toplevel(self.root) |
| 33 |
f = tkinter.Frame(t) |
| 34 |
f2 = tkinter.Frame(t) |
| 35 |
b = tkinter.Button(f2) |
| 36 |
for name in str(b).split('.'): |
| 37 |
self.assertFalse(name.isidentifier(), msg=repr(name)) |
| 38 |
|
| 39 |
def test_tk_setPalette(self): |
| 40 |
root = self.root |
| 41 |
root.tk_setPalette('black') |
| 42 |
self.assertEqual(root['background'], 'black') |
| 43 |
root.tk_setPalette('white') |
| 44 |
self.assertEqual(root['background'], 'white') |
| 45 |
self.assertRaisesRegex(tkinter.TclError, |
| 46 |
'^unknown color name "spam"$', |
| 47 |
root.tk_setPalette, 'spam') |
| 48 |
|
| 49 |
root.tk_setPalette(background='black') |
| 50 |
self.assertEqual(root['background'], 'black') |
| 51 |
root.tk_setPalette(background='blue', highlightColor='yellow') |
| 52 |
self.assertEqual(root['background'], 'blue') |
| 53 |
self.assertEqual(root['highlightcolor'], 'yellow') |
| 54 |
root.tk_setPalette(background='yellow', highlightColor='blue') |
| 55 |
self.assertEqual(root['background'], 'yellow') |
| 56 |
self.assertEqual(root['highlightcolor'], 'blue') |
| 57 |
self.assertRaisesRegex(tkinter.TclError, |
| 58 |
'^unknown color name "spam"$', |
| 59 |
root.tk_setPalette, background='spam') |
| 60 |
self.assertRaisesRegex(tkinter.TclError, |
| 61 |
'^must specify a background color$', |
| 62 |
root.tk_setPalette, spam='white') |
| 63 |
self.assertRaisesRegex(tkinter.TclError, |
| 64 |
'^must specify a background color$', |
| 65 |
root.tk_setPalette, highlightColor='blue') |
| 66 |
|
| 67 |
def test_after(self): |
| 68 |
root = self.root |
| 69 |
|
| 70 |
def callback(start=0, step=1): |
| 71 |
nonlocal count |
| 72 |
count = start + step |
| 73 |
|
| 74 |
|
| 75 |
self.assertIsNone(root.after(1)) |
| 76 |
|
| 77 |
|
| 78 |
count = 0 |
| 79 |
timer1 = root.after(0, callback) |
| 80 |
self.assertIn(timer1, root.tk.call('after', 'info')) |
| 81 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) |
| 82 |
root.update() |
| 83 |
self.assertEqual(count, 1) |
| 84 |
with self.assertRaises(tkinter.TclError): |
| 85 |
root.tk.call(script) |
| 86 |
|
| 87 |
|
| 88 |
count = 0 |
| 89 |
timer1 = root.after(0, callback, 42, 11) |
| 90 |
root.update() |
| 91 |
self.assertEqual(count, 53) |
| 92 |
|
| 93 |
|
| 94 |
timer1 = root.after(1000, callback) |
| 95 |
self.assertIn(timer1, root.tk.call('after', 'info')) |
| 96 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) |
| 97 |
root.after_cancel(timer1) |
| 98 |
self.assertEqual(count, 53) |
| 99 |
with self.assertRaises(tkinter.TclError): |
| 100 |
root.tk.call(script) |
| 101 |
|
| 102 |
|
| 103 |
count = 0 |
| 104 |
timer1 = root.after(0, functools.partial(callback, 42, 11)) |
| 105 |
root.update() |
| 106 |
self.assertEqual(count, 53) |
| 107 |
|
| 108 |
def test_after_idle(self): |
| 109 |
root = self.root |
| 110 |
|
| 111 |
def callback(start=0, step=1): |
| 112 |
nonlocal count |
| 113 |
count = start + step |
| 114 |
|
| 115 |
|
| 116 |
count = 0 |
| 117 |
idle1 = root.after_idle(callback) |
| 118 |
self.assertIn(idle1, root.tk.call('after', 'info')) |
| 119 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) |
| 120 |
root.update_idletasks() |
| 121 |
self.assertEqual(count, 1) |
| 122 |
with self.assertRaises(tkinter.TclError): |
| 123 |
root.tk.call(script) |
| 124 |
|
| 125 |
|
| 126 |
count = 0 |
| 127 |
idle1 = root.after_idle(callback, 42, 11) |
| 128 |
root.update_idletasks() |
| 129 |
self.assertEqual(count, 53) |
| 130 |
|
| 131 |
|
| 132 |
idle1 = root.after_idle(callback) |
| 133 |
self.assertIn(idle1, root.tk.call('after', 'info')) |
| 134 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) |
| 135 |
root.after_cancel(idle1) |
| 136 |
self.assertEqual(count, 53) |
| 137 |
with self.assertRaises(tkinter.TclError): |
| 138 |
root.tk.call(script) |
| 139 |
|
| 140 |
def test_after_cancel(self): |
| 141 |
root = self.root |
| 142 |
|
| 143 |
def callback(): |
| 144 |
nonlocal count |
| 145 |
count += 1 |
| 146 |
|
| 147 |
timer1 = root.after(5000, callback) |
| 148 |
idle1 = root.after_idle(callback) |
| 149 |
|
| 150 |
|
| 151 |
with self.assertRaises(ValueError): |
| 152 |
root.after_cancel(None) |
| 153 |
|
| 154 |
|
| 155 |
count = 0 |
| 156 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) |
| 157 |
root.tk.call(script) |
| 158 |
self.assertEqual(count, 1) |
| 159 |
root.after_cancel(timer1) |
| 160 |
with self.assertRaises(tkinter.TclError): |
| 161 |
root.tk.call(script) |
| 162 |
self.assertEqual(count, 1) |
| 163 |
with self.assertRaises(tkinter.TclError): |
| 164 |
root.tk.call('after', 'info', timer1) |
| 165 |
|
| 166 |
|
| 167 |
root.after_cancel(timer1) |
| 168 |
|
| 169 |
|
| 170 |
count = 0 |
| 171 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) |
| 172 |
root.tk.call(script) |
| 173 |
self.assertEqual(count, 1) |
| 174 |
root.after_cancel(idle1) |
| 175 |
with self.assertRaises(tkinter.TclError): |
| 176 |
root.tk.call(script) |
| 177 |
self.assertEqual(count, 1) |
| 178 |
with self.assertRaises(tkinter.TclError): |
| 179 |
root.tk.call('after', 'info', idle1) |
| 180 |
|
| 181 |
def test_clipboard(self): |
| 182 |
root = self.root |
| 183 |
root.clipboard_clear() |
| 184 |
root.clipboard_append('Ùñî') |
| 185 |
self.assertEqual(root.clipboard_get(), 'Ùñî') |
| 186 |
root.clipboard_append('çōđě') |
| 187 |
self.assertEqual(root.clipboard_get(), 'Ùñîçōđě') |
| 188 |
root.clipboard_clear() |
| 189 |
with self.assertRaises(tkinter.TclError): |
| 190 |
root.clipboard_get() |
| 191 |
|
| 192 |
def test_clipboard_astral(self): |
| 193 |
root = self.root |
| 194 |
root.clipboard_clear() |
| 195 |
root.clipboard_append('𝔘𝔫𝔦') |
| 196 |
self.assertEqual(root.clipboard_get(), '𝔘𝔫𝔦') |
| 197 |
root.clipboard_append('𝔠𝔬𝔡𝔢') |
| 198 |
self.assertEqual(root.clipboard_get(), '𝔘𝔫𝔦𝔠𝔬𝔡𝔢') |
| 199 |
root.clipboard_clear() |
| 200 |
with self.assertRaises(tkinter.TclError): |
| 201 |
root.clipboard_get() |
| 202 |
|
| 203 |
def test_winfo_rgb(self): |
| 204 |
|
| 205 |
def assertApprox(col1, col2): |
| 206 |
|
| 207 |
|
| 208 |
for col1_channel, col2_channel in zip(col1, col2): |
| 209 |
self.assertAlmostEqual(col1_channel, col2_channel, delta=33) |
| 210 |
|
| 211 |
root = self.root |
| 212 |
rgb = root.winfo_rgb |
| 213 |
|
| 214 |
|
| 215 |
self.assertEqual(rgb('red'), (65535, 0, 0)) |
| 216 |
self.assertEqual(rgb('dark slate blue'), (18504, 15677, 35723)) |
| 217 |
|
| 218 |
self.assertEqual(rgb('#F0F'), (0xFFFF, 0x0000, 0xFFFF)) |
| 219 |
|
| 220 |
assertApprox(rgb('#4a3c8c'), (0x4a4a, 0x3c3c, 0x8c8c)) |
| 221 |
|
| 222 |
assertApprox(rgb('#dede14143939'), (0xdede, 0x1414, 0x3939)) |
| 223 |
|
| 224 |
with self.assertRaises(tkinter.TclError): |
| 225 |
rgb('#123456789a') |
| 226 |
|
| 227 |
with self.assertRaises(tkinter.TclError): |
| 228 |
rgb((111, 78, 55)) |
| 229 |
|
| 230 |
def test_winfo_pathname(self): |
| 231 |
t = tkinter.Toplevel(self.root) |
| 232 |
w = tkinter.Button(t) |
| 233 |
wid = w.winfo_id() |
| 234 |
self.assertIsInstance(wid, int) |
| 235 |
self.assertEqual(self.root.winfo_pathname(hex(wid)), str(w)) |
| 236 |
self.assertEqual(self.root.winfo_pathname(hex(wid), displayof=None), str(w)) |
| 237 |
self.assertEqual(self.root.winfo_pathname(hex(wid), displayof=t), str(w)) |
| 238 |
self.assertEqual(self.root.winfo_pathname(wid), str(w)) |
| 239 |
self.assertEqual(self.root.winfo_pathname(wid, displayof=None), str(w)) |
| 240 |
self.assertEqual(self.root.winfo_pathname(wid, displayof=t), str(w)) |
| 241 |
|
| 242 |
def test_event_repr_defaults(self): |
| 243 |
e = tkinter.Event() |
| 244 |
e.serial = 12345 |
| 245 |
e.num = '??' |
| 246 |
e.height = '??' |
| 247 |
e.keycode = '??' |
| 248 |
e.state = 0 |
| 249 |
e.time = 123456789 |
| 250 |
e.width = '??' |
| 251 |
e.x = '??' |
| 252 |
e.y = '??' |
| 253 |
e.char = '' |
| 254 |
e.keysym = '??' |
| 255 |
e.keysym_num = '??' |
| 256 |
e.type = '100' |
| 257 |
e.widget = '??' |
| 258 |
e.x_root = '??' |
| 259 |
e.y_root = '??' |
| 260 |
e.delta = 0 |
| 261 |
self.assertEqual(repr(e), '<100 event>') |
| 262 |
|
| 263 |
def test_event_repr(self): |
| 264 |
e = tkinter.Event() |
| 265 |
e.serial = 12345 |
| 266 |
e.num = 3 |
| 267 |
e.focus = True |
| 268 |
e.height = 200 |
| 269 |
e.keycode = 65 |
| 270 |
e.state = 0x30405 |
| 271 |
e.time = 123456789 |
| 272 |
e.width = 300 |
| 273 |
e.x = 10 |
| 274 |
e.y = 20 |
| 275 |
e.char = 'A' |
| 276 |
e.send_event = True |
| 277 |
e.keysym = 'Key-A' |
| 278 |
e.keysym_num = ord('A') |
| 279 |
e.type = tkinter.EventType.Configure |
| 280 |
e.widget = '.text' |
| 281 |
e.x_root = 1010 |
| 282 |
e.y_root = 1020 |
| 283 |
e.delta = -1 |
| 284 |
self.assertEqual(repr(e), |
| 285 |
"<Configure event send_event=True" |
| 286 |
" state=Shift|Control|Button3|0x30000" |
| 287 |
" keysym=Key-A keycode=65 char='A'" |
| 288 |
" num=3 delta=-1 focus=True" |
| 289 |
" x=10 y=20 width=300 height=200>") |
| 290 |
|
| 291 |
def test_eventtype_enum(self): |
| 292 |
class CheckedEventType(enum.StrEnum): |
| 293 |
KeyPress = '2' |
| 294 |
Key = KeyPress |
| 295 |
KeyRelease = '3' |
| 296 |
ButtonPress = '4' |
| 297 |
Button = ButtonPress |
| 298 |
ButtonRelease = '5' |
| 299 |
Motion = '6' |
| 300 |
Enter = '7' |
| 301 |
Leave = '8' |
| 302 |
FocusIn = '9' |
| 303 |
FocusOut = '10' |
| 304 |
Keymap = '11' |
| 305 |
Expose = '12' |
| 306 |
GraphicsExpose = '13' |
| 307 |
NoExpose = '14' |
| 308 |
Visibility = '15' |
| 309 |
Create = '16' |
| 310 |
Destroy = '17' |
| 311 |
Unmap = '18' |
| 312 |
Map = '19' |
| 313 |
MapRequest = '20' |
| 314 |
Reparent = '21' |
| 315 |
Configure = '22' |
| 316 |
ConfigureRequest = '23' |
| 317 |
Gravity = '24' |
| 318 |
ResizeRequest = '25' |
| 319 |
Circulate = '26' |
| 320 |
CirculateRequest = '27' |
| 321 |
Property = '28' |
| 322 |
SelectionClear = '29' |
| 323 |
SelectionRequest = '30' |
| 324 |
Selection = '31' |
| 325 |
Colormap = '32' |
| 326 |
ClientMessage = '33' |
| 327 |
Mapping = '34' |
| 328 |
VirtualEvent = '35' |
| 329 |
Activate = '36' |
| 330 |
Deactivate = '37' |
| 331 |
MouseWheel = '38' |
| 332 |
enum._test_simple_enum(CheckedEventType, tkinter.EventType) |
| 333 |
|
| 334 |
def test_getboolean(self): |
| 335 |
for v in 'true', 'yes', 'on', '1', 't', 'y', 1, True: |
| 336 |
self.assertIs(self.root.getboolean(v), True) |
| 337 |
for v in 'false', 'no', 'off', '0', 'f', 'n', 0, False: |
| 338 |
self.assertIs(self.root.getboolean(v), False) |
| 339 |
self.assertRaises(ValueError, self.root.getboolean, 'yea') |
| 340 |
self.assertRaises(ValueError, self.root.getboolean, '') |
| 341 |
self.assertRaises(TypeError, self.root.getboolean, None) |
| 342 |
self.assertRaises(TypeError, self.root.getboolean, ()) |
| 343 |
|
| 344 |
def test_mainloop(self): |
| 345 |
log = [] |
| 346 |
def callback(): |
| 347 |
log.append(1) |
| 348 |
self.root.after(100, self.root.quit) |
| 349 |
self.root.after(100, callback) |
| 350 |
self.root.mainloop(1) |
| 351 |
self.assertEqual(log, []) |
| 352 |
self.root.mainloop(0) |
| 353 |
self.assertEqual(log, [1]) |
| 354 |
self.assertTrue(self.root.winfo_exists()) |
| 355 |
|
| 356 |
def test_info_patchlevel(self): |
| 357 |
vi = self.root.info_patchlevel() |
| 358 |
f = tkinter.Frame(self.root) |
| 359 |
self.assertEqual(f.info_patchlevel(), vi) |
| 360 |
|
| 361 |
self.assertIsInstance(vi[:], tuple) |
| 362 |
self.assertEqual(len(vi), 5) |
| 363 |
self.assertIsInstance(vi[0], int) |
| 364 |
self.assertIsInstance(vi[1], int) |
| 365 |
self.assertIsInstance(vi[2], int) |
| 366 |
self.assertIn(vi[3], ("alpha", "beta", "candidate", "final")) |
| 367 |
self.assertIsInstance(vi[4], int) |
| 368 |
self.assertIsInstance(vi.major, int) |
| 369 |
self.assertIsInstance(vi.minor, int) |
| 370 |
self.assertIsInstance(vi.micro, int) |
| 371 |
self.assertIn(vi.releaselevel, ("alpha", "beta", "final")) |
| 372 |
self.assertIsInstance(vi.serial, int) |
| 373 |
self.assertEqual(vi[0], vi.major) |
| 374 |
self.assertEqual(vi[1], vi.minor) |
| 375 |
self.assertEqual(vi[2], vi.micro) |
| 376 |
self.assertEqual(vi[3], vi.releaselevel) |
| 377 |
self.assertEqual(vi[4], vi.serial) |
| 378 |
self.assertTrue(vi > (1,0,0)) |
| 379 |
if vi.releaselevel == 'final': |
| 380 |
self.assertEqual(vi.serial, 0) |
| 381 |
else: |
| 382 |
self.assertEqual(vi.micro, 0) |
| 383 |
self.assertTrue(str(vi).startswith(f'{vi.major}.{vi.minor}')) |
| 384 |
|
| 385 |
|
| 386 |
class BindTest(AbstractTkTest, unittest.TestCase): |
| 387 |
|
| 388 |
def setUp(self): |
| 389 |
super().setUp() |
| 390 |
root = self.root |
| 391 |
self.frame = tkinter.Frame(self.root, class_='Test', |
| 392 |
width=150, height=100) |
| 393 |
self.frame.pack() |
| 394 |
|
| 395 |
def assertCommandExist(self, funcid): |
| 396 |
self.assertEqual(_info_commands(self.root, funcid), (funcid,)) |
| 397 |
|
| 398 |
def assertCommandNotExist(self, funcid): |
| 399 |
self.assertEqual(_info_commands(self.root, funcid), ()) |
| 400 |
|
| 401 |
def test_bind(self): |
| 402 |
event = '<Control-Alt-Key-a>' |
| 403 |
f = self.frame |
| 404 |
self.assertEqual(f.bind(), ()) |
| 405 |
self.assertEqual(f.bind(event), '') |
| 406 |
def test1(e): pass |
| 407 |
def test2(e): pass |
| 408 |
|
| 409 |
funcid = f.bind(event, test1) |
| 410 |
self.assertEqual(f.bind(), (event,)) |
| 411 |
script = f.bind(event) |
| 412 |
self.assertIn(funcid, script) |
| 413 |
self.assertCommandExist(funcid) |
| 414 |
|
| 415 |
funcid2 = f.bind(event, test2, add=True) |
| 416 |
script = f.bind(event) |
| 417 |
self.assertIn(funcid, script) |
| 418 |
self.assertIn(funcid2, script) |
| 419 |
self.assertCommandExist(funcid) |
| 420 |
self.assertCommandExist(funcid2) |
| 421 |
|
| 422 |
def test_unbind(self): |
| 423 |
event = '<Control-Alt-Key-b>' |
| 424 |
f = self.frame |
| 425 |
self.assertEqual(f.bind(), ()) |
| 426 |
self.assertEqual(f.bind(event), '') |
| 427 |
def test1(e): pass |
| 428 |
def test2(e): pass |
| 429 |
|
| 430 |
funcid = f.bind(event, test1) |
| 431 |
funcid2 = f.bind(event, test2, add=True) |
| 432 |
|
| 433 |
self.assertRaises(TypeError, f.unbind) |
| 434 |
f.unbind(event) |
| 435 |
self.assertEqual(f.bind(event), '') |
| 436 |
self.assertEqual(f.bind(), ()) |
| 437 |
|
| 438 |
def test_unbind2(self): |
| 439 |
f = self.frame |
| 440 |
f.wait_visibility() |
| 441 |
f.focus_force() |
| 442 |
f.update_idletasks() |
| 443 |
event = '<Control-Alt-Key-c>' |
| 444 |
self.assertEqual(f.bind(), ()) |
| 445 |
self.assertEqual(f.bind(event), '') |
| 446 |
def test1(e): events.append('a') |
| 447 |
def test2(e): events.append('b') |
| 448 |
def test3(e): events.append('c') |
| 449 |
|
| 450 |
funcid = f.bind(event, test1) |
| 451 |
funcid2 = f.bind(event, test2, add=True) |
| 452 |
funcid3 = f.bind(event, test3, add=True) |
| 453 |
events = [] |
| 454 |
f.event_generate(event) |
| 455 |
self.assertEqual(events, ['a', 'b', 'c']) |
| 456 |
|
| 457 |
f.unbind(event, funcid2) |
| 458 |
script = f.bind(event) |
| 459 |
self.assertNotIn(funcid2, script) |
| 460 |
self.assertIn(funcid, script) |
| 461 |
self.assertIn(funcid3, script) |
| 462 |
self.assertEqual(f.bind(), (event,)) |
| 463 |
self.assertCommandNotExist(funcid2) |
| 464 |
self.assertCommandExist(funcid) |
| 465 |
self.assertCommandExist(funcid3) |
| 466 |
events = [] |
| 467 |
f.event_generate(event) |
| 468 |
self.assertEqual(events, ['a', 'c']) |
| 469 |
|
| 470 |
f.unbind(event, funcid) |
| 471 |
f.unbind(event, funcid3) |
| 472 |
self.assertEqual(f.bind(event), '') |
| 473 |
self.assertEqual(f.bind(), ()) |
| 474 |
self.assertCommandNotExist(funcid) |
| 475 |
self.assertCommandNotExist(funcid2) |
| 476 |
self.assertCommandNotExist(funcid3) |
| 477 |
events = [] |
| 478 |
f.event_generate(event) |
| 479 |
self.assertEqual(events, []) |
| 480 |
|
| 481 |
|
| 482 |
self.assertRaises(tkinter.TclError, f.unbind, event, funcid2) |
| 483 |
|
| 484 |
def test_bind_rebind(self): |
| 485 |
event = '<Control-Alt-Key-d>' |
| 486 |
f = self.frame |
| 487 |
self.assertEqual(f.bind(), ()) |
| 488 |
self.assertEqual(f.bind(event), '') |
| 489 |
def test1(e): pass |
| 490 |
def test2(e): pass |
| 491 |
def test3(e): pass |
| 492 |
|
| 493 |
funcid = f.bind(event, test1) |
| 494 |
funcid2 = f.bind(event, test2, add=True) |
| 495 |
script = f.bind(event) |
| 496 |
self.assertIn(funcid2, script) |
| 497 |
self.assertIn(funcid, script) |
| 498 |
self.assertCommandExist(funcid) |
| 499 |
self.assertCommandExist(funcid2) |
| 500 |
|
| 501 |
funcid3 = f.bind(event, test3) |
| 502 |
script = f.bind(event) |
| 503 |
self.assertNotIn(funcid, script) |
| 504 |
self.assertNotIn(funcid2, script) |
| 505 |
self.assertIn(funcid3, script) |
| 506 |
self.assertCommandExist(funcid3) |
| 507 |
|
| 508 |
def test_bind_class(self): |
| 509 |
event = '<Control-Alt-Key-e>' |
| 510 |
bind_class = self.root.bind_class |
| 511 |
unbind_class = self.root.unbind_class |
| 512 |
self.assertRaises(TypeError, bind_class) |
| 513 |
self.assertEqual(bind_class('Test'), ()) |
| 514 |
self.assertEqual(bind_class('Test', event), '') |
| 515 |
self.addCleanup(unbind_class, 'Test', event) |
| 516 |
def test1(e): pass |
| 517 |
def test2(e): pass |
| 518 |
|
| 519 |
funcid = bind_class('Test', event, test1) |
| 520 |
self.assertEqual(bind_class('Test'), (event,)) |
| 521 |
script = bind_class('Test', event) |
| 522 |
self.assertIn(funcid, script) |
| 523 |
self.assertCommandExist(funcid) |
| 524 |
|
| 525 |
funcid2 = bind_class('Test', event, test2, add=True) |
| 526 |
script = bind_class('Test', event) |
| 527 |
self.assertIn(funcid, script) |
| 528 |
self.assertIn(funcid2, script) |
| 529 |
self.assertCommandExist(funcid) |
| 530 |
self.assertCommandExist(funcid2) |
| 531 |
|
| 532 |
def test_unbind_class(self): |
| 533 |
event = '<Control-Alt-Key-f>' |
| 534 |
bind_class = self.root.bind_class |
| 535 |
unbind_class = self.root.unbind_class |
| 536 |
self.assertEqual(bind_class('Test'), ()) |
| 537 |
self.assertEqual(bind_class('Test', event), '') |
| 538 |
self.addCleanup(unbind_class, 'Test', event) |
| 539 |
def test1(e): pass |
| 540 |
def test2(e): pass |
| 541 |
|
| 542 |
funcid = bind_class('Test', event, test1) |
| 543 |
funcid2 = bind_class('Test', event, test2, add=True) |
| 544 |
|
| 545 |
self.assertRaises(TypeError, unbind_class) |
| 546 |
self.assertRaises(TypeError, unbind_class, 'Test') |
| 547 |
unbind_class('Test', event) |
| 548 |
self.assertEqual(bind_class('Test', event), '') |
| 549 |
self.assertEqual(bind_class('Test'), ()) |
| 550 |
self.assertCommandExist(funcid) |
| 551 |
self.assertCommandExist(funcid2) |
| 552 |
|
| 553 |
unbind_class('Test', event) |
| 554 |
|
| 555 |
def test_bind_class_rebind(self): |
| 556 |
event = '<Control-Alt-Key-g>' |
| 557 |
bind_class = self.root.bind_class |
| 558 |
unbind_class = self.root.unbind_class |
| 559 |
self.assertEqual(bind_class('Test'), ()) |
| 560 |
self.assertEqual(bind_class('Test', event), '') |
| 561 |
self.addCleanup(unbind_class, 'Test', event) |
| 562 |
def test1(e): pass |
| 563 |
def test2(e): pass |
| 564 |
def test3(e): pass |
| 565 |
|
| 566 |
funcid = bind_class('Test', event, test1) |
| 567 |
funcid2 = bind_class('Test', event, test2, add=True) |
| 568 |
script = bind_class('Test', event) |
| 569 |
self.assertIn(funcid2, script) |
| 570 |
self.assertIn(funcid, script) |
| 571 |
self.assertCommandExist(funcid) |
| 572 |
self.assertCommandExist(funcid2) |
| 573 |
|
| 574 |
funcid3 = bind_class('Test', event, test3) |
| 575 |
script = bind_class('Test', event) |
| 576 |
self.assertNotIn(funcid, script) |
| 577 |
self.assertNotIn(funcid2, script) |
| 578 |
self.assertIn(funcid3, script) |
| 579 |
self.assertCommandExist(funcid) |
| 580 |
self.assertCommandExist(funcid2) |
| 581 |
self.assertCommandExist(funcid3) |
| 582 |
|
| 583 |
def test_bind_all(self): |
| 584 |
event = '<Control-Alt-Key-h>' |
| 585 |
bind_all = self.root.bind_all |
| 586 |
unbind_all = self.root.unbind_all |
| 587 |
self.assertNotIn(event, bind_all()) |
| 588 |
self.assertEqual(bind_all(event), '') |
| 589 |
self.addCleanup(unbind_all, event) |
| 590 |
def test1(e): pass |
| 591 |
def test2(e): pass |
| 592 |
|
| 593 |
funcid = bind_all(event, test1) |
| 594 |
self.assertIn(event, bind_all()) |
| 595 |
script = bind_all(event) |
| 596 |
self.assertIn(funcid, script) |
| 597 |
self.assertCommandExist(funcid) |
| 598 |
|
| 599 |
funcid2 = bind_all(event, test2, add=True) |
| 600 |
script = bind_all(event) |
| 601 |
self.assertIn(funcid, script) |
| 602 |
self.assertIn(funcid2, script) |
| 603 |
self.assertCommandExist(funcid) |
| 604 |
self.assertCommandExist(funcid2) |
| 605 |
|
| 606 |
def test_unbind_all(self): |
| 607 |
event = '<Control-Alt-Key-i>' |
| 608 |
bind_all = self.root.bind_all |
| 609 |
unbind_all = self.root.unbind_all |
| 610 |
self.assertNotIn(event, bind_all()) |
| 611 |
self.assertEqual(bind_all(event), '') |
| 612 |
self.addCleanup(unbind_all, event) |
| 613 |
def test1(e): pass |
| 614 |
def test2(e): pass |
| 615 |
|
| 616 |
funcid = bind_all(event, test1) |
| 617 |
funcid2 = bind_all(event, test2, add=True) |
| 618 |
|
| 619 |
unbind_all(event) |
| 620 |
self.assertEqual(bind_all(event), '') |
| 621 |
self.assertNotIn(event, bind_all()) |
| 622 |
self.assertCommandExist(funcid) |
| 623 |
self.assertCommandExist(funcid2) |
| 624 |
|
| 625 |
unbind_all(event) |
| 626 |
|
| 627 |
def test_bind_all_rebind(self): |
| 628 |
event = '<Control-Alt-Key-j>' |
| 629 |
bind_all = self.root.bind_all |
| 630 |
unbind_all = self.root.unbind_all |
| 631 |
self.assertNotIn(event, bind_all()) |
| 632 |
self.assertEqual(bind_all(event), '') |
| 633 |
self.addCleanup(unbind_all, event) |
| 634 |
def test1(e): pass |
| 635 |
def test2(e): pass |
| 636 |
def test3(e): pass |
| 637 |
|
| 638 |
funcid = bind_all(event, test1) |
| 639 |
funcid2 = bind_all(event, test2, add=True) |
| 640 |
script = bind_all(event) |
| 641 |
self.assertIn(funcid2, script) |
| 642 |
self.assertIn(funcid, script) |
| 643 |
self.assertCommandExist(funcid) |
| 644 |
self.assertCommandExist(funcid2) |
| 645 |
|
| 646 |
funcid3 = bind_all(event, test3) |
| 647 |
script = bind_all(event) |
| 648 |
self.assertNotIn(funcid, script) |
| 649 |
self.assertNotIn(funcid2, script) |
| 650 |
self.assertIn(funcid3, script) |
| 651 |
self.assertCommandExist(funcid) |
| 652 |
self.assertCommandExist(funcid2) |
| 653 |
self.assertCommandExist(funcid3) |
| 654 |
|
| 655 |
def _test_tag_bind(self, w): |
| 656 |
tag = 'sel' |
| 657 |
event = '<Control-Alt-Key-a>' |
| 658 |
w.pack() |
| 659 |
self.assertRaises(TypeError, w.tag_bind) |
| 660 |
tag_bind = w._tag_bind if isinstance(w, tkinter.Text) else w.tag_bind |
| 661 |
if isinstance(w, tkinter.Text): |
| 662 |
self.assertRaises(TypeError, w.tag_bind, tag) |
| 663 |
self.assertRaises(TypeError, w.tag_bind, tag, event) |
| 664 |
self.assertEqual(tag_bind(tag), ()) |
| 665 |
self.assertEqual(tag_bind(tag, event), '') |
| 666 |
def test1(e): pass |
| 667 |
def test2(e): pass |
| 668 |
|
| 669 |
funcid = w.tag_bind(tag, event, test1) |
| 670 |
self.assertEqual(tag_bind(tag), (event,)) |
| 671 |
script = tag_bind(tag, event) |
| 672 |
self.assertIn(funcid, script) |
| 673 |
self.assertCommandExist(funcid) |
| 674 |
|
| 675 |
funcid2 = w.tag_bind(tag, event, test2, add=True) |
| 676 |
script = tag_bind(tag, event) |
| 677 |
self.assertIn(funcid, script) |
| 678 |
self.assertIn(funcid2, script) |
| 679 |
self.assertCommandExist(funcid) |
| 680 |
self.assertCommandExist(funcid2) |
| 681 |
|
| 682 |
def _test_tag_unbind(self, w): |
| 683 |
tag = 'sel' |
| 684 |
event = '<Control-Alt-Key-b>' |
| 685 |
w.pack() |
| 686 |
tag_bind = w._tag_bind if isinstance(w, tkinter.Text) else w.tag_bind |
| 687 |
self.assertEqual(tag_bind(tag), ()) |
| 688 |
self.assertEqual(tag_bind(tag, event), '') |
| 689 |
def test1(e): pass |
| 690 |
def test2(e): pass |
| 691 |
|
| 692 |
funcid = w.tag_bind(tag, event, test1) |
| 693 |
funcid2 = w.tag_bind(tag, event, test2, add=True) |
| 694 |
|
| 695 |
self.assertRaises(TypeError, w.tag_unbind, tag) |
| 696 |
w.tag_unbind(tag, event) |
| 697 |
self.assertEqual(tag_bind(tag, event), '') |
| 698 |
self.assertEqual(tag_bind(tag), ()) |
| 699 |
|
| 700 |
def _test_tag_bind_rebind(self, w): |
| 701 |
tag = 'sel' |
| 702 |
event = '<Control-Alt-Key-d>' |
| 703 |
w.pack() |
| 704 |
tag_bind = w._tag_bind if isinstance(w, tkinter.Text) else w.tag_bind |
| 705 |
self.assertEqual(tag_bind(tag), ()) |
| 706 |
self.assertEqual(tag_bind(tag, event), '') |
| 707 |
def test1(e): pass |
| 708 |
def test2(e): pass |
| 709 |
def test3(e): pass |
| 710 |
|
| 711 |
funcid = w.tag_bind(tag, event, test1) |
| 712 |
funcid2 = w.tag_bind(tag, event, test2, add=True) |
| 713 |
script = tag_bind(tag, event) |
| 714 |
self.assertIn(funcid2, script) |
| 715 |
self.assertIn(funcid, script) |
| 716 |
self.assertCommandExist(funcid) |
| 717 |
self.assertCommandExist(funcid2) |
| 718 |
|
| 719 |
funcid3 = w.tag_bind(tag, event, test3) |
| 720 |
script = tag_bind(tag, event) |
| 721 |
self.assertNotIn(funcid, script) |
| 722 |
self.assertNotIn(funcid2, script) |
| 723 |
self.assertIn(funcid3, script) |
| 724 |
self.assertCommandExist(funcid3) |
| 725 |
|
| 726 |
def test_canvas_tag_bind(self): |
| 727 |
c = tkinter.Canvas(self.frame) |
| 728 |
self._test_tag_bind(c) |
| 729 |
|
| 730 |
def test_canvas_tag_unbind(self): |
| 731 |
c = tkinter.Canvas(self.frame) |
| 732 |
self._test_tag_unbind(c) |
| 733 |
|
| 734 |
def test_canvas_tag_bind_rebind(self): |
| 735 |
c = tkinter.Canvas(self.frame) |
| 736 |
self._test_tag_bind_rebind(c) |
| 737 |
|
| 738 |
def test_text_tag_bind(self): |
| 739 |
t = tkinter.Text(self.frame) |
| 740 |
self._test_tag_bind(t) |
| 741 |
|
| 742 |
def test_text_tag_unbind(self): |
| 743 |
t = tkinter.Text(self.frame) |
| 744 |
self._test_tag_unbind(t) |
| 745 |
|
| 746 |
def test_text_tag_bind_rebind(self): |
| 747 |
t = tkinter.Text(self.frame) |
| 748 |
self._test_tag_bind_rebind(t) |
| 749 |
|
| 750 |
def test_bindtags(self): |
| 751 |
f = self.frame |
| 752 |
self.assertEqual(self.root.bindtags(), ('.', 'Tk', 'all')) |
| 753 |
self.assertEqual(f.bindtags(), (str(f), 'Test', '.', 'all')) |
| 754 |
f.bindtags(('a', 'b c')) |
| 755 |
self.assertEqual(f.bindtags(), ('a', 'b c')) |
| 756 |
|
| 757 |
def test_bind_events(self): |
| 758 |
event = '<Enter>' |
| 759 |
root = self.root |
| 760 |
t = tkinter.Toplevel(root) |
| 761 |
f = tkinter.Frame(t, class_='Test', width=150, height=100) |
| 762 |
f.pack() |
| 763 |
root.wait_visibility() |
| 764 |
root.update_idletasks() |
| 765 |
self.addCleanup(root.unbind_class, 'Test', event) |
| 766 |
self.addCleanup(root.unbind_class, 'Toplevel', event) |
| 767 |
self.addCleanup(root.unbind_class, 'tag', event) |
| 768 |
self.addCleanup(root.unbind_class, 'tag2', event) |
| 769 |
self.addCleanup(root.unbind_all, event) |
| 770 |
def test(what): |
| 771 |
return lambda e: events.append((what, e.widget)) |
| 772 |
|
| 773 |
root.bind_all(event, test('all')) |
| 774 |
root.bind_class('Test', event, test('frame class')) |
| 775 |
root.bind_class('Toplevel', event, test('toplevel class')) |
| 776 |
root.bind_class('tag', event, test('tag')) |
| 777 |
root.bind_class('tag2', event, test('tag2')) |
| 778 |
f.bind(event, test('frame')) |
| 779 |
t.bind(event, test('toplevel')) |
| 780 |
|
| 781 |
events = [] |
| 782 |
f.event_generate(event) |
| 783 |
self.assertEqual(events, [ |
| 784 |
('frame', f), |
| 785 |
('frame class', f), |
| 786 |
('toplevel', f), |
| 787 |
('all', f), |
| 788 |
]) |
| 789 |
|
| 790 |
events = [] |
| 791 |
t.event_generate(event) |
| 792 |
self.assertEqual(events, [ |
| 793 |
('toplevel', t), |
| 794 |
('toplevel class', t), |
| 795 |
('all', t), |
| 796 |
]) |
| 797 |
|
| 798 |
f.bindtags(('tag', 'tag3')) |
| 799 |
events = [] |
| 800 |
f.event_generate(event) |
| 801 |
self.assertEqual(events, [('tag', f)]) |
| 802 |
|
| 803 |
|
| 804 |
class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase): |
| 805 |
|
| 806 |
def test_default_root(self): |
| 807 |
self.assertIs(tkinter._support_default_root, True) |
| 808 |
self.assertIsNone(tkinter._default_root) |
| 809 |
root = tkinter.Tk() |
| 810 |
root2 = tkinter.Tk() |
| 811 |
root3 = tkinter.Tk() |
| 812 |
self.assertIs(tkinter._default_root, root) |
| 813 |
root2.destroy() |
| 814 |
self.assertIs(tkinter._default_root, root) |
| 815 |
root.destroy() |
| 816 |
self.assertIsNone(tkinter._default_root) |
| 817 |
root3.destroy() |
| 818 |
self.assertIsNone(tkinter._default_root) |
| 819 |
|
| 820 |
def test_no_default_root(self): |
| 821 |
self.assertIs(tkinter._support_default_root, True) |
| 822 |
self.assertIsNone(tkinter._default_root) |
| 823 |
root = tkinter.Tk() |
| 824 |
self.assertIs(tkinter._default_root, root) |
| 825 |
tkinter.NoDefaultRoot() |
| 826 |
self.assertIs(tkinter._support_default_root, False) |
| 827 |
self.assertFalse(hasattr(tkinter, '_default_root')) |
| 828 |
|
| 829 |
tkinter.NoDefaultRoot() |
| 830 |
self.assertIs(tkinter._support_default_root, False) |
| 831 |
self.assertFalse(hasattr(tkinter, '_default_root')) |
| 832 |
root.destroy() |
| 833 |
self.assertIs(tkinter._support_default_root, False) |
| 834 |
self.assertFalse(hasattr(tkinter, '_default_root')) |
| 835 |
root = tkinter.Tk() |
| 836 |
self.assertIs(tkinter._support_default_root, False) |
| 837 |
self.assertFalse(hasattr(tkinter, '_default_root')) |
| 838 |
root.destroy() |
| 839 |
|
| 840 |
def test_getboolean(self): |
| 841 |
self.assertRaises(RuntimeError, tkinter.getboolean, '1') |
| 842 |
root = tkinter.Tk() |
| 843 |
self.assertIs(tkinter.getboolean('1'), True) |
| 844 |
self.assertRaises(ValueError, tkinter.getboolean, 'yea') |
| 845 |
root.destroy() |
| 846 |
tkinter.NoDefaultRoot() |
| 847 |
self.assertRaises(RuntimeError, tkinter.getboolean, '1') |
| 848 |
|
| 849 |
def test_mainloop(self): |
| 850 |
self.assertRaises(RuntimeError, tkinter.mainloop) |
| 851 |
root = tkinter.Tk() |
| 852 |
root.after_idle(root.quit) |
| 853 |
tkinter.mainloop() |
| 854 |
root.destroy() |
| 855 |
tkinter.NoDefaultRoot() |
| 856 |
self.assertRaises(RuntimeError, tkinter.mainloop) |
| 857 |
|
| 858 |
|
| 859 |
def _info_commands(widget, pattern=None): |
| 860 |
return widget.tk.splitlist(widget.tk.call('info', 'commands', pattern)) |
| 861 |
|
| 862 |
|
| 863 |
if __name__ == "__main__": |
| 864 |
unittest.main() |