| 1 |
import functools |
| 2 |
import platform |
| 3 |
import sys |
| 4 |
import textwrap |
| 5 |
import unittest |
| 6 |
import weakref |
| 7 |
import tkinter |
| 8 |
from tkinter import TclError |
| 9 |
import enum |
| 10 |
from test import support |
| 11 |
from test.support import os_helper |
| 12 |
from test.support.script_helper import assert_python_ok |
| 13 |
from test.test_tkinter.support import setUpModule # noqa: F401 |
| 14 |
from test.test_tkinter.support import (AbstractTkTest, AbstractDefaultRootTest, |
| 15 |
requires_tk, get_tk_patchlevel, |
| 16 |
tcl_version) |
| 17 |
|
| 18 |
support.requires('gui') |
| 19 |
|
| 20 |
class MiscTest(AbstractTkTest, unittest.TestCase): |
| 21 |
|
| 22 |
def test_all(self): |
| 23 |
self.assertIn("Widget", tkinter.__all__) |
| 24 |
# Check that variables from tkinter.constants are also in tkinter.__all__ |
| 25 |
self.assertIn("CASCADE", tkinter.__all__) |
| 26 |
self.assertIsNotNone(tkinter.CASCADE) |
| 27 |
# Check that sys, re, and constants are not in tkinter.__all__ |
| 28 |
self.assertNotIn("re", tkinter.__all__) |
| 29 |
self.assertNotIn("sys", tkinter.__all__) |
| 30 |
self.assertNotIn("constants", tkinter.__all__) |
| 31 |
# Check that an underscored functions is not in tkinter.__all__ |
| 32 |
self.assertNotIn("_tkerror", tkinter.__all__) |
| 33 |
# Check that wantobjects is not in tkinter.__all__ |
| 34 |
self.assertNotIn("wantobjects", tkinter.__all__) |
| 35 |
|
| 36 |
def test_repr(self): |
| 37 |
t = tkinter.Toplevel(self.root, name='top') |
| 38 |
f = tkinter.Frame(t, name='child') |
| 39 |
self.assertEqual(repr(f), '<tkinter.Frame object .top.child>') |
| 40 |
|
| 41 |
def test_generated_names(self): |
| 42 |
class Button2(tkinter.Button): |
| 43 |
pass |
| 44 |
|
| 45 |
t = tkinter.Toplevel(self.root) |
| 46 |
f = tkinter.Frame(t) |
| 47 |
f2 = tkinter.Frame(t) |
| 48 |
self.assertNotEqual(str(f), str(f2)) |
| 49 |
b = tkinter.Button(f2) |
| 50 |
b2 = Button2(f2) |
| 51 |
for name in str(b).split('.') + str(b2).split('.'): |
| 52 |
self.assertFalse(name.isidentifier(), msg=repr(name)) |
| 53 |
b3 = tkinter.Button(f2) |
| 54 |
b4 = Button2(f2) |
| 55 |
self.assertEqual(len({str(b), str(b2), str(b3), str(b4)}), 4) |
| 56 |
|
| 57 |
def test_dealloc_in_wrong_thread(self): |
| 58 |
# gh-83274: deallocating the interpreter in the wrong thread must not |
| 59 |
# crash. |
| 60 |
script = textwrap.dedent(""" |
| 61 |
import threading |
| 62 |
import tkinter |
| 63 |
root = tkinter.Tk() |
| 64 |
root.destroy() |
| 65 |
# Let another thread drop the last reference. |
| 66 |
ready = threading.Event() |
| 67 |
t = threading.Thread(target=lambda obj: ready.wait(), args=(root,)) |
| 68 |
t.start() |
| 69 |
del root |
| 70 |
ready.set() |
| 71 |
t.join() |
| 72 |
print('ok') |
| 73 |
""") |
| 74 |
rc, out, err = assert_python_ok('-c', script) |
| 75 |
self.assertEqual(out.strip(), b'ok') |
| 76 |
if not support.Py_GIL_DISABLED: |
| 77 |
# On the free-threaded build the interpreter may instead be |
| 78 |
# deallocated in its own thread (deferred reference counting), so |
| 79 |
# the warning is not necessarily emitted. The crucial guarantee -- |
| 80 |
# no crash -- is already checked by assert_python_ok() above. |
| 81 |
self.assertIn(b'RuntimeWarning', err) |
| 82 |
self.assertIn(b'gh-83274', err) |
| 83 |
|
| 84 |
@requires_tk(8, 6, 6) |
| 85 |
def test_tk_busy(self): |
| 86 |
root = self.root |
| 87 |
f = tkinter.Frame(root, name='myframe') |
| 88 |
f2 = tkinter.Frame(root) |
| 89 |
f.pack() |
| 90 |
f2.pack() |
| 91 |
b = tkinter.Button(f) |
| 92 |
b.pack() |
| 93 |
f.tk_busy_hold() |
| 94 |
with self.assertRaisesRegex(TclError, 'unknown option "-spam"'): |
| 95 |
f.tk_busy_configure(spam='eggs') |
| 96 |
with self.assertRaisesRegex(TclError, 'unknown option "-spam"'): |
| 97 |
f.tk_busy_cget('spam') |
| 98 |
with self.assertRaisesRegex(TclError, 'unknown option "-spam"'): |
| 99 |
f.tk_busy_configure('spam') |
| 100 |
self.assertIsInstance(f.tk_busy_configure(), dict) |
| 101 |
|
| 102 |
self.assertTrue(f.tk_busy_status()) |
| 103 |
self.assertFalse(root.tk_busy_status()) |
| 104 |
self.assertFalse(f2.tk_busy_status()) |
| 105 |
self.assertFalse(b.tk_busy_status()) |
| 106 |
self.assertIn(f, f.tk_busy_current()) |
| 107 |
self.assertIn(f, f.tk_busy_current('*.m?f*me')) |
| 108 |
self.assertNotIn(f, f.tk_busy_current('*spam')) |
| 109 |
|
| 110 |
f.tk_busy_forget() |
| 111 |
self.assertFalse(f.tk_busy_status()) |
| 112 |
self.assertFalse(f.tk_busy_current()) |
| 113 |
errmsg = r"can(no|')t find busy window.*" |
| 114 |
with self.assertRaisesRegex(TclError, errmsg): |
| 115 |
f.tk_busy_configure() |
| 116 |
with self.assertRaisesRegex(TclError, errmsg): |
| 117 |
f.tk_busy_forget() |
| 118 |
|
| 119 |
@requires_tk(8, 6, 6) |
| 120 |
def test_tk_busy_with_cursor(self): |
| 121 |
root = self.root |
| 122 |
if root._windowingsystem == 'aqua': |
| 123 |
self.skipTest('the cursor option is not supported on OSX/Aqua') |
| 124 |
f = tkinter.Frame(root, name='myframe') |
| 125 |
f.pack() |
| 126 |
f.tk_busy_hold(cursor='gumby') |
| 127 |
|
| 128 |
self.assertEqual(f.tk_busy_cget('cursor'), 'gumby') |
| 129 |
f.tk_busy_configure(cursor='heart') |
| 130 |
self.assertEqual(f.tk_busy_cget('cursor'), 'heart') |
| 131 |
self.assertEqual(f.tk_busy_configure()['cursor'][4], 'heart') |
| 132 |
self.assertEqual(f.tk_busy_configure('cursor')[4], 'heart') |
| 133 |
|
| 134 |
f.tk_busy_forget() |
| 135 |
errmsg = r"can(no|')t find busy window.*" |
| 136 |
with self.assertRaisesRegex(TclError, errmsg): |
| 137 |
f.tk_busy_cget('cursor') |
| 138 |
|
| 139 |
def test_tk_setPalette(self): |
| 140 |
root = self.root |
| 141 |
root.tk_setPalette('black') |
| 142 |
self.assertEqual(root['background'], 'black') |
| 143 |
root.tk_setPalette('white') |
| 144 |
self.assertEqual(root['background'], 'white') |
| 145 |
self.assertRaisesRegex(tkinter.TclError, |
| 146 |
'^unknown color name "spam"$', |
| 147 |
root.tk_setPalette, 'spam') |
| 148 |
|
| 149 |
root.tk_setPalette(background='black') |
| 150 |
self.assertEqual(root['background'], 'black') |
| 151 |
root.tk_setPalette(background='blue', highlightColor='yellow') |
| 152 |
self.assertEqual(root['background'], 'blue') |
| 153 |
self.assertEqual(root['highlightcolor'], 'yellow') |
| 154 |
root.tk_setPalette(background='yellow', highlightColor='blue') |
| 155 |
self.assertEqual(root['background'], 'yellow') |
| 156 |
self.assertEqual(root['highlightcolor'], 'blue') |
| 157 |
self.assertRaisesRegex(tkinter.TclError, |
| 158 |
'^unknown color name "spam"$', |
| 159 |
root.tk_setPalette, background='spam') |
| 160 |
self.assertRaisesRegex(tkinter.TclError, |
| 161 |
'^must specify a background color$', |
| 162 |
root.tk_setPalette, spam='white') |
| 163 |
self.assertRaisesRegex(tkinter.TclError, |
| 164 |
'^must specify a background color$', |
| 165 |
root.tk_setPalette, highlightColor='blue') |
| 166 |
|
| 167 |
def test_after(self): |
| 168 |
root = self.root |
| 169 |
|
| 170 |
def callback(start=0, step=1): |
| 171 |
nonlocal count |
| 172 |
count = start + step |
| 173 |
|
| 174 |
# Without function, sleeps for ms. |
| 175 |
self.assertIsNone(root.after(1)) |
| 176 |
|
| 177 |
# Set up with callback with no args. |
| 178 |
count = 0 |
| 179 |
timer1 = root.after(0, callback) |
| 180 |
self.assertIn(timer1, root.tk.call('after', 'info')) |
| 181 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) |
| 182 |
root.update() # Process all pending events. |
| 183 |
self.assertEqual(count, 1) |
| 184 |
with self.assertRaises(tkinter.TclError): |
| 185 |
root.tk.call(script) |
| 186 |
|
| 187 |
# Set up with callback with args. |
| 188 |
count = 0 |
| 189 |
timer1 = root.after(0, callback, 42, 11) |
| 190 |
root.update() # Process all pending events. |
| 191 |
self.assertEqual(count, 53) |
| 192 |
|
| 193 |
# Cancel before called. |
| 194 |
timer1 = root.after(1000, callback) |
| 195 |
self.assertIn(timer1, root.tk.call('after', 'info')) |
| 196 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) |
| 197 |
root.after_cancel(timer1) # Cancel this event. |
| 198 |
self.assertEqual(count, 53) |
| 199 |
with self.assertRaises(tkinter.TclError): |
| 200 |
root.tk.call(script) |
| 201 |
|
| 202 |
# Call with a callable class |
| 203 |
count = 0 |
| 204 |
timer1 = root.after(0, functools.partial(callback, 42, 11)) |
| 205 |
root.update() # Process all pending events. |
| 206 |
self.assertEqual(count, 53) |
| 207 |
|
| 208 |
def test_after_idle(self): |
| 209 |
root = self.root |
| 210 |
|
| 211 |
def callback(start=0, step=1): |
| 212 |
nonlocal count |
| 213 |
count = start + step |
| 214 |
|
| 215 |
# Set up with callback with no args. |
| 216 |
count = 0 |
| 217 |
idle1 = root.after_idle(callback) |
| 218 |
self.assertIn(idle1, root.tk.call('after', 'info')) |
| 219 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) |
| 220 |
root.update_idletasks() # Process all pending events. |
| 221 |
self.assertEqual(count, 1) |
| 222 |
with self.assertRaises(tkinter.TclError): |
| 223 |
root.tk.call(script) |
| 224 |
|
| 225 |
# Set up with callback with args. |
| 226 |
count = 0 |
| 227 |
idle1 = root.after_idle(callback, 42, 11) |
| 228 |
root.update_idletasks() # Process all pending events. |
| 229 |
self.assertEqual(count, 53) |
| 230 |
|
| 231 |
# Cancel before called. |
| 232 |
idle1 = root.after_idle(callback) |
| 233 |
self.assertIn(idle1, root.tk.call('after', 'info')) |
| 234 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) |
| 235 |
root.after_cancel(idle1) # Cancel this event. |
| 236 |
self.assertEqual(count, 53) |
| 237 |
with self.assertRaises(tkinter.TclError): |
| 238 |
root.tk.call(script) |
| 239 |
|
| 240 |
def test_after_cancel(self): |
| 241 |
root = self.root |
| 242 |
|
| 243 |
def callback(): |
| 244 |
nonlocal count |
| 245 |
count += 1 |
| 246 |
|
| 247 |
timer1 = root.after(5000, callback) |
| 248 |
idle1 = root.after_idle(callback) |
| 249 |
|
| 250 |
# No value for id raises a ValueError. |
| 251 |
with self.assertRaises(ValueError): |
| 252 |
root.after_cancel(None) |
| 253 |
|
| 254 |
# Cancel timer event. |
| 255 |
count = 0 |
| 256 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) |
| 257 |
root.tk.call(script) |
| 258 |
self.assertEqual(count, 1) |
| 259 |
root.after_cancel(timer1) |
| 260 |
with self.assertRaises(tkinter.TclError): |
| 261 |
root.tk.call(script) |
| 262 |
self.assertEqual(count, 1) |
| 263 |
with self.assertRaises(tkinter.TclError): |
| 264 |
root.tk.call('after', 'info', timer1) |
| 265 |
|
| 266 |
# Cancel same event - nothing happens. |
| 267 |
root.after_cancel(timer1) |
| 268 |
|
| 269 |
# Cancel idle event. |
| 270 |
count = 0 |
| 271 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) |
| 272 |
root.tk.call(script) |
| 273 |
self.assertEqual(count, 1) |
| 274 |
root.after_cancel(idle1) |
| 275 |
with self.assertRaises(tkinter.TclError): |
| 276 |
root.tk.call(script) |
| 277 |
self.assertEqual(count, 1) |
| 278 |
with self.assertRaises(tkinter.TclError): |
| 279 |
root.tk.call('after', 'info', idle1) |
| 280 |
|
| 281 |
def test_after_info(self): |
| 282 |
root = self.root |
| 283 |
|
| 284 |
# No events. |
| 285 |
self.assertEqual(root.after_info(), ()) |
| 286 |
|
| 287 |
# Add timer. |
| 288 |
timer = root.after(1, lambda: 'break') |
| 289 |
|
| 290 |
# With no parameter, it returns a tuple of the event handler ids. |
| 291 |
self.assertEqual(root.after_info(), (timer, )) |
| 292 |
root.after_cancel(timer) |
| 293 |
|
| 294 |
timer1 = root.after(5000, lambda: 'break') |
| 295 |
timer2 = root.after(5000, lambda: 'break') |
| 296 |
idle1 = root.after_idle(lambda: 'break') |
| 297 |
# Only contains new events and not 'timer'. |
| 298 |
self.assertEqual(root.after_info(), (idle1, timer2, timer1)) |
| 299 |
|
| 300 |
# With a parameter returns a tuple of (script, type). |
| 301 |
timer1_info = root.after_info(timer1) |
| 302 |
self.assertEqual(len(timer1_info), 2) |
| 303 |
self.assertEqual(timer1_info[1], 'timer') |
| 304 |
idle1_info = root.after_info(idle1) |
| 305 |
self.assertEqual(len(idle1_info), 2) |
| 306 |
self.assertEqual(idle1_info[1], 'idle') |
| 307 |
|
| 308 |
root.after_cancel(timer1) |
| 309 |
with self.assertRaises(tkinter.TclError): |
| 310 |
root.after_info(timer1) |
| 311 |
root.after_cancel(timer2) |
| 312 |
with self.assertRaises(tkinter.TclError): |
| 313 |
root.after_info(timer2) |
| 314 |
root.after_cancel(idle1) |
| 315 |
with self.assertRaises(tkinter.TclError): |
| 316 |
root.after_info(idle1) |
| 317 |
|
| 318 |
# No events. |
| 319 |
self.assertEqual(root.after_info(), ()) |
| 320 |
|
| 321 |
def test_clipboard(self): |
| 322 |
root = self.root |
| 323 |
root.clipboard_clear() |
| 324 |
root.clipboard_append('Ùñî') |
| 325 |
self.assertEqual(root.clipboard_get(), 'Ùñî') |
| 326 |
root.clipboard_append('çōđě') |
| 327 |
self.assertEqual(root.clipboard_get(), 'Ùñîçōđě') |
| 328 |
root.clipboard_clear() |
| 329 |
with self.assertRaises(tkinter.TclError): |
| 330 |
root.clipboard_get() |
| 331 |
|
| 332 |
def test_clipboard_astral(self): |
| 333 |
root = self.root |
| 334 |
root.clipboard_clear() |
| 335 |
root.clipboard_append('𝔘𝔫𝔦') |
| 336 |
self.assertEqual(root.clipboard_get(), '𝔘𝔫𝔦') |
| 337 |
root.clipboard_append('𝔠𝔬𝔡𝔢') |
| 338 |
self.assertEqual(root.clipboard_get(), '𝔘𝔫𝔦𝔠𝔬𝔡𝔢') |
| 339 |
root.clipboard_clear() |
| 340 |
with self.assertRaises(tkinter.TclError): |
| 341 |
root.clipboard_get() |
| 342 |
|
| 343 |
def test_getint(self): |
| 344 |
self.assertEqual(self.root.getint('42'), 42) |
| 345 |
self.assertEqual(self.root.getint(42), 42) |
| 346 |
self.assertEqual(self.root.getint('-5'), -5) |
| 347 |
self.assertRaises(ValueError, self.root.getint, 'spam') |
| 348 |
|
| 349 |
def test_getdouble(self): |
| 350 |
self.assertEqual(self.root.getdouble('3.5'), 3.5) |
| 351 |
self.assertEqual(self.root.getdouble(3), 3.0) |
| 352 |
self.assertRaises(ValueError, self.root.getdouble, 'spam') |
| 353 |
|
| 354 |
def test_getvar(self): |
| 355 |
self.root.setvar('test_var', 'hello') |
| 356 |
self.assertEqual(self.root.getvar('test_var'), 'hello') |
| 357 |
|
| 358 |
def test_register(self): |
| 359 |
result = [] |
| 360 |
def callback(): |
| 361 |
result.append(1) |
| 362 |
return 'spam' |
| 363 |
name = self.root.register(callback) |
| 364 |
self.assertIsInstance(name, str) |
| 365 |
self.assertEqual(self.root.tk.call(name), 'spam') |
| 366 |
self.assertEqual(result, [1]) |
| 367 |
self.root.deletecommand(name) |
| 368 |
self.assertRaises(TclError, self.root.tk.call, name) |
| 369 |
|
| 370 |
def test_createcommand_no_leak(self): |
| 371 |
# gh-80937: dropping the interpreter must release a command's callback, |
| 372 |
# even without an explicit deletecommand(). |
| 373 |
interp = tkinter.Tcl() |
| 374 |
callback = lambda: '' |
| 375 |
ref = weakref.ref(callback) |
| 376 |
interp.tk.createcommand('cb', callback) |
| 377 |
del callback, interp |
| 378 |
support.gc_collect() |
| 379 |
self.assertIsNone(ref()) |
| 380 |
|
| 381 |
def test_option(self): |
| 382 |
self.addCleanup(self.root.option_clear) |
| 383 |
self.root.option_add('*Button.background', 'red') |
| 384 |
b = tkinter.Button(self.root) |
| 385 |
self.assertEqual(b.option_get('background', 'Background'), 'red') |
| 386 |
self.assertEqual(b.option_get('foreground', 'Foreground'), '') |
| 387 |
self.root.option_clear() |
| 388 |
self.assertEqual(b.option_get('background', 'Background'), '') |
| 389 |
|
| 390 |
def test_option_readfile(self): |
| 391 |
self.addCleanup(self.root.option_clear) |
| 392 |
self.addCleanup(os_helper.unlink, os_helper.TESTFN) |
| 393 |
with open(os_helper.TESTFN, 'w') as f: |
| 394 |
f.write('*Button.background: red\n') |
| 395 |
self.root.option_readfile(os_helper.TESTFN) |
| 396 |
b = tkinter.Button(self.root) |
| 397 |
self.assertEqual(b.option_get('background', 'Background'), 'red') |
| 398 |
self.assertRaises(TclError, self.root.option_readfile, |
| 399 |
os_helper.TESTFN + '.nonexistent') |
| 400 |
self.assertRaises(TypeError, self.root.option_readfile) |
| 401 |
self.assertRaises(TypeError, self.root.option_readfile, 'a', 'b', 'c') |
| 402 |
|
| 403 |
def test_nametowidget(self): |
| 404 |
b = tkinter.Button(self.root, name='btn') |
| 405 |
self.assertIs(self.root.nametowidget('btn'), b) |
| 406 |
self.assertIs(self.root.nametowidget(str(b)), b) |
| 407 |
self.assertRaises(KeyError, self.root.nametowidget, '.nonexistent') |
| 408 |
|
| 409 |
def test_nametowidget_menu_clone(self): |
| 410 |
# A menu used as a menubar or cascade is cloned by Tk under an |
| 411 |
# auto-generated name (each path component is the original name |
| 412 |
# prefixed with one or more '#' clone markers). nametowidget() |
| 413 |
# maps such a name back to the original widget (gh-38464). |
| 414 |
menubar = tkinter.Menu(self.root) |
| 415 |
filemenu = tkinter.Menu(menubar, tearoff=0) |
| 416 |
menubar.add_cascade(label='File', menu=filemenu) |
| 417 |
submenu = tkinter.Menu(filemenu, tearoff=0) |
| 418 |
filemenu.add_cascade(label='More', menu=submenu) |
| 419 |
self.root['menu'] = menubar |
| 420 |
self.root.update_idletasks() |
| 421 |
|
| 422 |
originals = {menubar, filemenu, submenu} |
| 423 |
clones = [] |
| 424 |
def collect(parent): |
| 425 |
for name in self.root.tk.splitlist( |
| 426 |
self.root.tk.call('winfo', 'children', parent)): |
| 427 |
clones.append(name) |
| 428 |
collect(name) |
| 429 |
collect('.') |
| 430 |
# Every menu (originals and clones) resolves to an original widget. |
| 431 |
self.assertTrue(any('#' in name for name in clones)) |
| 432 |
for name in clones: |
| 433 |
self.assertIn(self.root.nametowidget(name), originals) |
| 434 |
|
| 435 |
def test_focus_methods(self): |
| 436 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 437 |
f.pack() |
| 438 |
self.root.wait_visibility() # needed on Windows |
| 439 |
self.root.update_idletasks() |
| 440 |
f.focus_force() |
| 441 |
self.root.update() |
| 442 |
self.assertIs(self.root.focus_get(), f) |
| 443 |
self.assertIs(self.root.focus_displayof(), f) |
| 444 |
self.assertIs(f.focus_lastfor(), f) |
| 445 |
b = tkinter.Button(f) |
| 446 |
b.pack() |
| 447 |
self.root.update() |
| 448 |
b.focus_set() |
| 449 |
self.root.update() |
| 450 |
self.assertIs(self.root.focus_get(), b) |
| 451 |
|
| 452 |
def test_focus_methods_unresolvable(self): |
| 453 |
# The focus may be on a widget that tkinter did not create and so |
| 454 |
# cannot map to an instance (e.g. a torn-off menu). The focus |
| 455 |
# methods return None instead of raising KeyError (gh-88758). |
| 456 |
menu = tkinter.Menu(self.root, tearoff=1) |
| 457 |
menu.add_command(label='Hello') |
| 458 |
tearoff = self.root.tk.call('tk::TearOffMenu', str(menu), 0, 0) |
| 459 |
self.addCleanup(self.root.tk.call, 'destroy', tearoff) |
| 460 |
self.root.update() |
| 461 |
self.assertRaises(KeyError, self.root.nametowidget, tearoff) |
| 462 |
|
| 463 |
self.root.tk.call('focus', '-force', tearoff) |
| 464 |
self.root.update() |
| 465 |
self.assertIsNone(self.root.focus_get()) |
| 466 |
self.assertIsNone(self.root.focus_displayof()) |
| 467 |
|
| 468 |
def test_grab(self): |
| 469 |
f = tkinter.Frame(self.root) |
| 470 |
f.pack() |
| 471 |
self.root.wait_visibility() |
| 472 |
self.assertIsNone(self.root.grab_current()) |
| 473 |
self.assertIsNone(f.grab_status()) |
| 474 |
f.grab_set() |
| 475 |
self.assertEqual(f.grab_status(), 'local') |
| 476 |
self.assertIs(self.root.grab_current(), f) |
| 477 |
f.grab_release() |
| 478 |
self.assertIsNone(f.grab_status()) |
| 479 |
self.assertIsNone(self.root.grab_current()) |
| 480 |
|
| 481 |
def test_selection_own(self): |
| 482 |
self.root.selection_own() |
| 483 |
self.assertIs(self.root.selection_own_get(), self.root) |
| 484 |
f = tkinter.Frame(self.root) |
| 485 |
f.selection_own() |
| 486 |
self.assertIs(self.root.selection_own_get(), f) |
| 487 |
|
| 488 |
def test_event_add_delete_info(self): |
| 489 |
self.addCleanup(self.root.event_delete, '<<TestEvent>>') |
| 490 |
self.root.event_add('<<TestEvent>>', '<Control-z>', '<Control-y>') |
| 491 |
self.assertEqual(self.root.event_info('<<TestEvent>>'), |
| 492 |
('<Control-Key-z>', '<Control-Key-y>')) |
| 493 |
self.assertIn('<<TestEvent>>', self.root.event_info()) |
| 494 |
self.root.event_delete('<<TestEvent>>', '<Control-y>') |
| 495 |
self.assertEqual(self.root.event_info('<<TestEvent>>'), |
| 496 |
('<Control-Key-z>',)) |
| 497 |
self.root.event_delete('<<TestEvent>>') |
| 498 |
self.assertEqual(self.root.event_info('<<TestEvent>>'), ()) |
| 499 |
self.assertRaises(TypeError, self.root.event_delete) |
| 500 |
|
| 501 |
def test_bell(self): |
| 502 |
self.root.bell() # No exception. |
| 503 |
self.root.bell(displayof=self.root) |
| 504 |
|
| 505 |
def test_tk_focusNext_focusPrev(self): |
| 506 |
f = tkinter.Frame(self.root) |
| 507 |
f.pack() |
| 508 |
entries = [tkinter.Entry(f) for _ in range(3)] |
| 509 |
for entry in entries: |
| 510 |
entry.pack() |
| 511 |
# tk_focusNext skips widgets that are not viewable. |
| 512 |
entries[-1].wait_visibility() |
| 513 |
self.assertIs(entries[0].tk_focusNext(), entries[1]) |
| 514 |
self.assertIs(entries[1].tk_focusNext(), entries[2]) |
| 515 |
self.assertIs(entries[2].tk_focusPrev(), entries[1]) |
| 516 |
self.assertIs(entries[1].tk_focusPrev(), entries[0]) |
| 517 |
self.assertRaises(TypeError, entries[0].tk_focusNext, 'x') |
| 518 |
self.assertRaises(TypeError, entries[0].tk_focusPrev, 'x') |
| 519 |
|
| 520 |
def test_tk_strictMotif(self): |
| 521 |
self.addCleanup(self.root.tk_strictMotif, False) |
| 522 |
self.assertIs(self.root.tk_strictMotif(), False) |
| 523 |
self.assertIs(self.root.tk_strictMotif(True), True) |
| 524 |
self.assertIs(self.root.tk_strictMotif(), True) |
| 525 |
self.assertIs(self.root.tk_strictMotif(False), False) |
| 526 |
self.assertRaises(TypeError, self.root.tk_strictMotif, 1, 2) |
| 527 |
|
| 528 |
def test_tk_bisque(self): |
| 529 |
# tk_bisque resets the color palette; use a separate root so that |
| 530 |
# the shared one is not affected. |
| 531 |
root = tkinter.Tk() |
| 532 |
self.addCleanup(root.destroy) |
| 533 |
root.tk_bisque() |
| 534 |
self.assertEqual(root['background'], '#ffe4c4') |
| 535 |
self.assertRaises(TypeError, root.tk_bisque, 'x') |
| 536 |
|
| 537 |
def test_wait_variable(self): |
| 538 |
var = tkinter.StringVar(self.root) |
| 539 |
self.assertEqual(self.root.waitvar, self.root.wait_variable) |
| 540 |
self.root.after(1, var.set, 'done') |
| 541 |
self.root.wait_variable(var) # Returns once the variable is set. |
| 542 |
self.assertEqual(var.get(), 'done') |
| 543 |
|
| 544 |
def test_wait_window(self): |
| 545 |
top = tkinter.Toplevel(self.root) |
| 546 |
self.root.after(1, top.destroy) |
| 547 |
self.root.wait_window(top) # Returns once the window is destroyed. |
| 548 |
self.assertFalse(top.winfo_exists()) |
| 549 |
|
| 550 |
def test_tk_focusFollowsMouse(self): |
| 551 |
self.root.tk_focusFollowsMouse() # No exception. |
| 552 |
|
| 553 |
def test_selection_handle(self): |
| 554 |
f = tkinter.Frame(self.root) |
| 555 |
def handler(offset, length): |
| 556 |
return 'PAYLOAD'[int(offset):int(offset) + int(length)] |
| 557 |
f.selection_handle(handler) |
| 558 |
f.selection_own() |
| 559 |
self.assertEqual(f.selection_get(), 'PAYLOAD') |
| 560 |
|
| 561 |
def test_grab_set_global(self): |
| 562 |
# A successful global grab directs all events on the display to this |
| 563 |
# application, so only the error paths are tested here. |
| 564 |
self.assertRaises(TypeError, self.root.grab_set_global, 'extra') |
| 565 |
with self.subTest('non-viewable window'): |
| 566 |
if self.root._windowingsystem != 'x11': |
| 567 |
# Grabbing a non-viewable window fails only on X11; elsewhere |
| 568 |
# it would actually grab the whole display. |
| 569 |
self.skipTest('only X11 fails the grab') |
| 570 |
f = tkinter.Frame(self.root) # not yet viewable |
| 571 |
self.assertRaisesRegex(TclError, 'grab failed', f.grab_set_global) |
| 572 |
|
| 573 |
def test_send(self): |
| 574 |
if self.root._windowingsystem != 'x11': |
| 575 |
self.skipTest('send is only supported on X11') |
| 576 |
self.assertRaisesRegex(TclError, 'no application named', |
| 577 |
self.root.send, 'no_such_interp_xyzzy', 'set x 1') |
| 578 |
|
| 579 |
def test_event_repr_defaults(self): |
| 580 |
e = tkinter.Event() |
| 581 |
e.serial = 12345 |
| 582 |
e.num = '??' |
| 583 |
e.height = '??' |
| 584 |
e.keycode = '??' |
| 585 |
e.state = 0 |
| 586 |
e.time = 123456789 |
| 587 |
e.width = '??' |
| 588 |
e.x = '??' |
| 589 |
e.y = '??' |
| 590 |
e.char = '' |
| 591 |
e.keysym = '??' |
| 592 |
e.keysym_num = '??' |
| 593 |
e.type = '100' |
| 594 |
e.widget = '??' |
| 595 |
e.x_root = '??' |
| 596 |
e.y_root = '??' |
| 597 |
e.delta = 0 |
| 598 |
self.assertEqual(repr(e), '<100 event>') |
| 599 |
|
| 600 |
def test_event_repr(self): |
| 601 |
e = tkinter.Event() |
| 602 |
e.serial = 12345 |
| 603 |
e.num = 3 |
| 604 |
e.focus = True |
| 605 |
e.height = 200 |
| 606 |
e.keycode = 65 |
| 607 |
e.state = 0x30405 |
| 608 |
e.time = 123456789 |
| 609 |
e.width = 300 |
| 610 |
e.x = 10 |
| 611 |
e.y = 20 |
| 612 |
e.char = 'A' |
| 613 |
e.send_event = True |
| 614 |
e.keysym = 'Key-A' |
| 615 |
e.keysym_num = ord('A') |
| 616 |
e.type = tkinter.EventType.Configure |
| 617 |
e.widget = '.text' |
| 618 |
e.x_root = 1010 |
| 619 |
e.y_root = 1020 |
| 620 |
e.delta = -1 |
| 621 |
self.assertEqual(repr(e), |
| 622 |
"<Configure event send_event=True" |
| 623 |
" state=Shift|Control|Button3|0x30000" |
| 624 |
" keysym=Key-A keycode=65 char='A'" |
| 625 |
" num=3 delta=-1 focus=True" |
| 626 |
" x=10 y=20 width=300 height=200>") |
| 627 |
|
| 628 |
def test_eventtype_enum(self): |
| 629 |
class CheckedEventType(enum.StrEnum): |
| 630 |
KeyPress = '2' |
| 631 |
Key = KeyPress |
| 632 |
KeyRelease = '3' |
| 633 |
ButtonPress = '4' |
| 634 |
Button = ButtonPress |
| 635 |
ButtonRelease = '5' |
| 636 |
Motion = '6' |
| 637 |
Enter = '7' |
| 638 |
Leave = '8' |
| 639 |
FocusIn = '9' |
| 640 |
FocusOut = '10' |
| 641 |
Keymap = '11' # undocumented |
| 642 |
Expose = '12' |
| 643 |
GraphicsExpose = '13' # undocumented |
| 644 |
NoExpose = '14' # undocumented |
| 645 |
Visibility = '15' |
| 646 |
Create = '16' |
| 647 |
Destroy = '17' |
| 648 |
Unmap = '18' |
| 649 |
Map = '19' |
| 650 |
MapRequest = '20' |
| 651 |
Reparent = '21' |
| 652 |
Configure = '22' |
| 653 |
ConfigureRequest = '23' |
| 654 |
Gravity = '24' |
| 655 |
ResizeRequest = '25' |
| 656 |
Circulate = '26' |
| 657 |
CirculateRequest = '27' |
| 658 |
Property = '28' |
| 659 |
SelectionClear = '29' # undocumented |
| 660 |
SelectionRequest = '30' # undocumented |
| 661 |
Selection = '31' # undocumented |
| 662 |
Colormap = '32' |
| 663 |
ClientMessage = '33' # undocumented |
| 664 |
Mapping = '34' # undocumented |
| 665 |
VirtualEvent = '35' # undocumented |
| 666 |
Activate = '36' |
| 667 |
Deactivate = '37' |
| 668 |
MouseWheel = '38' |
| 669 |
enum._test_simple_enum(CheckedEventType, tkinter.EventType) |
| 670 |
|
| 671 |
def test_getboolean(self): |
| 672 |
for v in 'true', 'yes', 'on', '1', 't', 'y', 1, True: |
| 673 |
self.assertIs(self.root.getboolean(v), True) |
| 674 |
for v in 'false', 'no', 'off', '0', 'f', 'n', 0, False: |
| 675 |
self.assertIs(self.root.getboolean(v), False) |
| 676 |
self.assertRaises(ValueError, self.root.getboolean, 'yea') |
| 677 |
self.assertRaises(ValueError, self.root.getboolean, '') |
| 678 |
self.assertRaises(TypeError, self.root.getboolean, None) |
| 679 |
self.assertRaises(TypeError, self.root.getboolean, ()) |
| 680 |
|
| 681 |
def test_mainloop(self): |
| 682 |
log = [] |
| 683 |
def callback(): |
| 684 |
log.append(1) |
| 685 |
self.root.after(100, self.root.quit) |
| 686 |
self.root.after(100, callback) |
| 687 |
self.root.mainloop(1) |
| 688 |
self.assertEqual(log, []) |
| 689 |
self.root.mainloop(0) |
| 690 |
self.assertEqual(log, [1]) |
| 691 |
self.assertTrue(self.root.winfo_exists()) |
| 692 |
|
| 693 |
def test_info_patchlevel(self): |
| 694 |
vi = self.root.info_patchlevel() |
| 695 |
f = tkinter.Frame(self.root) |
| 696 |
self.assertEqual(f.info_patchlevel(), vi) |
| 697 |
# The following is almost a copy of tests for sys.version_info. |
| 698 |
self.assertIsInstance(vi[:], tuple) |
| 699 |
self.assertEqual(len(vi), 5) |
| 700 |
self.assertIsInstance(vi[0], int) |
| 701 |
self.assertIsInstance(vi[1], int) |
| 702 |
self.assertIsInstance(vi[2], int) |
| 703 |
self.assertIn(vi[3], ("alpha", "beta", "candidate", "final")) |
| 704 |
self.assertIsInstance(vi[4], int) |
| 705 |
self.assertIsInstance(vi.major, int) |
| 706 |
self.assertIsInstance(vi.minor, int) |
| 707 |
self.assertIsInstance(vi.micro, int) |
| 708 |
self.assertIn(vi.releaselevel, ("alpha", "beta", "final")) |
| 709 |
self.assertIsInstance(vi.serial, int) |
| 710 |
self.assertEqual(vi[0], vi.major) |
| 711 |
self.assertEqual(vi[1], vi.minor) |
| 712 |
self.assertEqual(vi[2], vi.micro) |
| 713 |
self.assertEqual(vi[3], vi.releaselevel) |
| 714 |
self.assertEqual(vi[4], vi.serial) |
| 715 |
self.assertTrue(vi > (1,0,0)) |
| 716 |
if vi.releaselevel == 'final': |
| 717 |
self.assertEqual(vi.serial, 0) |
| 718 |
else: |
| 719 |
self.assertEqual(vi.micro, 0) |
| 720 |
self.assertTrue(str(vi).startswith(f'{vi.major}.{vi.minor}')) |
| 721 |
|
| 722 |
def test_embedded_null(self): |
| 723 |
widget = tkinter.Entry(self.root) |
| 724 |
widget.insert(0, 'abc\0def') # ASCII-only |
| 725 |
widget.selection_range(0, 'end') |
| 726 |
self.assertEqual(widget.selection_get(), 'abc\x00def') |
| 727 |
widget.insert(0, '\u20ac\0') # non-ASCII |
| 728 |
widget.selection_range(0, 'end') |
| 729 |
self.assertEqual(widget.selection_get(), '\u20ac\0abc\x00def') |
| 730 |
|
| 731 |
|
| 732 |
class TkTest(AbstractTkTest, unittest.TestCase): |
| 733 |
|
| 734 |
def test_className(self): |
| 735 |
# The className argument sets the class of the root window. Tk |
| 736 |
# title-cases it: the first letter is upper-cased, the rest lower-cased. |
| 737 |
cases = [ |
| 738 |
('fooBAR', 'Foobar'), |
| 739 |
('éÉ', 'Éé'), # small and capital E WITH ACUTE |
| 740 |
] |
| 741 |
if tcl_version >= (9, 0): |
| 742 |
# small and capital DESERET LETTER LONG I (a non-BMP script) |
| 743 |
cases.append(('\U00010428\U00010400', '\U00010400\U00010428')) |
| 744 |
for className, klass in cases: |
| 745 |
root = tkinter.Tk(className=className) |
| 746 |
try: |
| 747 |
self.assertEqual(root.winfo_class(), klass) |
| 748 |
finally: |
| 749 |
root.destroy() |
| 750 |
if tcl_version < (9, 0): |
| 751 |
# gh-126219: title-casing a non-BMP first letter crashed Tcl 8.x; |
| 752 |
# such a class name is now rejected. |
| 753 |
self.assertRaises(ValueError, tkinter.Tk, className='\U00010428') |
| 754 |
|
| 755 |
|
| 756 |
class WinfoTest(AbstractTkTest, unittest.TestCase): |
| 757 |
|
| 758 |
def test_winfo_rgb(self): |
| 759 |
|
| 760 |
def assertApprox(col1, col2): |
| 761 |
# A small amount of flexibility is required (bpo-45496) |
| 762 |
# 33 is ~0.05% of 65535, which is a reasonable margin |
| 763 |
for col1_channel, col2_channel in zip(col1, col2): |
| 764 |
self.assertAlmostEqual(col1_channel, col2_channel, delta=33) |
| 765 |
|
| 766 |
root = self.root |
| 767 |
rgb = root.winfo_rgb |
| 768 |
|
| 769 |
# Color name. |
| 770 |
self.assertEqual(rgb('red'), (65535, 0, 0)) |
| 771 |
self.assertEqual(rgb('dark slate blue'), (18504, 15677, 35723)) |
| 772 |
# #RGB - extends each 4-bit hex value to be 16-bit. |
| 773 |
self.assertEqual(rgb('#F0F'), (0xFFFF, 0x0000, 0xFFFF)) |
| 774 |
# #RRGGBB - extends each 8-bit hex value to be 16-bit. |
| 775 |
assertApprox(rgb('#4a3c8c'), (0x4a4a, 0x3c3c, 0x8c8c)) |
| 776 |
# #RRRRGGGGBBBB |
| 777 |
assertApprox(rgb('#dede14143939'), (0xdede, 0x1414, 0x3939)) |
| 778 |
# Invalid string. |
| 779 |
with self.assertRaises(tkinter.TclError): |
| 780 |
rgb('#123456789a') |
| 781 |
# RGB triplet is invalid input. |
| 782 |
with self.assertRaises(tkinter.TclError): |
| 783 |
rgb((111, 78, 55)) |
| 784 |
|
| 785 |
def test_winfo_pathname(self): |
| 786 |
t = tkinter.Toplevel(self.root) |
| 787 |
w = tkinter.Button(t) |
| 788 |
wid = w.winfo_id() |
| 789 |
self.assertIsInstance(wid, int) |
| 790 |
self.assertEqual(self.root.winfo_pathname(hex(wid)), str(w)) |
| 791 |
self.assertEqual(self.root.winfo_pathname(hex(wid), displayof=None), str(w)) |
| 792 |
self.assertEqual(self.root.winfo_pathname(hex(wid), displayof=t), str(w)) |
| 793 |
self.assertEqual(self.root.winfo_pathname(wid), str(w)) |
| 794 |
self.assertEqual(self.root.winfo_pathname(wid, displayof=None), str(w)) |
| 795 |
self.assertEqual(self.root.winfo_pathname(wid, displayof=t), str(w)) |
| 796 |
|
| 797 |
def test_winfo_class_name_parent(self): |
| 798 |
f = tkinter.Frame(self.root) |
| 799 |
b = tkinter.Button(f) |
| 800 |
self.assertEqual(f.winfo_class(), 'Frame') |
| 801 |
self.assertEqual(b.winfo_class(), 'Button') |
| 802 |
self.assertEqual(b.winfo_name(), str(b).rsplit('.', 1)[-1]) |
| 803 |
self.assertEqual(b.winfo_parent(), str(f)) |
| 804 |
self.assertEqual(f.winfo_parent(), str(self.root)) |
| 805 |
self.assertIs(f.winfo_toplevel(), self.root) |
| 806 |
t = tkinter.Toplevel(self.root) |
| 807 |
self.assertIs(tkinter.Button(t).winfo_toplevel(), t) |
| 808 |
self.assertEqual(self.root.nametowidget(b.winfo_parent()), f) |
| 809 |
|
| 810 |
def test_winfo_children(self): |
| 811 |
self.assertEqual(self.root.winfo_children(), []) |
| 812 |
f = tkinter.Frame(self.root) |
| 813 |
b = tkinter.Button(f) |
| 814 |
self.assertEqual(self.root.winfo_children(), [f]) |
| 815 |
self.assertEqual(f.winfo_children(), [b]) |
| 816 |
|
| 817 |
def test_winfo_visual_info(self): |
| 818 |
f = tkinter.Frame(self.root) |
| 819 |
self.assertIsInstance(f.winfo_depth(), int) |
| 820 |
self.assertIsInstance(f.winfo_cells(), int) |
| 821 |
self.assertIsInstance(f.winfo_visual(), str) |
| 822 |
self.assertIsInstance(f.winfo_visualid(), str) |
| 823 |
self.assertIsInstance(f.winfo_colormapfull(), bool) |
| 824 |
visuals = self.root.winfo_visualsavailable() |
| 825 |
self.assertIsInstance(visuals, list) |
| 826 |
for name, depth in visuals: |
| 827 |
self.assertIsInstance(name, str) |
| 828 |
self.assertIsInstance(depth, int) |
| 829 |
|
| 830 |
def test_winfo_viewable(self): |
| 831 |
f = tkinter.Frame(self.root) |
| 832 |
self.assertFalse(f.winfo_viewable()) |
| 833 |
f.pack() |
| 834 |
f.wait_visibility() |
| 835 |
self.root.update() |
| 836 |
self.assertTrue(f.winfo_viewable()) |
| 837 |
|
| 838 |
def test_winfo_atom(self): |
| 839 |
atom = self.root.winfo_atom('PRIMARY') |
| 840 |
self.assertIsInstance(atom, int) |
| 841 |
self.assertEqual(self.root.winfo_atomname(atom), 'PRIMARY') |
| 842 |
self.assertEqual( |
| 843 |
self.root.winfo_atomname(atom, displayof=self.root), 'PRIMARY') |
| 844 |
self.assertEqual( |
| 845 |
self.root.winfo_atom('PRIMARY', displayof=self.root), atom) |
| 846 |
self.assertRaisesRegex(TclError, 'no atom exists', |
| 847 |
self.root.winfo_atomname, 10 ** 9) |
| 848 |
|
| 849 |
def test_winfo_pointer(self): |
| 850 |
self.assertIsInstance(self.root.winfo_pointerx(), int) |
| 851 |
self.assertIsInstance(self.root.winfo_pointery(), int) |
| 852 |
xy = self.root.winfo_pointerxy() |
| 853 |
self.assertIsInstance(xy, tuple) |
| 854 |
self.assertEqual(len(xy), 2) |
| 855 |
self.assertTrue(all(isinstance(v, int) for v in xy)) |
| 856 |
|
| 857 |
def test_winfo_containing(self): |
| 858 |
self.root.update() |
| 859 |
# No window contains a point far off the screen. |
| 860 |
self.assertIsNone(self.root.winfo_containing(-10000, -10000)) |
| 861 |
self.assertIsNone( |
| 862 |
self.root.winfo_containing(-10000, -10000, displayof=self.root)) |
| 863 |
|
| 864 |
def test_winfo_fpixels(self): |
| 865 |
self.assertIsInstance(self.root.winfo_fpixels('1i'), float) |
| 866 |
self.assertAlmostEqual(self.root.winfo_fpixels('1i'), |
| 867 |
self.root.winfo_fpixels('72p')) |
| 868 |
# Tk < 9 reports 'bad screen distance "spam"', Tk 9 reports |
| 869 |
# 'expected screen distance ... but got "spam"'. |
| 870 |
self.assertRaisesRegex(TclError, |
| 871 |
r'(bad|expected) screen distance.*"spam"', |
| 872 |
self.root.winfo_fpixels, 'spam') |
| 873 |
|
| 874 |
def test_winfo_screen(self): |
| 875 |
for name in ('winfo_screenwidth', 'winfo_screenheight', |
| 876 |
'winfo_screenmmwidth', 'winfo_screenmmheight', |
| 877 |
'winfo_screencells', 'winfo_screendepth'): |
| 878 |
value = getattr(self.root, name)() |
| 879 |
self.assertIsInstance(value, int) |
| 880 |
self.assertGreater(value, 0) |
| 881 |
self.assertIsInstance(self.root.winfo_screenvisual(), str) |
| 882 |
self.assertIsInstance(self.root.winfo_screen(), str) |
| 883 |
self.assertIsInstance(self.root.winfo_server(), str) |
| 884 |
|
| 885 |
def test_winfo_vroot(self): |
| 886 |
for name in ('winfo_vrootwidth', 'winfo_vrootheight', |
| 887 |
'winfo_vrootx', 'winfo_vrooty'): |
| 888 |
self.assertIsInstance(getattr(self.root, name)(), int) |
| 889 |
|
| 890 |
def test_winfo_interps(self): |
| 891 |
interps = self.root.winfo_interps() |
| 892 |
self.assertIsInstance(interps, tuple) |
| 893 |
# The registry of interpreters is only populated where "send" is |
| 894 |
# supported (i.e. X11), so do not require this interpreter's name. |
| 895 |
if self.root._windowingsystem == 'x11': |
| 896 |
self.assertIn(self.root.tk.call('tk', 'appname'), interps) |
| 897 |
self.assertEqual(self.root.winfo_interps(displayof=self.root), interps) |
| 898 |
|
| 899 |
|
| 900 |
class WmTest(AbstractTkTest, unittest.TestCase): |
| 901 |
|
| 902 |
def test_wm_attribute(self): |
| 903 |
w = self.root |
| 904 |
attributes = w.wm_attributes(return_python_dict=True) |
| 905 |
self.assertIsInstance(attributes, dict) |
| 906 |
attributes2 = w.wm_attributes() |
| 907 |
self.assertIsInstance(attributes2, tuple) |
| 908 |
self.assertEqual(attributes2[::2], |
| 909 |
tuple('-' + k for k in attributes)) |
| 910 |
self.assertEqual(attributes2[1::2], tuple(attributes.values())) |
| 911 |
# silently deprecated |
| 912 |
attributes3 = w.wm_attributes(None) |
| 913 |
if self.wantobjects: |
| 914 |
self.assertEqual(attributes3, attributes2) |
| 915 |
else: |
| 916 |
self.assertIsInstance(attributes3, str) |
| 917 |
|
| 918 |
for name in attributes: |
| 919 |
self.assertEqual(w.wm_attributes(name), attributes[name]) |
| 920 |
# silently deprecated |
| 921 |
for name in attributes: |
| 922 |
self.assertEqual(w.wm_attributes('-' + name), attributes[name]) |
| 923 |
|
| 924 |
self.assertIn('alpha', attributes) |
| 925 |
self.assertIn('fullscreen', attributes) |
| 926 |
self.assertIn('topmost', attributes) |
| 927 |
if w._windowingsystem == "win32": |
| 928 |
self.assertIn('disabled', attributes) |
| 929 |
self.assertIn('toolwindow', attributes) |
| 930 |
self.assertIn('transparentcolor', attributes) |
| 931 |
if w._windowingsystem == "aqua": |
| 932 |
self.assertIn('modified', attributes) |
| 933 |
self.assertIn('notify', attributes) |
| 934 |
self.assertIn('titlepath', attributes) |
| 935 |
self.assertIn('transparent', attributes) |
| 936 |
if w._windowingsystem == "x11": |
| 937 |
self.assertIn('type', attributes) |
| 938 |
self.assertIn('zoomed', attributes) |
| 939 |
|
| 940 |
w.wm_attributes(alpha=0.5) |
| 941 |
self.assertEqual(w.wm_attributes('alpha'), |
| 942 |
0.5 if self.wantobjects else '0.5') |
| 943 |
w.wm_attributes(alpha=1.0) |
| 944 |
self.assertEqual(w.wm_attributes('alpha'), |
| 945 |
1.0 if self.wantobjects else '1.0') |
| 946 |
# silently deprecated |
| 947 |
w.wm_attributes('-alpha', 0.5) |
| 948 |
self.assertEqual(w.wm_attributes('alpha'), |
| 949 |
0.5 if self.wantobjects else '0.5') |
| 950 |
w.wm_attributes(alpha=1.0) |
| 951 |
self.assertEqual(w.wm_attributes('alpha'), |
| 952 |
1.0 if self.wantobjects else '1.0') |
| 953 |
|
| 954 |
def test_wm_iconbitmap(self): |
| 955 |
t = tkinter.Toplevel(self.root) |
| 956 |
patchlevel = get_tk_patchlevel(t) |
| 957 |
|
| 958 |
if ( |
| 959 |
t._windowingsystem == 'aqua' |
| 960 |
and sys.platform == 'darwin' |
| 961 |
and platform.machine() == 'x86_64' |
| 962 |
and platform.mac_ver()[0].startswith('26.') |
| 963 |
): |
| 964 |
# https://github.com/python/cpython/issues/146531 |
| 965 |
# Tk bug 4a2070f0d3a99aa412bc582d386d575ca2f37323 |
| 966 |
# Not fixed as of Tk 8.6.18 and 9.0.4. |
| 967 |
self.skipTest('wm iconbitmap hangs on macOS 26 Intel') |
| 968 |
|
| 969 |
self.assertEqual(t.wm_iconbitmap(), '') |
| 970 |
t.wm_iconbitmap('hourglass') |
| 971 |
bug = False |
| 972 |
if t._windowingsystem == 'aqua': |
| 973 |
# Tk bug 13ac26b35dc55f7c37f70b39d59d7ef3e63017c8. |
| 974 |
if patchlevel < (8, 6, 17) or (9, 0) <= patchlevel < (9, 0, 2): |
| 975 |
bug = True |
| 976 |
if not bug: |
| 977 |
self.assertEqual(t.wm_iconbitmap(), 'hourglass') |
| 978 |
self.assertEqual(self.root.wm_iconbitmap(), '') |
| 979 |
t.wm_iconbitmap('') |
| 980 |
self.assertEqual(t.wm_iconbitmap(), '') |
| 981 |
|
| 982 |
if t._windowingsystem == 'win32': |
| 983 |
t.wm_iconbitmap(default='hourglass') |
| 984 |
self.assertEqual(t.wm_iconbitmap(), 'hourglass') |
| 985 |
self.assertEqual(self.root.wm_iconbitmap(), '') |
| 986 |
t.wm_iconbitmap(default='') |
| 987 |
self.assertEqual(t.wm_iconbitmap(), '') |
| 988 |
|
| 989 |
t.destroy() |
| 990 |
|
| 991 |
def test_wm_iconphoto(self): |
| 992 |
t = tkinter.Toplevel(self.root) |
| 993 |
img = tkinter.PhotoImage(master=t, width=16, height=16) |
| 994 |
t.wm_iconphoto(False, img) # No exception. |
| 995 |
t.wm_iconphoto(True, img) |
| 996 |
self.assertRaises(tkinter.TclError, t.wm_iconphoto, False, 'spam') |
| 997 |
|
| 998 |
def test_wm_title(self): |
| 999 |
t = tkinter.Toplevel(self.root) |
| 1000 |
t.title('Hello') |
| 1001 |
self.assertEqual(t.title(), 'Hello') |
| 1002 |
self.assertEqual(t.wm_title(), 'Hello') |
| 1003 |
t.wm_title('Spam') |
| 1004 |
self.assertEqual(t.title(), 'Spam') |
| 1005 |
|
| 1006 |
def test_wm_geometry(self): |
| 1007 |
t = tkinter.Toplevel(self.root) |
| 1008 |
t.geometry('200x100+10+20') |
| 1009 |
t.update() |
| 1010 |
self.assertRegex(t.geometry(), r'^200x100\+-?\d+\+-?\d+$') |
| 1011 |
self.assertEqual(t.wm_geometry(), t.geometry()) |
| 1012 |
|
| 1013 |
def test_wm_minsize_maxsize(self): |
| 1014 |
t = tkinter.Toplevel(self.root) |
| 1015 |
# Use a width above the minimum enforced by some platforms (72 on Aqua). |
| 1016 |
t.minsize(150, 100) |
| 1017 |
self.assertEqual(t.minsize(), (150, 100)) |
| 1018 |
t.maxsize(500, 600) |
| 1019 |
self.assertEqual(t.maxsize(), (500, 600)) |
| 1020 |
|
| 1021 |
def test_wm_resizable(self): |
| 1022 |
t = tkinter.Toplevel(self.root) |
| 1023 |
t.resizable(False, True) |
| 1024 |
self.assertEqual(t.resizable(), (0, 1)) |
| 1025 |
self.assertRaisesRegex(TclError, 'expected boolean value', |
| 1026 |
t.resizable, 'spam', True) |
| 1027 |
|
| 1028 |
def test_wm_aspect(self): |
| 1029 |
t = tkinter.Toplevel(self.root) |
| 1030 |
self.assertEqual(t.aspect(), None) |
| 1031 |
t.aspect(1, 2, 3, 4) |
| 1032 |
self.assertEqual(t.aspect(), (1, 2, 3, 4)) |
| 1033 |
|
| 1034 |
def test_wm_grid(self): |
| 1035 |
t = tkinter.Toplevel(self.root) |
| 1036 |
t.wm_grid(10, 10, 5, 5) |
| 1037 |
self.assertEqual(t.wm_grid(), (10, 10, 5, 5)) |
| 1038 |
|
| 1039 |
def test_wm_positionfrom_sizefrom(self): |
| 1040 |
# These set X11 size hints and may be no-ops on other platforms. |
| 1041 |
t = tkinter.Toplevel(self.root) |
| 1042 |
t.positionfrom('user') |
| 1043 |
self.assertIn(t.positionfrom(), ('user', '')) |
| 1044 |
t.sizefrom('program') |
| 1045 |
self.assertIn(t.sizefrom(), ('program', '')) |
| 1046 |
|
| 1047 |
def test_wm_focusmodel(self): |
| 1048 |
t = tkinter.Toplevel(self.root) |
| 1049 |
self.assertEqual(t.focusmodel(), 'passive') |
| 1050 |
t.focusmodel('active') |
| 1051 |
self.assertEqual(t.focusmodel(), 'active') |
| 1052 |
self.assertRaises(TclError, t.focusmodel, 'spam') |
| 1053 |
|
| 1054 |
def test_wm_iconname(self): |
| 1055 |
# WM_ICON_NAME is an X11 property and may be a no-op elsewhere. |
| 1056 |
t = tkinter.Toplevel(self.root) |
| 1057 |
t.iconname('Icon') |
| 1058 |
self.assertIn(t.iconname(), ('Icon', '')) |
| 1059 |
|
| 1060 |
def test_wm_iconposition(self): |
| 1061 |
t = tkinter.Toplevel(self.root) |
| 1062 |
t.wm_iconposition(3, 4) # An X11 hint; may be a no-op elsewhere. |
| 1063 |
if t._windowingsystem == 'x11': |
| 1064 |
self.assertEqual(t.wm_iconposition(), (3, 4)) |
| 1065 |
|
| 1066 |
def test_wm_iconmask_iconwindow(self): |
| 1067 |
if self.root._windowingsystem != 'x11': |
| 1068 |
self.skipTest('iconmask and iconwindow are X11-specific') |
| 1069 |
t = tkinter.Toplevel(self.root) |
| 1070 |
t.wm_iconmask('gray50') # No exception. |
| 1071 |
icon = tkinter.Toplevel(self.root) |
| 1072 |
t.wm_iconwindow(icon) |
| 1073 |
self.assertEqual(str(t.wm_iconwindow()), str(icon)) |
| 1074 |
|
| 1075 |
def test_wm_colormapwindows(self): |
| 1076 |
if self.root._windowingsystem != 'x11': |
| 1077 |
self.skipTest('colormapwindows is X11-specific') |
| 1078 |
t = tkinter.Toplevel(self.root) |
| 1079 |
self.assertEqual(t.wm_colormapwindows(), []) |
| 1080 |
f = tkinter.Frame(t) |
| 1081 |
t.wm_colormapwindows(f) |
| 1082 |
self.assertEqual([str(w) for w in t.wm_colormapwindows()], [str(f)]) |
| 1083 |
|
| 1084 |
def test_wm_manage_forget(self): |
| 1085 |
f = tkinter.Frame(self.root) |
| 1086 |
self.root.wm_manage(f) # Make the frame a top-level window. |
| 1087 |
self.root.wm_forget(f) # Revert it; no exception either way. |
| 1088 |
|
| 1089 |
def test_wm_client_command(self): |
| 1090 |
t = tkinter.Toplevel(self.root) |
| 1091 |
t.client('myhost') |
| 1092 |
t.wm_command('myapp -x') |
| 1093 |
# WM_CLIENT_MACHINE and WM_COMMAND are X11 properties; elsewhere the |
| 1094 |
# setters may be no-ops and wm_command may return a split list. |
| 1095 |
if t._windowingsystem == 'x11': |
| 1096 |
self.assertEqual(t.client(), 'myhost') |
| 1097 |
self.assertEqual(t.wm_command(), 'myapp -x') |
| 1098 |
|
| 1099 |
def test_wm_overrideredirect(self): |
| 1100 |
t = tkinter.Toplevel(self.root) |
| 1101 |
self.assertFalse(t.overrideredirect()) |
| 1102 |
t.overrideredirect(True) |
| 1103 |
self.assertTrue(t.overrideredirect()) |
| 1104 |
|
| 1105 |
def test_wm_state(self): |
| 1106 |
t = tkinter.Toplevel(self.root) |
| 1107 |
t.update() |
| 1108 |
self.assertEqual(t.state(), 'normal') |
| 1109 |
t.withdraw() |
| 1110 |
self.assertEqual(t.state(), 'withdrawn') |
| 1111 |
t.deiconify() |
| 1112 |
t.update() |
| 1113 |
self.assertEqual(t.state(), 'normal') |
| 1114 |
self.assertRaises(TclError, t.state, 'spam') |
| 1115 |
|
| 1116 |
def test_wm_frame(self): |
| 1117 |
t = tkinter.Toplevel(self.root) |
| 1118 |
t.update() |
| 1119 |
self.assertIsInstance(t.frame(), str) |
| 1120 |
|
| 1121 |
def test_wm_group(self): |
| 1122 |
# The window group is an X11 concept and may be a no-op elsewhere. |
| 1123 |
t = tkinter.Toplevel(self.root) |
| 1124 |
t.group(self.root) |
| 1125 |
self.assertIn(t.group(), (str(self.root), '')) |
| 1126 |
|
| 1127 |
def test_wm_protocol(self): |
| 1128 |
t = tkinter.Toplevel(self.root) |
| 1129 |
self.assertIsInstance(t.protocol(), tuple) |
| 1130 |
t.protocol('WM_SAVE_YOURSELF', lambda: None) |
| 1131 |
self.assertIn('WM_SAVE_YOURSELF', t.protocol()) |
| 1132 |
# Querying a single protocol returns the bound command name. |
| 1133 |
self.assertTrue(t.protocol('WM_SAVE_YOURSELF')) |
| 1134 |
|
| 1135 |
def test_wm_transient(self): |
| 1136 |
t = tkinter.Toplevel(self.root) |
| 1137 |
self.assertEqual(t.transient(), '') |
| 1138 |
t.transient(self.root) |
| 1139 |
self.assertEqual(str(t.transient()), str(self.root)) |
| 1140 |
|
| 1141 |
|
| 1142 |
class EventTest(AbstractTkTest, unittest.TestCase): |
| 1143 |
|
| 1144 |
def test_focus(self): |
| 1145 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 1146 |
f.pack() |
| 1147 |
self.root.wait_visibility() # needed on Windows |
| 1148 |
self.root.update_idletasks() |
| 1149 |
|
| 1150 |
events = [] |
| 1151 |
f.bind('<FocusIn>', events.append) |
| 1152 |
|
| 1153 |
f.focus_force() |
| 1154 |
self.root.update() |
| 1155 |
self.assertEqual(len(events), 1, events) |
| 1156 |
e = events[0] |
| 1157 |
self.assertIs(e.type, tkinter.EventType.FocusIn) |
| 1158 |
self.assertIs(e.widget, f) |
| 1159 |
self.assertIsInstance(e.serial, int) |
| 1160 |
self.assertEqual(e.time, '??') |
| 1161 |
self.assertIs(e.send_event, False) |
| 1162 |
self.assertFalse(hasattr(e, 'focus')) |
| 1163 |
self.assertEqual(e.num, '??') |
| 1164 |
self.assertEqual(e.state, '??') |
| 1165 |
self.assertEqual(e.char, '??') |
| 1166 |
self.assertEqual(e.keycode, '??') |
| 1167 |
self.assertEqual(e.keysym, '??') |
| 1168 |
self.assertEqual(e.keysym_num, '??') |
| 1169 |
self.assertEqual(e.width, '??') |
| 1170 |
self.assertEqual(e.height, '??') |
| 1171 |
self.assertEqual(e.x, '??') |
| 1172 |
self.assertEqual(e.y, '??') |
| 1173 |
self.assertEqual(e.x_root, '??') |
| 1174 |
self.assertEqual(e.y_root, '??') |
| 1175 |
self.assertEqual(e.delta, 0) |
| 1176 |
self.assertEqual(repr(e), '<FocusIn event>') |
| 1177 |
|
| 1178 |
def test_configure(self): |
| 1179 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 1180 |
f.pack() |
| 1181 |
self.root.wait_visibility() # needed on Windows |
| 1182 |
self.root.update_idletasks() |
| 1183 |
|
| 1184 |
events = [] |
| 1185 |
f.bind('<Configure>', events.append) |
| 1186 |
|
| 1187 |
f.configure(height=120, borderwidth=10) |
| 1188 |
self.assertEqual(len(events), 1, events) |
| 1189 |
e = events[0] |
| 1190 |
self.assertIs(e.type, tkinter.EventType.Configure) |
| 1191 |
self.assertIs(e.widget, f) |
| 1192 |
self.assertIsInstance(e.serial, int) |
| 1193 |
self.assertEqual(e.time, '??') |
| 1194 |
self.assertIs(e.send_event, False) |
| 1195 |
self.assertFalse(hasattr(e, 'focus')) |
| 1196 |
self.assertEqual(e.num, '??') |
| 1197 |
self.assertEqual(e.state, '??') |
| 1198 |
self.assertEqual(e.char, '??') |
| 1199 |
self.assertEqual(e.keycode, '??') |
| 1200 |
self.assertEqual(e.keysym, '??') |
| 1201 |
self.assertEqual(e.keysym_num, '??') |
| 1202 |
self.assertEqual(e.width, 150) |
| 1203 |
self.assertEqual(e.height, 100) |
| 1204 |
self.assertEqual(e.x, 0) |
| 1205 |
self.assertEqual(e.y, 0) |
| 1206 |
self.assertEqual(e.x_root, '??') |
| 1207 |
self.assertEqual(e.y_root, '??') |
| 1208 |
self.assertEqual(e.delta, 0) |
| 1209 |
self.assertEqual(repr(e), '<Configure event x=0 y=0 width=150 height=100>') |
| 1210 |
|
| 1211 |
def test_event_generate_key_press(self): |
| 1212 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 1213 |
f.pack() |
| 1214 |
self.root.wait_visibility() # needed on Windows |
| 1215 |
self.root.update_idletasks() |
| 1216 |
|
| 1217 |
events = [] |
| 1218 |
f.bind('<KeyPress>', events.append) |
| 1219 |
f.focus_force() |
| 1220 |
|
| 1221 |
f.event_generate('<Alt-z>') |
| 1222 |
self.assertEqual(len(events), 1, events) |
| 1223 |
e = events[0] |
| 1224 |
self.assertIs(e.type, tkinter.EventType.KeyPress) |
| 1225 |
self.assertIs(e.widget, f) |
| 1226 |
self.assertIsInstance(e.serial, int) |
| 1227 |
self.assertEqual(e.time, 0) |
| 1228 |
self.assertIs(e.send_event, False) |
| 1229 |
self.assertFalse(hasattr(e, 'focus')) |
| 1230 |
self.assertEqual(e.num, '??') |
| 1231 |
self.assertIsInstance(e.state, int) |
| 1232 |
self.assertNotEqual(e.state, 0) |
| 1233 |
self.assertEqual(e.char, 'z') |
| 1234 |
self.assertIsInstance(e.keycode, int) |
| 1235 |
self.assertNotEqual(e.keycode, 0) |
| 1236 |
self.assertEqual(e.keysym, 'z') |
| 1237 |
self.assertEqual(e.keysym_num, ord('z')) |
| 1238 |
self.assertEqual(e.width, '??') |
| 1239 |
self.assertEqual(e.height, '??') |
| 1240 |
self.assertEqual(e.x, -1 - f.winfo_rootx()) |
| 1241 |
self.assertEqual(e.y, -1 - f.winfo_rooty()) |
| 1242 |
self.assertEqual(e.x_root, -1) |
| 1243 |
self.assertEqual(e.y_root, -1) |
| 1244 |
self.assertEqual(e.delta, 0) |
| 1245 |
self.assertEqual(repr(e), |
| 1246 |
f"<KeyPress event state={e.state:#x} " |
| 1247 |
f"keysym=z keycode={e.keycode} char='z' x={e.x} y={e.y}>") |
| 1248 |
|
| 1249 |
def test_event_generate_enter(self): |
| 1250 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 1251 |
f.pack() |
| 1252 |
self.root.wait_visibility() # needed on Windows |
| 1253 |
self.root.update_idletasks() |
| 1254 |
|
| 1255 |
events = [] |
| 1256 |
f.bind('<Enter>', events.append) |
| 1257 |
|
| 1258 |
f.event_generate('<Enter>', x=100, y=50) |
| 1259 |
self.assertEqual(len(events), 1, events) |
| 1260 |
e = events[0] |
| 1261 |
self.assertIs(e.type, tkinter.EventType.Enter) |
| 1262 |
self.assertIs(e.widget, f) |
| 1263 |
self.assertIsInstance(e.serial, int) |
| 1264 |
self.assertEqual(e.time, 0) |
| 1265 |
self.assertIs(e.send_event, False) |
| 1266 |
self.assertIs(e.focus, False) |
| 1267 |
self.assertEqual(e.num, '??') |
| 1268 |
self.assertEqual(e.state, 0) |
| 1269 |
self.assertEqual(e.char, '??') |
| 1270 |
self.assertEqual(e.keycode, '??') |
| 1271 |
self.assertEqual(e.keysym, '??') |
| 1272 |
self.assertEqual(e.keysym_num, '??') |
| 1273 |
self.assertEqual(e.width, '??') |
| 1274 |
self.assertEqual(e.height, '??') |
| 1275 |
self.assertEqual(e.x, 100) |
| 1276 |
self.assertEqual(e.y, 50) |
| 1277 |
self.assertEqual(e.x_root, 100 + f.winfo_rootx()) |
| 1278 |
self.assertEqual(e.y_root, 50 + f.winfo_rooty()) |
| 1279 |
self.assertEqual(e.delta, 0) |
| 1280 |
self.assertEqual(repr(e), '<Enter event focus=False x=100 y=50>') |
| 1281 |
|
| 1282 |
def test_event_generate_button_press(self): |
| 1283 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 1284 |
f.pack() |
| 1285 |
self.root.wait_visibility() # needed on Windows |
| 1286 |
self.root.update_idletasks() |
| 1287 |
|
| 1288 |
events = [] |
| 1289 |
f.bind('<ButtonPress>', events.append) |
| 1290 |
f.focus_force() |
| 1291 |
|
| 1292 |
f.event_generate('<Button-1>', x=100, y=50) |
| 1293 |
self.assertEqual(len(events), 1, events) |
| 1294 |
e = events[0] |
| 1295 |
self.assertIs(e.type, tkinter.EventType.ButtonPress) |
| 1296 |
self.assertIs(e.widget, f) |
| 1297 |
self.assertIsInstance(e.serial, int) |
| 1298 |
self.assertEqual(e.time, 0) |
| 1299 |
self.assertIs(e.send_event, False) |
| 1300 |
self.assertFalse(hasattr(e, 'focus')) |
| 1301 |
self.assertEqual(e.num, 1) |
| 1302 |
self.assertEqual(e.state, 0) |
| 1303 |
self.assertEqual(e.char, '??') |
| 1304 |
self.assertEqual(e.keycode, '??') |
| 1305 |
self.assertEqual(e.keysym, '??') |
| 1306 |
self.assertEqual(e.keysym_num, '??') |
| 1307 |
self.assertEqual(e.width, '??') |
| 1308 |
self.assertEqual(e.height, '??') |
| 1309 |
self.assertEqual(e.x, 100) |
| 1310 |
self.assertEqual(e.y, 50) |
| 1311 |
self.assertEqual(e.x_root, f.winfo_rootx() + 100) |
| 1312 |
self.assertEqual(e.y_root, f.winfo_rooty() + 50) |
| 1313 |
self.assertEqual(e.delta, 0) |
| 1314 |
self.assertEqual(repr(e), '<ButtonPress event num=1 x=100 y=50>') |
| 1315 |
|
| 1316 |
def test_event_generate_motion(self): |
| 1317 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 1318 |
f.pack() |
| 1319 |
self.root.wait_visibility() # needed on Windows |
| 1320 |
self.root.update_idletasks() |
| 1321 |
|
| 1322 |
events = [] |
| 1323 |
f.bind('<Motion>', events.append) |
| 1324 |
f.focus_force() |
| 1325 |
|
| 1326 |
f.event_generate('<B1-Motion>', x=100, y=50) |
| 1327 |
self.assertEqual(len(events), 1, events) |
| 1328 |
e = events[0] |
| 1329 |
self.assertIs(e.type, tkinter.EventType.Motion) |
| 1330 |
self.assertIs(e.widget, f) |
| 1331 |
self.assertIsInstance(e.serial, int) |
| 1332 |
self.assertEqual(e.time, 0) |
| 1333 |
self.assertIs(e.send_event, False) |
| 1334 |
self.assertFalse(hasattr(e, 'focus')) |
| 1335 |
self.assertEqual(e.num, '??') |
| 1336 |
self.assertEqual(e.state, 0x100) |
| 1337 |
self.assertEqual(e.char, '??') |
| 1338 |
self.assertEqual(e.keycode, '??') |
| 1339 |
self.assertEqual(e.keysym, '??') |
| 1340 |
self.assertEqual(e.keysym_num, '??') |
| 1341 |
self.assertEqual(e.width, '??') |
| 1342 |
self.assertEqual(e.height, '??') |
| 1343 |
self.assertEqual(e.x, 100) |
| 1344 |
self.assertEqual(e.y, 50) |
| 1345 |
self.assertEqual(e.x_root, f.winfo_rootx() + 100) |
| 1346 |
self.assertEqual(e.y_root, f.winfo_rooty() + 50) |
| 1347 |
self.assertEqual(e.delta, 0) |
| 1348 |
self.assertEqual(repr(e), '<Motion event state=Button1 x=100 y=50>') |
| 1349 |
|
| 1350 |
def test_event_generate_mouse_wheel(self): |
| 1351 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 1352 |
f.pack() |
| 1353 |
self.root.wait_visibility() # needed on Windows |
| 1354 |
self.root.update_idletasks() |
| 1355 |
|
| 1356 |
events = [] |
| 1357 |
f.bind('<MouseWheel>', events.append) |
| 1358 |
f.focus_force() |
| 1359 |
|
| 1360 |
f.event_generate('<MouseWheel>', x=100, y=50, delta=-5) |
| 1361 |
self.assertEqual(len(events), 1, events) |
| 1362 |
e = events[0] |
| 1363 |
self.assertIs(e.type, tkinter.EventType.MouseWheel) |
| 1364 |
self.assertIs(e.widget, f) |
| 1365 |
self.assertIsInstance(e.serial, int) |
| 1366 |
self.assertIs(e.send_event, False) |
| 1367 |
self.assertFalse(hasattr(e, 'focus')) |
| 1368 |
self.assertEqual(e.time, 0) |
| 1369 |
self.assertEqual(e.num, '??') |
| 1370 |
self.assertEqual(e.state, 0) |
| 1371 |
self.assertEqual(e.char, '??') |
| 1372 |
self.assertEqual(e.keycode, '??') |
| 1373 |
self.assertEqual(e.keysym, '??') |
| 1374 |
self.assertEqual(e.keysym_num, '??') |
| 1375 |
self.assertEqual(e.width, '??') |
| 1376 |
self.assertEqual(e.height, '??') |
| 1377 |
self.assertEqual(e.x, 100) |
| 1378 |
self.assertEqual(e.y, 50) |
| 1379 |
self.assertEqual(e.x_root, f.winfo_rootx() + 100) |
| 1380 |
self.assertEqual(e.y_root, f.winfo_rooty() + 50) |
| 1381 |
self.assertEqual(e.delta, -5) |
| 1382 |
self.assertEqual(repr(e), '<MouseWheel event delta=-5 x=100 y=50>') |
| 1383 |
|
| 1384 |
def test_generate_event_virtual_event(self): |
| 1385 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 1386 |
f.pack() |
| 1387 |
self.root.wait_visibility() # needed on Windows |
| 1388 |
self.root.update_idletasks() |
| 1389 |
|
| 1390 |
events = [] |
| 1391 |
f.bind('<<Spam>>', events.append) |
| 1392 |
f.focus_force() |
| 1393 |
|
| 1394 |
f.event_generate('<<Spam>>', x=50) |
| 1395 |
self.assertEqual(len(events), 1, events) |
| 1396 |
e = events[0] |
| 1397 |
self.assertIs(e.type, tkinter.EventType.VirtualEvent) |
| 1398 |
self.assertIs(e.widget, f) |
| 1399 |
self.assertIsInstance(e.serial, int) |
| 1400 |
self.assertEqual(e.time, 0) |
| 1401 |
self.assertIs(e.send_event, False) |
| 1402 |
self.assertFalse(hasattr(e, 'focus')) |
| 1403 |
self.assertEqual(e.num, '??') |
| 1404 |
self.assertEqual(e.state, 0) |
| 1405 |
self.assertEqual(e.char, '??') |
| 1406 |
self.assertEqual(e.keycode, '??') |
| 1407 |
self.assertEqual(e.keysym, '??') |
| 1408 |
self.assertEqual(e.keysym_num, '??') |
| 1409 |
self.assertEqual(e.width, '??') |
| 1410 |
self.assertEqual(e.height, '??') |
| 1411 |
self.assertEqual(e.x, 50) |
| 1412 |
self.assertEqual(e.y, 0) |
| 1413 |
self.assertEqual(e.x_root, f.winfo_rootx() + 50) |
| 1414 |
self.assertEqual(e.y_root, -1) |
| 1415 |
self.assertEqual(e.delta, 0) |
| 1416 |
self.assertEqual(repr(e), |
| 1417 |
f"<VirtualEvent event x=50 y=0>") |
| 1418 |
|
| 1419 |
|
| 1420 |
class BindTest(AbstractTkTest, unittest.TestCase): |
| 1421 |
|
| 1422 |
def setUp(self): |
| 1423 |
super().setUp() |
| 1424 |
root = self.root |
| 1425 |
self.frame = tkinter.Frame(self.root, class_='Test', |
| 1426 |
width=150, height=100) |
| 1427 |
self.frame.pack() |
| 1428 |
|
| 1429 |
def assertCommandExist(self, funcid): |
| 1430 |
self.assertEqual(_info_commands(self.root, funcid), (funcid,)) |
| 1431 |
|
| 1432 |
def assertCommandNotExist(self, funcid): |
| 1433 |
self.assertEqual(_info_commands(self.root, funcid), ()) |
| 1434 |
|
| 1435 |
def test_bind(self): |
| 1436 |
event = '<Control-Alt-Key-a>' |
| 1437 |
f = self.frame |
| 1438 |
self.assertEqual(f.bind(), ()) |
| 1439 |
self.assertEqual(f.bind(event), '') |
| 1440 |
def test1(e): pass |
| 1441 |
def test2(e): pass |
| 1442 |
|
| 1443 |
funcid = f.bind(event, test1) |
| 1444 |
self.assertEqual(f.bind(), (event,)) |
| 1445 |
script = f.bind(event) |
| 1446 |
self.assertIn(funcid, script) |
| 1447 |
self.assertCommandExist(funcid) |
| 1448 |
|
| 1449 |
funcid2 = f.bind(event, test2, add=True) |
| 1450 |
script = f.bind(event) |
| 1451 |
self.assertIn(funcid, script) |
| 1452 |
self.assertIn(funcid2, script) |
| 1453 |
self.assertCommandExist(funcid) |
| 1454 |
self.assertCommandExist(funcid2) |
| 1455 |
|
| 1456 |
def test_unbind(self): |
| 1457 |
event = '<Control-Alt-Key-b>' |
| 1458 |
f = self.frame |
| 1459 |
self.assertEqual(f.bind(), ()) |
| 1460 |
self.assertEqual(f.bind(event), '') |
| 1461 |
def test1(e): pass |
| 1462 |
def test2(e): pass |
| 1463 |
|
| 1464 |
funcid = f.bind(event, test1) |
| 1465 |
funcid2 = f.bind(event, test2, add=True) |
| 1466 |
|
| 1467 |
self.assertRaises(TypeError, f.unbind) |
| 1468 |
f.unbind(event) |
| 1469 |
self.assertEqual(f.bind(event), '') |
| 1470 |
self.assertEqual(f.bind(), ()) |
| 1471 |
|
| 1472 |
def test_unbind2(self): |
| 1473 |
f = self.frame |
| 1474 |
f.wait_visibility() |
| 1475 |
f.focus_force() |
| 1476 |
f.update_idletasks() |
| 1477 |
event = '<Control-Alt-Key-c>' |
| 1478 |
self.assertEqual(f.bind(), ()) |
| 1479 |
self.assertEqual(f.bind(event), '') |
| 1480 |
def test1(e): events.append('a') |
| 1481 |
def test2(e): events.append('b') |
| 1482 |
def test3(e): events.append('c') |
| 1483 |
|
| 1484 |
funcid = f.bind(event, test1) |
| 1485 |
funcid2 = f.bind(event, test2, add=True) |
| 1486 |
funcid3 = f.bind(event, test3, add=True) |
| 1487 |
events = [] |
| 1488 |
f.event_generate(event) |
| 1489 |
self.assertEqual(events, ['a', 'b', 'c']) |
| 1490 |
|
| 1491 |
f.unbind(event, funcid2) |
| 1492 |
script = f.bind(event) |
| 1493 |
self.assertNotIn(funcid2, script) |
| 1494 |
self.assertIn(funcid, script) |
| 1495 |
self.assertIn(funcid3, script) |
| 1496 |
self.assertEqual(f.bind(), (event,)) |
| 1497 |
self.assertCommandNotExist(funcid2) |
| 1498 |
self.assertCommandExist(funcid) |
| 1499 |
self.assertCommandExist(funcid3) |
| 1500 |
events = [] |
| 1501 |
f.event_generate(event) |
| 1502 |
self.assertEqual(events, ['a', 'c']) |
| 1503 |
|
| 1504 |
f.unbind(event, funcid) |
| 1505 |
f.unbind(event, funcid3) |
| 1506 |
self.assertEqual(f.bind(event), '') |
| 1507 |
self.assertEqual(f.bind(), ()) |
| 1508 |
self.assertCommandNotExist(funcid) |
| 1509 |
self.assertCommandNotExist(funcid2) |
| 1510 |
self.assertCommandNotExist(funcid3) |
| 1511 |
events = [] |
| 1512 |
f.event_generate(event) |
| 1513 |
self.assertEqual(events, []) |
| 1514 |
|
| 1515 |
# non-idempotent |
| 1516 |
self.assertRaises(tkinter.TclError, f.unbind, event, funcid2) |
| 1517 |
|
| 1518 |
def test_bind_rebind(self): |
| 1519 |
event = '<Control-Alt-Key-d>' |
| 1520 |
f = self.frame |
| 1521 |
self.assertEqual(f.bind(), ()) |
| 1522 |
self.assertEqual(f.bind(event), '') |
| 1523 |
def test1(e): pass |
| 1524 |
def test2(e): pass |
| 1525 |
def test3(e): pass |
| 1526 |
|
| 1527 |
funcid = f.bind(event, test1) |
| 1528 |
funcid2 = f.bind(event, test2, add=True) |
| 1529 |
script = f.bind(event) |
| 1530 |
self.assertIn(funcid2, script) |
| 1531 |
self.assertIn(funcid, script) |
| 1532 |
self.assertCommandExist(funcid) |
| 1533 |
self.assertCommandExist(funcid2) |
| 1534 |
|
| 1535 |
funcid3 = f.bind(event, test3) |
| 1536 |
script = f.bind(event) |
| 1537 |
self.assertNotIn(funcid, script) |
| 1538 |
self.assertNotIn(funcid2, script) |
| 1539 |
self.assertIn(funcid3, script) |
| 1540 |
self.assertCommandExist(funcid3) |
| 1541 |
|
| 1542 |
def test_bind_class(self): |
| 1543 |
event = '<Control-Alt-Key-e>' |
| 1544 |
bind_class = self.root.bind_class |
| 1545 |
unbind_class = self.root.unbind_class |
| 1546 |
self.assertRaises(TypeError, bind_class) |
| 1547 |
self.assertEqual(bind_class('Test'), ()) |
| 1548 |
self.assertEqual(bind_class('Test', event), '') |
| 1549 |
self.addCleanup(unbind_class, 'Test', event) |
| 1550 |
def test1(e): pass |
| 1551 |
def test2(e): pass |
| 1552 |
|
| 1553 |
funcid = bind_class('Test', event, test1) |
| 1554 |
self.assertEqual(bind_class('Test'), (event,)) |
| 1555 |
script = bind_class('Test', event) |
| 1556 |
self.assertIn(funcid, script) |
| 1557 |
self.assertCommandExist(funcid) |
| 1558 |
|
| 1559 |
funcid2 = bind_class('Test', event, test2, add=True) |
| 1560 |
script = bind_class('Test', event) |
| 1561 |
self.assertIn(funcid, script) |
| 1562 |
self.assertIn(funcid2, script) |
| 1563 |
self.assertCommandExist(funcid) |
| 1564 |
self.assertCommandExist(funcid2) |
| 1565 |
|
| 1566 |
def test_unbind_class(self): |
| 1567 |
event = '<Control-Alt-Key-f>' |
| 1568 |
bind_class = self.root.bind_class |
| 1569 |
unbind_class = self.root.unbind_class |
| 1570 |
self.assertEqual(bind_class('Test'), ()) |
| 1571 |
self.assertEqual(bind_class('Test', event), '') |
| 1572 |
self.addCleanup(unbind_class, 'Test', event) |
| 1573 |
def test1(e): pass |
| 1574 |
def test2(e): pass |
| 1575 |
|
| 1576 |
funcid = bind_class('Test', event, test1) |
| 1577 |
funcid2 = bind_class('Test', event, test2, add=True) |
| 1578 |
|
| 1579 |
self.assertRaises(TypeError, unbind_class) |
| 1580 |
self.assertRaises(TypeError, unbind_class, 'Test') |
| 1581 |
unbind_class('Test', event) |
| 1582 |
self.assertEqual(bind_class('Test', event), '') |
| 1583 |
self.assertEqual(bind_class('Test'), ()) |
| 1584 |
self.assertCommandExist(funcid) |
| 1585 |
self.assertCommandExist(funcid2) |
| 1586 |
|
| 1587 |
unbind_class('Test', event) # idempotent |
| 1588 |
|
| 1589 |
def test_bind_class_rebind(self): |
| 1590 |
event = '<Control-Alt-Key-g>' |
| 1591 |
bind_class = self.root.bind_class |
| 1592 |
unbind_class = self.root.unbind_class |
| 1593 |
self.assertEqual(bind_class('Test'), ()) |
| 1594 |
self.assertEqual(bind_class('Test', event), '') |
| 1595 |
self.addCleanup(unbind_class, 'Test', event) |
| 1596 |
def test1(e): pass |
| 1597 |
def test2(e): pass |
| 1598 |
def test3(e): pass |
| 1599 |
|
| 1600 |
funcid = bind_class('Test', event, test1) |
| 1601 |
funcid2 = bind_class('Test', event, test2, add=True) |
| 1602 |
script = bind_class('Test', event) |
| 1603 |
self.assertIn(funcid2, script) |
| 1604 |
self.assertIn(funcid, script) |
| 1605 |
self.assertCommandExist(funcid) |
| 1606 |
self.assertCommandExist(funcid2) |
| 1607 |
|
| 1608 |
funcid3 = bind_class('Test', event, test3) |
| 1609 |
script = bind_class('Test', event) |
| 1610 |
self.assertNotIn(funcid, script) |
| 1611 |
self.assertNotIn(funcid2, script) |
| 1612 |
self.assertIn(funcid3, script) |
| 1613 |
self.assertCommandExist(funcid) |
| 1614 |
self.assertCommandExist(funcid2) |
| 1615 |
self.assertCommandExist(funcid3) |
| 1616 |
|
| 1617 |
def test_bind_all(self): |
| 1618 |
event = '<Control-Alt-Key-h>' |
| 1619 |
bind_all = self.root.bind_all |
| 1620 |
unbind_all = self.root.unbind_all |
| 1621 |
self.assertNotIn(event, bind_all()) |
| 1622 |
self.assertEqual(bind_all(event), '') |
| 1623 |
self.addCleanup(unbind_all, event) |
| 1624 |
def test1(e): pass |
| 1625 |
def test2(e): pass |
| 1626 |
|
| 1627 |
funcid = bind_all(event, test1) |
| 1628 |
self.assertIn(event, bind_all()) |
| 1629 |
script = bind_all(event) |
| 1630 |
self.assertIn(funcid, script) |
| 1631 |
self.assertCommandExist(funcid) |
| 1632 |
|
| 1633 |
funcid2 = bind_all(event, test2, add=True) |
| 1634 |
script = bind_all(event) |
| 1635 |
self.assertIn(funcid, script) |
| 1636 |
self.assertIn(funcid2, script) |
| 1637 |
self.assertCommandExist(funcid) |
| 1638 |
self.assertCommandExist(funcid2) |
| 1639 |
|
| 1640 |
def test_unbind_all(self): |
| 1641 |
event = '<Control-Alt-Key-i>' |
| 1642 |
bind_all = self.root.bind_all |
| 1643 |
unbind_all = self.root.unbind_all |
| 1644 |
self.assertNotIn(event, bind_all()) |
| 1645 |
self.assertEqual(bind_all(event), '') |
| 1646 |
self.addCleanup(unbind_all, event) |
| 1647 |
def test1(e): pass |
| 1648 |
def test2(e): pass |
| 1649 |
|
| 1650 |
funcid = bind_all(event, test1) |
| 1651 |
funcid2 = bind_all(event, test2, add=True) |
| 1652 |
|
| 1653 |
unbind_all(event) |
| 1654 |
self.assertEqual(bind_all(event), '') |
| 1655 |
self.assertNotIn(event, bind_all()) |
| 1656 |
self.assertCommandExist(funcid) |
| 1657 |
self.assertCommandExist(funcid2) |
| 1658 |
|
| 1659 |
unbind_all(event) # idempotent |
| 1660 |
|
| 1661 |
def test_bind_all_rebind(self): |
| 1662 |
event = '<Control-Alt-Key-j>' |
| 1663 |
bind_all = self.root.bind_all |
| 1664 |
unbind_all = self.root.unbind_all |
| 1665 |
self.assertNotIn(event, bind_all()) |
| 1666 |
self.assertEqual(bind_all(event), '') |
| 1667 |
self.addCleanup(unbind_all, event) |
| 1668 |
def test1(e): pass |
| 1669 |
def test2(e): pass |
| 1670 |
def test3(e): pass |
| 1671 |
|
| 1672 |
funcid = bind_all(event, test1) |
| 1673 |
funcid2 = bind_all(event, test2, add=True) |
| 1674 |
script = bind_all(event) |
| 1675 |
self.assertIn(funcid2, script) |
| 1676 |
self.assertIn(funcid, script) |
| 1677 |
self.assertCommandExist(funcid) |
| 1678 |
self.assertCommandExist(funcid2) |
| 1679 |
|
| 1680 |
funcid3 = bind_all(event, test3) |
| 1681 |
script = bind_all(event) |
| 1682 |
self.assertNotIn(funcid, script) |
| 1683 |
self.assertNotIn(funcid2, script) |
| 1684 |
self.assertIn(funcid3, script) |
| 1685 |
self.assertCommandExist(funcid) |
| 1686 |
self.assertCommandExist(funcid2) |
| 1687 |
self.assertCommandExist(funcid3) |
| 1688 |
|
| 1689 |
def _test_tag_bind(self, w): |
| 1690 |
tag = 'sel' |
| 1691 |
event = '<Control-Alt-Key-a>' |
| 1692 |
w.pack() |
| 1693 |
self.assertRaises(TypeError, w.tag_bind) |
| 1694 |
tag_bind = w._tag_bind if isinstance(w, tkinter.Text) else w.tag_bind |
| 1695 |
if isinstance(w, tkinter.Text): |
| 1696 |
self.assertRaises(TypeError, w.tag_bind, tag) |
| 1697 |
self.assertRaises(TypeError, w.tag_bind, tag, event) |
| 1698 |
self.assertEqual(tag_bind(tag), ()) |
| 1699 |
self.assertEqual(tag_bind(tag, event), '') |
| 1700 |
def test1(e): pass |
| 1701 |
def test2(e): pass |
| 1702 |
|
| 1703 |
funcid = w.tag_bind(tag, event, test1) |
| 1704 |
self.assertEqual(tag_bind(tag), (event,)) |
| 1705 |
script = tag_bind(tag, event) |
| 1706 |
self.assertIn(funcid, script) |
| 1707 |
self.assertCommandExist(funcid) |
| 1708 |
|
| 1709 |
funcid2 = w.tag_bind(tag, event, test2, add=True) |
| 1710 |
script = tag_bind(tag, event) |
| 1711 |
self.assertIn(funcid, script) |
| 1712 |
self.assertIn(funcid2, script) |
| 1713 |
self.assertCommandExist(funcid) |
| 1714 |
self.assertCommandExist(funcid2) |
| 1715 |
|
| 1716 |
def _test_tag_unbind(self, w): |
| 1717 |
tag = 'sel' |
| 1718 |
event = '<Control-Alt-Key-b>' |
| 1719 |
w.pack() |
| 1720 |
tag_bind = w._tag_bind if isinstance(w, tkinter.Text) else w.tag_bind |
| 1721 |
self.assertEqual(tag_bind(tag), ()) |
| 1722 |
self.assertEqual(tag_bind(tag, event), '') |
| 1723 |
def test1(e): pass |
| 1724 |
def test2(e): pass |
| 1725 |
|
| 1726 |
funcid = w.tag_bind(tag, event, test1) |
| 1727 |
funcid2 = w.tag_bind(tag, event, test2, add=True) |
| 1728 |
|
| 1729 |
self.assertRaises(TypeError, w.tag_unbind, tag) |
| 1730 |
w.tag_unbind(tag, event) |
| 1731 |
self.assertEqual(tag_bind(tag, event), '') |
| 1732 |
self.assertEqual(tag_bind(tag), ()) |
| 1733 |
|
| 1734 |
def _test_tag_bind_rebind(self, w): |
| 1735 |
tag = 'sel' |
| 1736 |
event = '<Control-Alt-Key-d>' |
| 1737 |
w.pack() |
| 1738 |
tag_bind = w._tag_bind if isinstance(w, tkinter.Text) else w.tag_bind |
| 1739 |
self.assertEqual(tag_bind(tag), ()) |
| 1740 |
self.assertEqual(tag_bind(tag, event), '') |
| 1741 |
def test1(e): pass |
| 1742 |
def test2(e): pass |
| 1743 |
def test3(e): pass |
| 1744 |
|
| 1745 |
funcid = w.tag_bind(tag, event, test1) |
| 1746 |
funcid2 = w.tag_bind(tag, event, test2, add=True) |
| 1747 |
script = tag_bind(tag, event) |
| 1748 |
self.assertIn(funcid2, script) |
| 1749 |
self.assertIn(funcid, script) |
| 1750 |
self.assertCommandExist(funcid) |
| 1751 |
self.assertCommandExist(funcid2) |
| 1752 |
|
| 1753 |
funcid3 = w.tag_bind(tag, event, test3) |
| 1754 |
script = tag_bind(tag, event) |
| 1755 |
self.assertNotIn(funcid, script) |
| 1756 |
self.assertNotIn(funcid2, script) |
| 1757 |
self.assertIn(funcid3, script) |
| 1758 |
self.assertCommandExist(funcid3) |
| 1759 |
|
| 1760 |
def test_canvas_tag_bind(self): |
| 1761 |
c = tkinter.Canvas(self.frame) |
| 1762 |
self._test_tag_bind(c) |
| 1763 |
|
| 1764 |
def test_canvas_tag_unbind(self): |
| 1765 |
c = tkinter.Canvas(self.frame) |
| 1766 |
self._test_tag_unbind(c) |
| 1767 |
|
| 1768 |
def test_canvas_tag_bind_rebind(self): |
| 1769 |
c = tkinter.Canvas(self.frame) |
| 1770 |
self._test_tag_bind_rebind(c) |
| 1771 |
|
| 1772 |
def test_text_tag_bind(self): |
| 1773 |
t = tkinter.Text(self.frame) |
| 1774 |
self._test_tag_bind(t) |
| 1775 |
|
| 1776 |
def test_text_tag_unbind(self): |
| 1777 |
t = tkinter.Text(self.frame) |
| 1778 |
self._test_tag_unbind(t) |
| 1779 |
|
| 1780 |
def test_text_tag_bind_rebind(self): |
| 1781 |
t = tkinter.Text(self.frame) |
| 1782 |
self._test_tag_bind_rebind(t) |
| 1783 |
|
| 1784 |
def test_bindtags(self): |
| 1785 |
f = self.frame |
| 1786 |
self.assertEqual(self.root.bindtags(), ('.', 'Tk', 'all')) |
| 1787 |
self.assertEqual(f.bindtags(), (str(f), 'Test', '.', 'all')) |
| 1788 |
f.bindtags(('a', 'b c')) |
| 1789 |
self.assertEqual(f.bindtags(), ('a', 'b c')) |
| 1790 |
|
| 1791 |
def test_bind_events(self): |
| 1792 |
event = '<Enter>' |
| 1793 |
root = self.root |
| 1794 |
t = tkinter.Toplevel(root) |
| 1795 |
f = tkinter.Frame(t, class_='Test', width=150, height=100) |
| 1796 |
f.pack() |
| 1797 |
root.wait_visibility() # needed on Windows |
| 1798 |
root.update_idletasks() |
| 1799 |
self.addCleanup(root.unbind_class, 'Test', event) |
| 1800 |
self.addCleanup(root.unbind_class, 'Toplevel', event) |
| 1801 |
self.addCleanup(root.unbind_class, 'tag', event) |
| 1802 |
self.addCleanup(root.unbind_class, 'tag2', event) |
| 1803 |
self.addCleanup(root.unbind_all, event) |
| 1804 |
def test(what): |
| 1805 |
return lambda e: events.append((what, e.widget)) |
| 1806 |
|
| 1807 |
root.bind_all(event, test('all')) |
| 1808 |
root.bind_class('Test', event, test('frame class')) |
| 1809 |
root.bind_class('Toplevel', event, test('toplevel class')) |
| 1810 |
root.bind_class('tag', event, test('tag')) |
| 1811 |
root.bind_class('tag2', event, test('tag2')) |
| 1812 |
f.bind(event, test('frame')) |
| 1813 |
t.bind(event, test('toplevel')) |
| 1814 |
|
| 1815 |
events = [] |
| 1816 |
f.event_generate(event) |
| 1817 |
self.assertEqual(events, [ |
| 1818 |
('frame', f), |
| 1819 |
('frame class', f), |
| 1820 |
('toplevel', f), |
| 1821 |
('all', f), |
| 1822 |
]) |
| 1823 |
|
| 1824 |
events = [] |
| 1825 |
t.event_generate(event) |
| 1826 |
self.assertEqual(events, [ |
| 1827 |
('toplevel', t), |
| 1828 |
('toplevel class', t), |
| 1829 |
('all', t), |
| 1830 |
]) |
| 1831 |
|
| 1832 |
f.bindtags(('tag', 'tag3')) |
| 1833 |
events = [] |
| 1834 |
f.event_generate(event) |
| 1835 |
self.assertEqual(events, [('tag', f)]) |
| 1836 |
|
| 1837 |
|
| 1838 |
class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase): |
| 1839 |
|
| 1840 |
def test_default_root(self): |
| 1841 |
self.assertIs(tkinter._support_default_root, True) |
| 1842 |
self.assertIsNone(tkinter._default_root) |
| 1843 |
root = tkinter.Tk() |
| 1844 |
root2 = tkinter.Tk() |
| 1845 |
root3 = tkinter.Tk() |
| 1846 |
self.assertIs(tkinter._default_root, root) |
| 1847 |
root2.destroy() |
| 1848 |
self.assertIs(tkinter._default_root, root) |
| 1849 |
root.destroy() |
| 1850 |
self.assertIsNone(tkinter._default_root) |
| 1851 |
root3.destroy() |
| 1852 |
self.assertIsNone(tkinter._default_root) |
| 1853 |
|
| 1854 |
def test_no_default_root(self): |
| 1855 |
self.assertIs(tkinter._support_default_root, True) |
| 1856 |
self.assertIsNone(tkinter._default_root) |
| 1857 |
root = tkinter.Tk() |
| 1858 |
self.assertIs(tkinter._default_root, root) |
| 1859 |
tkinter.NoDefaultRoot() |
| 1860 |
self.assertIs(tkinter._support_default_root, False) |
| 1861 |
self.assertFalse(hasattr(tkinter, '_default_root')) |
| 1862 |
# repeated call is no-op |
| 1863 |
tkinter.NoDefaultRoot() |
| 1864 |
self.assertIs(tkinter._support_default_root, False) |
| 1865 |
self.assertFalse(hasattr(tkinter, '_default_root')) |
| 1866 |
root.destroy() |
| 1867 |
self.assertIs(tkinter._support_default_root, False) |
| 1868 |
self.assertFalse(hasattr(tkinter, '_default_root')) |
| 1869 |
root = tkinter.Tk() |
| 1870 |
self.assertIs(tkinter._support_default_root, False) |
| 1871 |
self.assertFalse(hasattr(tkinter, '_default_root')) |
| 1872 |
root.destroy() |
| 1873 |
|
| 1874 |
def test_getboolean(self): |
| 1875 |
self.assertRaises(RuntimeError, tkinter.getboolean, '1') |
| 1876 |
root = tkinter.Tk() |
| 1877 |
self.assertIs(tkinter.getboolean('1'), True) |
| 1878 |
self.assertRaises(ValueError, tkinter.getboolean, 'yea') |
| 1879 |
root.destroy() |
| 1880 |
tkinter.NoDefaultRoot() |
| 1881 |
self.assertRaises(RuntimeError, tkinter.getboolean, '1') |
| 1882 |
|
| 1883 |
def test_mainloop(self): |
| 1884 |
self.assertRaises(RuntimeError, tkinter.mainloop) |
| 1885 |
root = tkinter.Tk() |
| 1886 |
root.after_idle(root.quit) |
| 1887 |
tkinter.mainloop() |
| 1888 |
root.destroy() |
| 1889 |
tkinter.NoDefaultRoot() |
| 1890 |
self.assertRaises(RuntimeError, tkinter.mainloop) |
| 1891 |
|
| 1892 |
|
| 1893 |
def _info_commands(widget, pattern=None): |
| 1894 |
return widget.tk.splitlist(widget.tk.call('info', 'commands', pattern)) |
| 1895 |
|
| 1896 |
|
| 1897 |
if __name__ == "__main__": |
| 1898 |
unittest.main() |