| 1 |
import functools |
| 2 |
import unittest |
| 3 |
import tkinter |
| 4 |
from tkinter import TclError |
| 5 |
import enum |
| 6 |
from test import support |
| 7 |
from test.test_tkinter.support import setUpModule # noqa: F401 |
| 8 |
from test.test_tkinter.support import (AbstractTkTest, AbstractDefaultRootTest, |
| 9 |
requires_tk, get_tk_patchlevel) |
| 10 |
|
| 11 |
support.requires('gui') |
| 12 |
|
| 13 |
class MiscTest(AbstractTkTest, unittest.TestCase): |
| 14 |
|
| 15 |
def test_all(self): |
| 16 |
self.assertIn("Widget", tkinter.__all__) |
| 17 |
# Check that variables from tkinter.constants are also in tkinter.__all__ |
| 18 |
self.assertIn("CASCADE", tkinter.__all__) |
| 19 |
self.assertIsNotNone(tkinter.CASCADE) |
| 20 |
# Check that sys, re, and constants are not in tkinter.__all__ |
| 21 |
self.assertNotIn("re", tkinter.__all__) |
| 22 |
self.assertNotIn("sys", tkinter.__all__) |
| 23 |
self.assertNotIn("constants", tkinter.__all__) |
| 24 |
# Check that an underscored functions is not in tkinter.__all__ |
| 25 |
self.assertNotIn("_tkerror", tkinter.__all__) |
| 26 |
# Check that wantobjects is not in tkinter.__all__ |
| 27 |
self.assertNotIn("wantobjects", tkinter.__all__) |
| 28 |
|
| 29 |
def test_repr(self): |
| 30 |
t = tkinter.Toplevel(self.root, name='top') |
| 31 |
f = tkinter.Frame(t, name='child') |
| 32 |
self.assertEqual(repr(f), '<tkinter.Frame object .top.child>') |
| 33 |
|
| 34 |
def test_generated_names(self): |
| 35 |
class Button2(tkinter.Button): |
| 36 |
pass |
| 37 |
|
| 38 |
t = tkinter.Toplevel(self.root) |
| 39 |
f = tkinter.Frame(t) |
| 40 |
f2 = tkinter.Frame(t) |
| 41 |
self.assertNotEqual(str(f), str(f2)) |
| 42 |
b = tkinter.Button(f2) |
| 43 |
b2 = Button2(f2) |
| 44 |
for name in str(b).split('.') + str(b2).split('.'): |
| 45 |
self.assertFalse(name.isidentifier(), msg=repr(name)) |
| 46 |
b3 = tkinter.Button(f2) |
| 47 |
b4 = Button2(f2) |
| 48 |
self.assertEqual(len({str(b), str(b2), str(b3), str(b4)}), 4) |
| 49 |
|
| 50 |
@requires_tk(8, 6, 6) |
| 51 |
def test_tk_busy(self): |
| 52 |
root = self.root |
| 53 |
f = tkinter.Frame(root, name='myframe') |
| 54 |
f2 = tkinter.Frame(root) |
| 55 |
f.pack() |
| 56 |
f2.pack() |
| 57 |
b = tkinter.Button(f) |
| 58 |
b.pack() |
| 59 |
f.tk_busy_hold() |
| 60 |
with self.assertRaisesRegex(TclError, 'unknown option "-spam"'): |
| 61 |
f.tk_busy_configure(spam='eggs') |
| 62 |
with self.assertRaisesRegex(TclError, 'unknown option "-spam"'): |
| 63 |
f.tk_busy_cget('spam') |
| 64 |
with self.assertRaisesRegex(TclError, 'unknown option "-spam"'): |
| 65 |
f.tk_busy_configure('spam') |
| 66 |
self.assertIsInstance(f.tk_busy_configure(), dict) |
| 67 |
|
| 68 |
self.assertTrue(f.tk_busy_status()) |
| 69 |
self.assertFalse(root.tk_busy_status()) |
| 70 |
self.assertFalse(f2.tk_busy_status()) |
| 71 |
self.assertFalse(b.tk_busy_status()) |
| 72 |
self.assertIn(f, f.tk_busy_current()) |
| 73 |
self.assertIn(f, f.tk_busy_current('*.m?f*me')) |
| 74 |
self.assertNotIn(f, f.tk_busy_current('*spam')) |
| 75 |
|
| 76 |
f.tk_busy_forget() |
| 77 |
self.assertFalse(f.tk_busy_status()) |
| 78 |
self.assertFalse(f.tk_busy_current()) |
| 79 |
errmsg = r"can(no|')t find busy window.*" |
| 80 |
with self.assertRaisesRegex(TclError, errmsg): |
| 81 |
f.tk_busy_configure() |
| 82 |
with self.assertRaisesRegex(TclError, errmsg): |
| 83 |
f.tk_busy_forget() |
| 84 |
|
| 85 |
@requires_tk(8, 6, 6) |
| 86 |
def test_tk_busy_with_cursor(self): |
| 87 |
root = self.root |
| 88 |
if root._windowingsystem == 'aqua': |
| 89 |
self.skipTest('the cursor option is not supported on OSX/Aqua') |
| 90 |
f = tkinter.Frame(root, name='myframe') |
| 91 |
f.pack() |
| 92 |
f.tk_busy_hold(cursor='gumby') |
| 93 |
|
| 94 |
self.assertEqual(f.tk_busy_cget('cursor'), 'gumby') |
| 95 |
f.tk_busy_configure(cursor='heart') |
| 96 |
self.assertEqual(f.tk_busy_cget('cursor'), 'heart') |
| 97 |
self.assertEqual(f.tk_busy_configure()['cursor'][4], 'heart') |
| 98 |
self.assertEqual(f.tk_busy_configure('cursor')[4], 'heart') |
| 99 |
|
| 100 |
f.tk_busy_forget() |
| 101 |
errmsg = r"can(no|')t find busy window.*" |
| 102 |
with self.assertRaisesRegex(TclError, errmsg): |
| 103 |
f.tk_busy_cget('cursor') |
| 104 |
|
| 105 |
def test_tk_setPalette(self): |
| 106 |
root = self.root |
| 107 |
root.tk_setPalette('black') |
| 108 |
self.assertEqual(root['background'], 'black') |
| 109 |
root.tk_setPalette('white') |
| 110 |
self.assertEqual(root['background'], 'white') |
| 111 |
self.assertRaisesRegex(tkinter.TclError, |
| 112 |
'^unknown color name "spam"$', |
| 113 |
root.tk_setPalette, 'spam') |
| 114 |
|
| 115 |
root.tk_setPalette(background='black') |
| 116 |
self.assertEqual(root['background'], 'black') |
| 117 |
root.tk_setPalette(background='blue', highlightColor='yellow') |
| 118 |
self.assertEqual(root['background'], 'blue') |
| 119 |
self.assertEqual(root['highlightcolor'], 'yellow') |
| 120 |
root.tk_setPalette(background='yellow', highlightColor='blue') |
| 121 |
self.assertEqual(root['background'], 'yellow') |
| 122 |
self.assertEqual(root['highlightcolor'], 'blue') |
| 123 |
self.assertRaisesRegex(tkinter.TclError, |
| 124 |
'^unknown color name "spam"$', |
| 125 |
root.tk_setPalette, background='spam') |
| 126 |
self.assertRaisesRegex(tkinter.TclError, |
| 127 |
'^must specify a background color$', |
| 128 |
root.tk_setPalette, spam='white') |
| 129 |
self.assertRaisesRegex(tkinter.TclError, |
| 130 |
'^must specify a background color$', |
| 131 |
root.tk_setPalette, highlightColor='blue') |
| 132 |
|
| 133 |
def test_after(self): |
| 134 |
root = self.root |
| 135 |
|
| 136 |
def callback(start=0, step=1, *, end=0): |
| 137 |
nonlocal count |
| 138 |
count = start + step + end |
| 139 |
|
| 140 |
# Without function, sleeps for ms. |
| 141 |
self.assertIsNone(root.after(1)) |
| 142 |
|
| 143 |
# Set up with callback with no args. |
| 144 |
count = 0 |
| 145 |
timer1 = root.after(0, callback) |
| 146 |
self.assertIn(timer1, root.tk.call('after', 'info')) |
| 147 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) |
| 148 |
root.update() # Process all pending events. |
| 149 |
self.assertEqual(count, 1) |
| 150 |
with self.assertRaises(tkinter.TclError): |
| 151 |
root.tk.call(script) |
| 152 |
|
| 153 |
# Set up with callback with args. |
| 154 |
count = 0 |
| 155 |
timer1 = root.after(0, callback, 42, 11) |
| 156 |
root.update() # Process all pending events. |
| 157 |
self.assertEqual(count, 53) |
| 158 |
|
| 159 |
# Cancel before called. |
| 160 |
timer1 = root.after(1000, callback) |
| 161 |
self.assertIn(timer1, root.tk.call('after', 'info')) |
| 162 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) |
| 163 |
root.after_cancel(timer1) # Cancel this event. |
| 164 |
self.assertEqual(count, 53) |
| 165 |
with self.assertRaises(tkinter.TclError): |
| 166 |
root.tk.call(script) |
| 167 |
|
| 168 |
# Call with a callable class |
| 169 |
count = 0 |
| 170 |
timer1 = root.after(0, functools.partial(callback, 42, 11)) |
| 171 |
root.update() # Process all pending events. |
| 172 |
self.assertEqual(count, 53) |
| 173 |
|
| 174 |
# Set up with callback with keyword args. |
| 175 |
count = 0 |
| 176 |
timer1 = root.after(0, callback, 42, step=11, end=1) |
| 177 |
root.update() # Process all pending events. |
| 178 |
self.assertEqual(count, 54) |
| 179 |
|
| 180 |
def test_after_idle(self): |
| 181 |
root = self.root |
| 182 |
|
| 183 |
def callback(start=0, step=1, *, end=0): |
| 184 |
nonlocal count |
| 185 |
count = start + step + end |
| 186 |
|
| 187 |
# Set up with callback with no args. |
| 188 |
count = 0 |
| 189 |
idle1 = root.after_idle(callback) |
| 190 |
self.assertIn(idle1, root.tk.call('after', 'info')) |
| 191 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) |
| 192 |
root.update_idletasks() # Process all pending events. |
| 193 |
self.assertEqual(count, 1) |
| 194 |
with self.assertRaises(tkinter.TclError): |
| 195 |
root.tk.call(script) |
| 196 |
|
| 197 |
# Set up with callback with args. |
| 198 |
count = 0 |
| 199 |
idle1 = root.after_idle(callback, 42, 11) |
| 200 |
root.update_idletasks() # Process all pending events. |
| 201 |
self.assertEqual(count, 53) |
| 202 |
|
| 203 |
# Cancel before called. |
| 204 |
idle1 = root.after_idle(callback) |
| 205 |
self.assertIn(idle1, root.tk.call('after', 'info')) |
| 206 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) |
| 207 |
root.after_cancel(idle1) # Cancel this event. |
| 208 |
self.assertEqual(count, 53) |
| 209 |
with self.assertRaises(tkinter.TclError): |
| 210 |
root.tk.call(script) |
| 211 |
|
| 212 |
# Set up with callback with keyword args. |
| 213 |
count = 0 |
| 214 |
idle1 = root.after_idle(callback, 42, step=11, end=1) |
| 215 |
root.update() # Process all pending events. |
| 216 |
self.assertEqual(count, 54) |
| 217 |
|
| 218 |
def test_after_cancel(self): |
| 219 |
root = self.root |
| 220 |
|
| 221 |
def callback(): |
| 222 |
nonlocal count |
| 223 |
count += 1 |
| 224 |
|
| 225 |
timer1 = root.after(5000, callback) |
| 226 |
idle1 = root.after_idle(callback) |
| 227 |
|
| 228 |
# No value for id raises a ValueError. |
| 229 |
with self.assertRaises(ValueError): |
| 230 |
root.after_cancel(None) |
| 231 |
|
| 232 |
# Cancel timer event. |
| 233 |
count = 0 |
| 234 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) |
| 235 |
root.tk.call(script) |
| 236 |
self.assertEqual(count, 1) |
| 237 |
root.after_cancel(timer1) |
| 238 |
with self.assertRaises(tkinter.TclError): |
| 239 |
root.tk.call(script) |
| 240 |
self.assertEqual(count, 1) |
| 241 |
with self.assertRaises(tkinter.TclError): |
| 242 |
root.tk.call('after', 'info', timer1) |
| 243 |
|
| 244 |
# Cancel same event - nothing happens. |
| 245 |
root.after_cancel(timer1) |
| 246 |
|
| 247 |
# Cancel idle event. |
| 248 |
count = 0 |
| 249 |
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) |
| 250 |
root.tk.call(script) |
| 251 |
self.assertEqual(count, 1) |
| 252 |
root.after_cancel(idle1) |
| 253 |
with self.assertRaises(tkinter.TclError): |
| 254 |
root.tk.call(script) |
| 255 |
self.assertEqual(count, 1) |
| 256 |
with self.assertRaises(tkinter.TclError): |
| 257 |
root.tk.call('after', 'info', idle1) |
| 258 |
|
| 259 |
def test_after_info(self): |
| 260 |
root = self.root |
| 261 |
|
| 262 |
# No events. |
| 263 |
self.assertEqual(root.after_info(), ()) |
| 264 |
|
| 265 |
# Add timer. |
| 266 |
timer = root.after(1, lambda: 'break') |
| 267 |
|
| 268 |
# With no parameter, it returns a tuple of the event handler ids. |
| 269 |
self.assertEqual(root.after_info(), (timer, )) |
| 270 |
root.after_cancel(timer) |
| 271 |
|
| 272 |
timer1 = root.after(5000, lambda: 'break') |
| 273 |
timer2 = root.after(5000, lambda: 'break') |
| 274 |
idle1 = root.after_idle(lambda: 'break') |
| 275 |
# Only contains new events and not 'timer'. |
| 276 |
self.assertEqual(root.after_info(), (idle1, timer2, timer1)) |
| 277 |
|
| 278 |
# With a parameter returns a tuple of (script, type). |
| 279 |
timer1_info = root.after_info(timer1) |
| 280 |
self.assertEqual(len(timer1_info), 2) |
| 281 |
self.assertEqual(timer1_info[1], 'timer') |
| 282 |
idle1_info = root.after_info(idle1) |
| 283 |
self.assertEqual(len(idle1_info), 2) |
| 284 |
self.assertEqual(idle1_info[1], 'idle') |
| 285 |
|
| 286 |
root.after_cancel(timer1) |
| 287 |
with self.assertRaises(tkinter.TclError): |
| 288 |
root.after_info(timer1) |
| 289 |
root.after_cancel(timer2) |
| 290 |
with self.assertRaises(tkinter.TclError): |
| 291 |
root.after_info(timer2) |
| 292 |
root.after_cancel(idle1) |
| 293 |
with self.assertRaises(tkinter.TclError): |
| 294 |
root.after_info(idle1) |
| 295 |
|
| 296 |
# No events. |
| 297 |
self.assertEqual(root.after_info(), ()) |
| 298 |
|
| 299 |
def test_clipboard(self): |
| 300 |
root = self.root |
| 301 |
root.clipboard_clear() |
| 302 |
root.clipboard_append('Ùñî') |
| 303 |
self.assertEqual(root.clipboard_get(), 'Ùñî') |
| 304 |
root.clipboard_append('çōđě') |
| 305 |
self.assertEqual(root.clipboard_get(), 'Ùñîçōđě') |
| 306 |
root.clipboard_clear() |
| 307 |
with self.assertRaises(tkinter.TclError): |
| 308 |
root.clipboard_get() |
| 309 |
|
| 310 |
def test_clipboard_astral(self): |
| 311 |
root = self.root |
| 312 |
root.clipboard_clear() |
| 313 |
root.clipboard_append('𝔘𝔫𝔦') |
| 314 |
self.assertEqual(root.clipboard_get(), '𝔘𝔫𝔦') |
| 315 |
root.clipboard_append('𝔠𝔬𝔡𝔢') |
| 316 |
self.assertEqual(root.clipboard_get(), '𝔘𝔫𝔦𝔠𝔬𝔡𝔢') |
| 317 |
root.clipboard_clear() |
| 318 |
with self.assertRaises(tkinter.TclError): |
| 319 |
root.clipboard_get() |
| 320 |
|
| 321 |
def test_winfo_rgb(self): |
| 322 |
|
| 323 |
def assertApprox(col1, col2): |
| 324 |
# A small amount of flexibility is required (bpo-45496) |
| 325 |
# 33 is ~0.05% of 65535, which is a reasonable margin |
| 326 |
for col1_channel, col2_channel in zip(col1, col2): |
| 327 |
self.assertAlmostEqual(col1_channel, col2_channel, delta=33) |
| 328 |
|
| 329 |
root = self.root |
| 330 |
rgb = root.winfo_rgb |
| 331 |
|
| 332 |
# Color name. |
| 333 |
self.assertEqual(rgb('red'), (65535, 0, 0)) |
| 334 |
self.assertEqual(rgb('dark slate blue'), (18504, 15677, 35723)) |
| 335 |
# #RGB - extends each 4-bit hex value to be 16-bit. |
| 336 |
self.assertEqual(rgb('#F0F'), (0xFFFF, 0x0000, 0xFFFF)) |
| 337 |
# #RRGGBB - extends each 8-bit hex value to be 16-bit. |
| 338 |
assertApprox(rgb('#4a3c8c'), (0x4a4a, 0x3c3c, 0x8c8c)) |
| 339 |
# #RRRRGGGGBBBB |
| 340 |
assertApprox(rgb('#dede14143939'), (0xdede, 0x1414, 0x3939)) |
| 341 |
# Invalid string. |
| 342 |
with self.assertRaises(tkinter.TclError): |
| 343 |
rgb('#123456789a') |
| 344 |
# RGB triplet is invalid input. |
| 345 |
with self.assertRaises(tkinter.TclError): |
| 346 |
rgb((111, 78, 55)) |
| 347 |
|
| 348 |
def test_winfo_pathname(self): |
| 349 |
t = tkinter.Toplevel(self.root) |
| 350 |
w = tkinter.Button(t) |
| 351 |
wid = w.winfo_id() |
| 352 |
self.assertIsInstance(wid, int) |
| 353 |
self.assertEqual(self.root.winfo_pathname(hex(wid)), str(w)) |
| 354 |
self.assertEqual(self.root.winfo_pathname(hex(wid), displayof=None), str(w)) |
| 355 |
self.assertEqual(self.root.winfo_pathname(hex(wid), displayof=t), str(w)) |
| 356 |
self.assertEqual(self.root.winfo_pathname(wid), str(w)) |
| 357 |
self.assertEqual(self.root.winfo_pathname(wid, displayof=None), str(w)) |
| 358 |
self.assertEqual(self.root.winfo_pathname(wid, displayof=t), str(w)) |
| 359 |
|
| 360 |
def test_event_repr_defaults(self): |
| 361 |
e = tkinter.Event() |
| 362 |
e.serial = 12345 |
| 363 |
e.num = '??' |
| 364 |
e.height = '??' |
| 365 |
e.keycode = '??' |
| 366 |
e.state = 0 |
| 367 |
e.time = 123456789 |
| 368 |
e.width = '??' |
| 369 |
e.x = '??' |
| 370 |
e.y = '??' |
| 371 |
e.char = '' |
| 372 |
e.keysym = '??' |
| 373 |
e.keysym_num = '??' |
| 374 |
e.type = '100' |
| 375 |
e.widget = '??' |
| 376 |
e.x_root = '??' |
| 377 |
e.y_root = '??' |
| 378 |
e.delta = 0 |
| 379 |
self.assertEqual(repr(e), '<100 event>') |
| 380 |
|
| 381 |
def test_event_repr(self): |
| 382 |
e = tkinter.Event() |
| 383 |
e.serial = 12345 |
| 384 |
e.num = 3 |
| 385 |
e.focus = True |
| 386 |
e.height = 200 |
| 387 |
e.keycode = 65 |
| 388 |
e.state = 0x30405 |
| 389 |
e.time = 123456789 |
| 390 |
e.width = 300 |
| 391 |
e.x = 10 |
| 392 |
e.y = 20 |
| 393 |
e.char = 'A' |
| 394 |
e.send_event = True |
| 395 |
e.keysym = 'Key-A' |
| 396 |
e.keysym_num = ord('A') |
| 397 |
e.type = tkinter.EventType.Configure |
| 398 |
e.widget = '.text' |
| 399 |
e.x_root = 1010 |
| 400 |
e.y_root = 1020 |
| 401 |
e.delta = -1 |
| 402 |
self.assertEqual(repr(e), |
| 403 |
"<Configure event send_event=True" |
| 404 |
" state=Shift|Control|Button3|0x30000" |
| 405 |
" keysym=Key-A keycode=65 char='A'" |
| 406 |
" num=3 delta=-1 focus=True" |
| 407 |
" x=10 y=20 width=300 height=200>") |
| 408 |
|
| 409 |
def test_eventtype_enum(self): |
| 410 |
class CheckedEventType(enum.StrEnum): |
| 411 |
KeyPress = '2' |
| 412 |
Key = KeyPress |
| 413 |
KeyRelease = '3' |
| 414 |
ButtonPress = '4' |
| 415 |
Button = ButtonPress |
| 416 |
ButtonRelease = '5' |
| 417 |
Motion = '6' |
| 418 |
Enter = '7' |
| 419 |
Leave = '8' |
| 420 |
FocusIn = '9' |
| 421 |
FocusOut = '10' |
| 422 |
Keymap = '11' # undocumented |
| 423 |
Expose = '12' |
| 424 |
GraphicsExpose = '13' # undocumented |
| 425 |
NoExpose = '14' # undocumented |
| 426 |
Visibility = '15' |
| 427 |
Create = '16' |
| 428 |
Destroy = '17' |
| 429 |
Unmap = '18' |
| 430 |
Map = '19' |
| 431 |
MapRequest = '20' |
| 432 |
Reparent = '21' |
| 433 |
Configure = '22' |
| 434 |
ConfigureRequest = '23' |
| 435 |
Gravity = '24' |
| 436 |
ResizeRequest = '25' |
| 437 |
Circulate = '26' |
| 438 |
CirculateRequest = '27' |
| 439 |
Property = '28' |
| 440 |
SelectionClear = '29' # undocumented |
| 441 |
SelectionRequest = '30' # undocumented |
| 442 |
Selection = '31' # undocumented |
| 443 |
Colormap = '32' |
| 444 |
ClientMessage = '33' # undocumented |
| 445 |
Mapping = '34' # undocumented |
| 446 |
VirtualEvent = '35' # undocumented |
| 447 |
Activate = '36' |
| 448 |
Deactivate = '37' |
| 449 |
MouseWheel = '38' |
| 450 |
enum._test_simple_enum(CheckedEventType, tkinter.EventType) |
| 451 |
|
| 452 |
def test_getboolean(self): |
| 453 |
for v in 'true', 'yes', 'on', '1', 't', 'y', 1, True: |
| 454 |
self.assertIs(self.root.getboolean(v), True) |
| 455 |
for v in 'false', 'no', 'off', '0', 'f', 'n', 0, False: |
| 456 |
self.assertIs(self.root.getboolean(v), False) |
| 457 |
self.assertRaises(ValueError, self.root.getboolean, 'yea') |
| 458 |
self.assertRaises(ValueError, self.root.getboolean, '') |
| 459 |
self.assertRaises(TypeError, self.root.getboolean, None) |
| 460 |
self.assertRaises(TypeError, self.root.getboolean, ()) |
| 461 |
|
| 462 |
def test_mainloop(self): |
| 463 |
log = [] |
| 464 |
def callback(): |
| 465 |
log.append(1) |
| 466 |
self.root.after(100, self.root.quit) |
| 467 |
self.root.after(100, callback) |
| 468 |
self.root.mainloop(1) |
| 469 |
self.assertEqual(log, []) |
| 470 |
self.root.mainloop(0) |
| 471 |
self.assertEqual(log, [1]) |
| 472 |
self.assertTrue(self.root.winfo_exists()) |
| 473 |
|
| 474 |
def test_info_patchlevel(self): |
| 475 |
vi = self.root.info_patchlevel() |
| 476 |
f = tkinter.Frame(self.root) |
| 477 |
self.assertEqual(f.info_patchlevel(), vi) |
| 478 |
# The following is almost a copy of tests for sys.version_info. |
| 479 |
self.assertIsInstance(vi[:], tuple) |
| 480 |
self.assertEqual(len(vi), 5) |
| 481 |
self.assertIsInstance(vi[0], int) |
| 482 |
self.assertIsInstance(vi[1], int) |
| 483 |
self.assertIsInstance(vi[2], int) |
| 484 |
self.assertIn(vi[3], ("alpha", "beta", "candidate", "final")) |
| 485 |
self.assertIsInstance(vi[4], int) |
| 486 |
self.assertIsInstance(vi.major, int) |
| 487 |
self.assertIsInstance(vi.minor, int) |
| 488 |
self.assertIsInstance(vi.micro, int) |
| 489 |
self.assertIn(vi.releaselevel, ("alpha", "beta", "final")) |
| 490 |
self.assertIsInstance(vi.serial, int) |
| 491 |
self.assertEqual(vi[0], vi.major) |
| 492 |
self.assertEqual(vi[1], vi.minor) |
| 493 |
self.assertEqual(vi[2], vi.micro) |
| 494 |
self.assertEqual(vi[3], vi.releaselevel) |
| 495 |
self.assertEqual(vi[4], vi.serial) |
| 496 |
self.assertTrue(vi > (1,0,0)) |
| 497 |
if vi.releaselevel == 'final': |
| 498 |
self.assertEqual(vi.serial, 0) |
| 499 |
else: |
| 500 |
self.assertEqual(vi.micro, 0) |
| 501 |
self.assertStartsWith(str(vi), f'{vi.major}.{vi.minor}') |
| 502 |
|
| 503 |
def test_embedded_null(self): |
| 504 |
widget = tkinter.Entry(self.root) |
| 505 |
widget.insert(0, 'abc\0def') # ASCII-only |
| 506 |
widget.selection_range(0, 'end') |
| 507 |
self.assertEqual(widget.selection_get(), 'abc\x00def') |
| 508 |
widget.insert(0, '\u20ac\0') # non-ASCII |
| 509 |
widget.selection_range(0, 'end') |
| 510 |
self.assertEqual(widget.selection_get(), '\u20ac\0abc\x00def') |
| 511 |
|
| 512 |
|
| 513 |
class WmTest(AbstractTkTest, unittest.TestCase): |
| 514 |
|
| 515 |
def test_wm_attribute(self): |
| 516 |
w = self.root |
| 517 |
attributes = w.wm_attributes(return_python_dict=True) |
| 518 |
self.assertIsInstance(attributes, dict) |
| 519 |
attributes2 = w.wm_attributes() |
| 520 |
self.assertIsInstance(attributes2, tuple) |
| 521 |
self.assertEqual(attributes2[::2], |
| 522 |
tuple('-' + k for k in attributes)) |
| 523 |
self.assertEqual(attributes2[1::2], tuple(attributes.values())) |
| 524 |
# silently deprecated |
| 525 |
attributes3 = w.wm_attributes(None) |
| 526 |
if self.wantobjects: |
| 527 |
self.assertEqual(attributes3, attributes2) |
| 528 |
else: |
| 529 |
self.assertIsInstance(attributes3, str) |
| 530 |
|
| 531 |
for name in attributes: |
| 532 |
self.assertEqual(w.wm_attributes(name), attributes[name]) |
| 533 |
# silently deprecated |
| 534 |
for name in attributes: |
| 535 |
self.assertEqual(w.wm_attributes('-' + name), attributes[name]) |
| 536 |
|
| 537 |
self.assertIn('alpha', attributes) |
| 538 |
self.assertIn('fullscreen', attributes) |
| 539 |
self.assertIn('topmost', attributes) |
| 540 |
if w._windowingsystem == "win32": |
| 541 |
self.assertIn('disabled', attributes) |
| 542 |
self.assertIn('toolwindow', attributes) |
| 543 |
self.assertIn('transparentcolor', attributes) |
| 544 |
if w._windowingsystem == "aqua": |
| 545 |
self.assertIn('modified', attributes) |
| 546 |
self.assertIn('notify', attributes) |
| 547 |
self.assertIn('titlepath', attributes) |
| 548 |
self.assertIn('transparent', attributes) |
| 549 |
if w._windowingsystem == "x11": |
| 550 |
self.assertIn('type', attributes) |
| 551 |
self.assertIn('zoomed', attributes) |
| 552 |
|
| 553 |
w.wm_attributes(alpha=0.5) |
| 554 |
self.assertEqual(w.wm_attributes('alpha'), |
| 555 |
0.5 if self.wantobjects else '0.5') |
| 556 |
w.wm_attributes(alpha=1.0) |
| 557 |
self.assertEqual(w.wm_attributes('alpha'), |
| 558 |
1.0 if self.wantobjects else '1.0') |
| 559 |
# silently deprecated |
| 560 |
w.wm_attributes('-alpha', 0.5) |
| 561 |
self.assertEqual(w.wm_attributes('alpha'), |
| 562 |
0.5 if self.wantobjects else '0.5') |
| 563 |
w.wm_attributes(alpha=1.0) |
| 564 |
self.assertEqual(w.wm_attributes('alpha'), |
| 565 |
1.0 if self.wantobjects else '1.0') |
| 566 |
|
| 567 |
def test_wm_iconbitmap(self): |
| 568 |
t = tkinter.Toplevel(self.root) |
| 569 |
self.assertEqual(t.wm_iconbitmap(), '') |
| 570 |
t.wm_iconbitmap('hourglass') |
| 571 |
bug = False |
| 572 |
if t._windowingsystem == 'aqua': |
| 573 |
# Tk bug 13ac26b35dc55f7c37f70b39d59d7ef3e63017c8. |
| 574 |
patchlevel = get_tk_patchlevel(t) |
| 575 |
if patchlevel < (8, 6, 17) or (9, 0) <= patchlevel < (9, 0, 2): |
| 576 |
bug = True |
| 577 |
if not bug: |
| 578 |
self.assertEqual(t.wm_iconbitmap(), 'hourglass') |
| 579 |
self.assertEqual(self.root.wm_iconbitmap(), '') |
| 580 |
t.wm_iconbitmap('') |
| 581 |
self.assertEqual(t.wm_iconbitmap(), '') |
| 582 |
|
| 583 |
if t._windowingsystem == 'win32': |
| 584 |
t.wm_iconbitmap(default='hourglass') |
| 585 |
self.assertEqual(t.wm_iconbitmap(), 'hourglass') |
| 586 |
self.assertEqual(self.root.wm_iconbitmap(), '') |
| 587 |
t.wm_iconbitmap(default='') |
| 588 |
self.assertEqual(t.wm_iconbitmap(), '') |
| 589 |
|
| 590 |
t.destroy() |
| 591 |
|
| 592 |
|
| 593 |
class EventTest(AbstractTkTest, unittest.TestCase): |
| 594 |
|
| 595 |
def test_focus(self): |
| 596 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 597 |
f.pack() |
| 598 |
self.root.wait_visibility() # needed on Windows |
| 599 |
self.root.update_idletasks() |
| 600 |
|
| 601 |
events = [] |
| 602 |
f.bind('<FocusIn>', events.append) |
| 603 |
|
| 604 |
f.focus_force() |
| 605 |
self.root.update() |
| 606 |
self.assertEqual(len(events), 1, events) |
| 607 |
e = events[0] |
| 608 |
self.assertIs(e.type, tkinter.EventType.FocusIn) |
| 609 |
self.assertIs(e.widget, f) |
| 610 |
self.assertIsInstance(e.serial, int) |
| 611 |
self.assertEqual(e.time, '??') |
| 612 |
self.assertIs(e.send_event, False) |
| 613 |
self.assertNotHasAttr(e, 'focus') |
| 614 |
self.assertEqual(e.num, '??') |
| 615 |
self.assertEqual(e.state, '??') |
| 616 |
self.assertEqual(e.char, '??') |
| 617 |
self.assertEqual(e.keycode, '??') |
| 618 |
self.assertEqual(e.keysym, '??') |
| 619 |
self.assertEqual(e.keysym_num, '??') |
| 620 |
self.assertEqual(e.width, '??') |
| 621 |
self.assertEqual(e.height, '??') |
| 622 |
self.assertEqual(e.x, '??') |
| 623 |
self.assertEqual(e.y, '??') |
| 624 |
self.assertEqual(e.x_root, '??') |
| 625 |
self.assertEqual(e.y_root, '??') |
| 626 |
self.assertEqual(e.delta, 0) |
| 627 |
self.assertEqual(repr(e), '<FocusIn event>') |
| 628 |
|
| 629 |
def test_configure(self): |
| 630 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 631 |
f.pack() |
| 632 |
self.root.wait_visibility() # needed on Windows |
| 633 |
self.root.update_idletasks() |
| 634 |
|
| 635 |
events = [] |
| 636 |
f.bind('<Configure>', events.append) |
| 637 |
|
| 638 |
f.configure(height=120, borderwidth=10) |
| 639 |
self.assertEqual(len(events), 1, events) |
| 640 |
e = events[0] |
| 641 |
self.assertIs(e.type, tkinter.EventType.Configure) |
| 642 |
self.assertIs(e.widget, f) |
| 643 |
self.assertIsInstance(e.serial, int) |
| 644 |
self.assertEqual(e.time, '??') |
| 645 |
self.assertIs(e.send_event, False) |
| 646 |
self.assertNotHasAttr(e, 'focus') |
| 647 |
self.assertEqual(e.num, '??') |
| 648 |
self.assertEqual(e.state, '??') |
| 649 |
self.assertEqual(e.char, '??') |
| 650 |
self.assertEqual(e.keycode, '??') |
| 651 |
self.assertEqual(e.keysym, '??') |
| 652 |
self.assertEqual(e.keysym_num, '??') |
| 653 |
self.assertEqual(e.width, 150) |
| 654 |
self.assertEqual(e.height, 100) |
| 655 |
self.assertEqual(e.x, 0) |
| 656 |
self.assertEqual(e.y, 0) |
| 657 |
self.assertEqual(e.x_root, '??') |
| 658 |
self.assertEqual(e.y_root, '??') |
| 659 |
self.assertEqual(e.delta, 0) |
| 660 |
self.assertEqual(repr(e), '<Configure event x=0 y=0 width=150 height=100>') |
| 661 |
|
| 662 |
def test_event_generate_key_press(self): |
| 663 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 664 |
f.pack() |
| 665 |
self.root.wait_visibility() # needed on Windows |
| 666 |
self.root.update_idletasks() |
| 667 |
|
| 668 |
events = [] |
| 669 |
f.bind('<KeyPress>', events.append) |
| 670 |
f.focus_force() |
| 671 |
|
| 672 |
f.event_generate('<Alt-z>') |
| 673 |
self.assertEqual(len(events), 1, events) |
| 674 |
e = events[0] |
| 675 |
self.assertIs(e.type, tkinter.EventType.KeyPress) |
| 676 |
self.assertIs(e.widget, f) |
| 677 |
self.assertIsInstance(e.serial, int) |
| 678 |
self.assertEqual(e.time, 0) |
| 679 |
self.assertIs(e.send_event, False) |
| 680 |
self.assertNotHasAttr(e, 'focus') |
| 681 |
self.assertEqual(e.num, '??') |
| 682 |
self.assertIsInstance(e.state, int) |
| 683 |
self.assertNotEqual(e.state, 0) |
| 684 |
self.assertEqual(e.char, 'z') |
| 685 |
self.assertIsInstance(e.keycode, int) |
| 686 |
self.assertNotEqual(e.keycode, 0) |
| 687 |
self.assertEqual(e.keysym, 'z') |
| 688 |
self.assertEqual(e.keysym_num, ord('z')) |
| 689 |
self.assertEqual(e.width, '??') |
| 690 |
self.assertEqual(e.height, '??') |
| 691 |
self.assertEqual(e.x, -1 - f.winfo_rootx()) |
| 692 |
self.assertEqual(e.y, -1 - f.winfo_rooty()) |
| 693 |
self.assertEqual(e.x_root, -1) |
| 694 |
self.assertEqual(e.y_root, -1) |
| 695 |
self.assertEqual(e.delta, 0) |
| 696 |
self.assertEqual(repr(e), |
| 697 |
f"<KeyPress event state={e.state:#x} " |
| 698 |
f"keysym=z keycode={e.keycode} char='z' x={e.x} y={e.y}>") |
| 699 |
|
| 700 |
def test_event_generate_enter(self): |
| 701 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 702 |
f.pack() |
| 703 |
self.root.wait_visibility() # needed on Windows |
| 704 |
self.root.update_idletasks() |
| 705 |
|
| 706 |
events = [] |
| 707 |
f.bind('<Enter>', events.append) |
| 708 |
|
| 709 |
f.event_generate('<Enter>', x=100, y=50) |
| 710 |
self.assertEqual(len(events), 1, events) |
| 711 |
e = events[0] |
| 712 |
self.assertIs(e.type, tkinter.EventType.Enter) |
| 713 |
self.assertIs(e.widget, f) |
| 714 |
self.assertIsInstance(e.serial, int) |
| 715 |
self.assertEqual(e.time, 0) |
| 716 |
self.assertIs(e.send_event, False) |
| 717 |
self.assertIs(e.focus, False) |
| 718 |
self.assertEqual(e.num, '??') |
| 719 |
self.assertEqual(e.state, 0) |
| 720 |
self.assertEqual(e.char, '??') |
| 721 |
self.assertEqual(e.keycode, '??') |
| 722 |
self.assertEqual(e.keysym, '??') |
| 723 |
self.assertEqual(e.keysym_num, '??') |
| 724 |
self.assertEqual(e.width, '??') |
| 725 |
self.assertEqual(e.height, '??') |
| 726 |
self.assertEqual(e.x, 100) |
| 727 |
self.assertEqual(e.y, 50) |
| 728 |
self.assertEqual(e.x_root, 100 + f.winfo_rootx()) |
| 729 |
self.assertEqual(e.y_root, 50 + f.winfo_rooty()) |
| 730 |
self.assertEqual(e.delta, 0) |
| 731 |
self.assertEqual(repr(e), '<Enter event focus=False x=100 y=50>') |
| 732 |
|
| 733 |
def test_event_generate_button_press(self): |
| 734 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 735 |
f.pack() |
| 736 |
self.root.wait_visibility() # needed on Windows |
| 737 |
self.root.update_idletasks() |
| 738 |
|
| 739 |
events = [] |
| 740 |
f.bind('<ButtonPress>', events.append) |
| 741 |
f.focus_force() |
| 742 |
|
| 743 |
f.event_generate('<Button-1>', x=100, y=50) |
| 744 |
self.assertEqual(len(events), 1, events) |
| 745 |
e = events[0] |
| 746 |
self.assertIs(e.type, tkinter.EventType.ButtonPress) |
| 747 |
self.assertIs(e.widget, f) |
| 748 |
self.assertIsInstance(e.serial, int) |
| 749 |
self.assertEqual(e.time, 0) |
| 750 |
self.assertIs(e.send_event, False) |
| 751 |
self.assertNotHasAttr(e, 'focus') |
| 752 |
self.assertEqual(e.num, 1) |
| 753 |
self.assertEqual(e.state, 0) |
| 754 |
self.assertEqual(e.char, '??') |
| 755 |
self.assertEqual(e.keycode, '??') |
| 756 |
self.assertEqual(e.keysym, '??') |
| 757 |
self.assertEqual(e.keysym_num, '??') |
| 758 |
self.assertEqual(e.width, '??') |
| 759 |
self.assertEqual(e.height, '??') |
| 760 |
self.assertEqual(e.x, 100) |
| 761 |
self.assertEqual(e.y, 50) |
| 762 |
self.assertEqual(e.x_root, f.winfo_rootx() + 100) |
| 763 |
self.assertEqual(e.y_root, f.winfo_rooty() + 50) |
| 764 |
self.assertEqual(e.delta, 0) |
| 765 |
self.assertEqual(repr(e), '<ButtonPress event num=1 x=100 y=50>') |
| 766 |
|
| 767 |
def test_event_generate_motion(self): |
| 768 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 769 |
f.pack() |
| 770 |
self.root.wait_visibility() # needed on Windows |
| 771 |
self.root.update_idletasks() |
| 772 |
|
| 773 |
events = [] |
| 774 |
f.bind('<Motion>', events.append) |
| 775 |
f.focus_force() |
| 776 |
|
| 777 |
f.event_generate('<B1-Motion>', x=100, y=50) |
| 778 |
self.assertEqual(len(events), 1, events) |
| 779 |
e = events[0] |
| 780 |
self.assertIs(e.type, tkinter.EventType.Motion) |
| 781 |
self.assertIs(e.widget, f) |
| 782 |
self.assertIsInstance(e.serial, int) |
| 783 |
self.assertEqual(e.time, 0) |
| 784 |
self.assertIs(e.send_event, False) |
| 785 |
self.assertNotHasAttr(e, 'focus') |
| 786 |
self.assertEqual(e.num, '??') |
| 787 |
self.assertEqual(e.state, 0x100) |
| 788 |
self.assertEqual(e.char, '??') |
| 789 |
self.assertEqual(e.keycode, '??') |
| 790 |
self.assertEqual(e.keysym, '??') |
| 791 |
self.assertEqual(e.keysym_num, '??') |
| 792 |
self.assertEqual(e.width, '??') |
| 793 |
self.assertEqual(e.height, '??') |
| 794 |
self.assertEqual(e.x, 100) |
| 795 |
self.assertEqual(e.y, 50) |
| 796 |
self.assertEqual(e.x_root, f.winfo_rootx() + 100) |
| 797 |
self.assertEqual(e.y_root, f.winfo_rooty() + 50) |
| 798 |
self.assertEqual(e.delta, 0) |
| 799 |
self.assertEqual(repr(e), '<Motion event state=Button1 x=100 y=50>') |
| 800 |
|
| 801 |
def test_event_generate_mouse_wheel(self): |
| 802 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 803 |
f.pack() |
| 804 |
self.root.wait_visibility() # needed on Windows |
| 805 |
self.root.update_idletasks() |
| 806 |
|
| 807 |
events = [] |
| 808 |
f.bind('<MouseWheel>', events.append) |
| 809 |
f.focus_force() |
| 810 |
|
| 811 |
f.event_generate('<MouseWheel>', x=100, y=50, delta=-5) |
| 812 |
self.assertEqual(len(events), 1, events) |
| 813 |
e = events[0] |
| 814 |
self.assertIs(e.type, tkinter.EventType.MouseWheel) |
| 815 |
self.assertIs(e.widget, f) |
| 816 |
self.assertIsInstance(e.serial, int) |
| 817 |
self.assertIs(e.send_event, False) |
| 818 |
self.assertNotHasAttr(e, 'focus') |
| 819 |
self.assertEqual(e.time, 0) |
| 820 |
self.assertEqual(e.num, '??') |
| 821 |
self.assertEqual(e.state, 0) |
| 822 |
self.assertEqual(e.char, '??') |
| 823 |
self.assertEqual(e.keycode, '??') |
| 824 |
self.assertEqual(e.keysym, '??') |
| 825 |
self.assertEqual(e.keysym_num, '??') |
| 826 |
self.assertEqual(e.width, '??') |
| 827 |
self.assertEqual(e.height, '??') |
| 828 |
self.assertEqual(e.x, 100) |
| 829 |
self.assertEqual(e.y, 50) |
| 830 |
self.assertEqual(e.x_root, f.winfo_rootx() + 100) |
| 831 |
self.assertEqual(e.y_root, f.winfo_rooty() + 50) |
| 832 |
self.assertEqual(e.delta, -5) |
| 833 |
self.assertEqual(repr(e), '<MouseWheel event delta=-5 x=100 y=50>') |
| 834 |
|
| 835 |
def test_generate_event_virtual_event(self): |
| 836 |
f = tkinter.Frame(self.root, width=150, height=100) |
| 837 |
f.pack() |
| 838 |
self.root.wait_visibility() # needed on Windows |
| 839 |
self.root.update_idletasks() |
| 840 |
|
| 841 |
events = [] |
| 842 |
f.bind('<<Spam>>', events.append) |
| 843 |
f.focus_force() |
| 844 |
|
| 845 |
f.event_generate('<<Spam>>', x=50) |
| 846 |
self.assertEqual(len(events), 1, events) |
| 847 |
e = events[0] |
| 848 |
self.assertIs(e.type, tkinter.EventType.VirtualEvent) |
| 849 |
self.assertIs(e.widget, f) |
| 850 |
self.assertIsInstance(e.serial, int) |
| 851 |
self.assertEqual(e.time, 0) |
| 852 |
self.assertIs(e.send_event, False) |
| 853 |
self.assertNotHasAttr(e, 'focus') |
| 854 |
self.assertEqual(e.num, '??') |
| 855 |
self.assertEqual(e.state, 0) |
| 856 |
self.assertEqual(e.char, '??') |
| 857 |
self.assertEqual(e.keycode, '??') |
| 858 |
self.assertEqual(e.keysym, '??') |
| 859 |
self.assertEqual(e.keysym_num, '??') |
| 860 |
self.assertEqual(e.width, '??') |
| 861 |
self.assertEqual(e.height, '??') |
| 862 |
self.assertEqual(e.x, 50) |
| 863 |
self.assertEqual(e.y, 0) |
| 864 |
self.assertEqual(e.x_root, f.winfo_rootx() + 50) |
| 865 |
self.assertEqual(e.y_root, -1) |
| 866 |
self.assertEqual(e.delta, 0) |
| 867 |
self.assertEqual(repr(e), |
| 868 |
f"<VirtualEvent event x=50 y=0>") |
| 869 |
|
| 870 |
|
| 871 |
class BindTest(AbstractTkTest, unittest.TestCase): |
| 872 |
|
| 873 |
def setUp(self): |
| 874 |
super().setUp() |
| 875 |
root = self.root |
| 876 |
self.frame = tkinter.Frame(self.root, class_='Test', |
| 877 |
width=150, height=100) |
| 878 |
self.frame.pack() |
| 879 |
|
| 880 |
def assertCommandExist(self, funcid): |
| 881 |
self.assertEqual(_info_commands(self.root, funcid), (funcid,)) |
| 882 |
|
| 883 |
def assertCommandNotExist(self, funcid): |
| 884 |
self.assertEqual(_info_commands(self.root, funcid), ()) |
| 885 |
|
| 886 |
def test_bind(self): |
| 887 |
event = '<Control-Alt-Key-a>' |
| 888 |
f = self.frame |
| 889 |
self.assertEqual(f.bind(), ()) |
| 890 |
self.assertEqual(f.bind(event), '') |
| 891 |
def test1(e): pass |
| 892 |
def test2(e): pass |
| 893 |
|
| 894 |
funcid = f.bind(event, test1) |
| 895 |
self.assertEqual(f.bind(), (event,)) |
| 896 |
script = f.bind(event) |
| 897 |
self.assertIn(funcid, script) |
| 898 |
self.assertCommandExist(funcid) |
| 899 |
|
| 900 |
funcid2 = f.bind(event, test2, add=True) |
| 901 |
script = f.bind(event) |
| 902 |
self.assertIn(funcid, script) |
| 903 |
self.assertIn(funcid2, script) |
| 904 |
self.assertCommandExist(funcid) |
| 905 |
self.assertCommandExist(funcid2) |
| 906 |
|
| 907 |
def test_unbind(self): |
| 908 |
event = '<Control-Alt-Key-b>' |
| 909 |
f = self.frame |
| 910 |
self.assertEqual(f.bind(), ()) |
| 911 |
self.assertEqual(f.bind(event), '') |
| 912 |
def test1(e): pass |
| 913 |
def test2(e): pass |
| 914 |
|
| 915 |
funcid = f.bind(event, test1) |
| 916 |
funcid2 = f.bind(event, test2, add=True) |
| 917 |
|
| 918 |
self.assertRaises(TypeError, f.unbind) |
| 919 |
f.unbind(event) |
| 920 |
self.assertEqual(f.bind(event), '') |
| 921 |
self.assertEqual(f.bind(), ()) |
| 922 |
|
| 923 |
def test_unbind2(self): |
| 924 |
f = self.frame |
| 925 |
f.wait_visibility() |
| 926 |
f.focus_force() |
| 927 |
f.update_idletasks() |
| 928 |
event = '<Control-Alt-Key-c>' |
| 929 |
self.assertEqual(f.bind(), ()) |
| 930 |
self.assertEqual(f.bind(event), '') |
| 931 |
def test1(e): events.append('a') |
| 932 |
def test2(e): events.append('b') |
| 933 |
def test3(e): events.append('c') |
| 934 |
|
| 935 |
funcid = f.bind(event, test1) |
| 936 |
funcid2 = f.bind(event, test2, add=True) |
| 937 |
funcid3 = f.bind(event, test3, add=True) |
| 938 |
events = [] |
| 939 |
f.event_generate(event) |
| 940 |
self.assertEqual(events, ['a', 'b', 'c']) |
| 941 |
|
| 942 |
f.unbind(event, funcid2) |
| 943 |
script = f.bind(event) |
| 944 |
self.assertNotIn(funcid2, script) |
| 945 |
self.assertIn(funcid, script) |
| 946 |
self.assertIn(funcid3, script) |
| 947 |
self.assertEqual(f.bind(), (event,)) |
| 948 |
self.assertCommandNotExist(funcid2) |
| 949 |
self.assertCommandExist(funcid) |
| 950 |
self.assertCommandExist(funcid3) |
| 951 |
events = [] |
| 952 |
f.event_generate(event) |
| 953 |
self.assertEqual(events, ['a', 'c']) |
| 954 |
|
| 955 |
f.unbind(event, funcid) |
| 956 |
f.unbind(event, funcid3) |
| 957 |
self.assertEqual(f.bind(event), '') |
| 958 |
self.assertEqual(f.bind(), ()) |
| 959 |
self.assertCommandNotExist(funcid) |
| 960 |
self.assertCommandNotExist(funcid2) |
| 961 |
self.assertCommandNotExist(funcid3) |
| 962 |
events = [] |
| 963 |
f.event_generate(event) |
| 964 |
self.assertEqual(events, []) |
| 965 |
|
| 966 |
# non-idempotent |
| 967 |
self.assertRaises(tkinter.TclError, f.unbind, event, funcid2) |
| 968 |
|
| 969 |
def test_bind_rebind(self): |
| 970 |
event = '<Control-Alt-Key-d>' |
| 971 |
f = self.frame |
| 972 |
self.assertEqual(f.bind(), ()) |
| 973 |
self.assertEqual(f.bind(event), '') |
| 974 |
def test1(e): pass |
| 975 |
def test2(e): pass |
| 976 |
def test3(e): pass |
| 977 |
|
| 978 |
funcid = f.bind(event, test1) |
| 979 |
funcid2 = f.bind(event, test2, add=True) |
| 980 |
script = f.bind(event) |
| 981 |
self.assertIn(funcid2, script) |
| 982 |
self.assertIn(funcid, script) |
| 983 |
self.assertCommandExist(funcid) |
| 984 |
self.assertCommandExist(funcid2) |
| 985 |
|
| 986 |
funcid3 = f.bind(event, test3) |
| 987 |
script = f.bind(event) |
| 988 |
self.assertNotIn(funcid, script) |
| 989 |
self.assertNotIn(funcid2, script) |
| 990 |
self.assertIn(funcid3, script) |
| 991 |
self.assertCommandExist(funcid3) |
| 992 |
|
| 993 |
def test_bind_class(self): |
| 994 |
event = '<Control-Alt-Key-e>' |
| 995 |
bind_class = self.root.bind_class |
| 996 |
unbind_class = self.root.unbind_class |
| 997 |
self.assertRaises(TypeError, bind_class) |
| 998 |
self.assertEqual(bind_class('Test'), ()) |
| 999 |
self.assertEqual(bind_class('Test', event), '') |
| 1000 |
self.addCleanup(unbind_class, 'Test', event) |
| 1001 |
def test1(e): pass |
| 1002 |
def test2(e): pass |
| 1003 |
|
| 1004 |
funcid = bind_class('Test', event, test1) |
| 1005 |
self.assertEqual(bind_class('Test'), (event,)) |
| 1006 |
script = bind_class('Test', event) |
| 1007 |
self.assertIn(funcid, script) |
| 1008 |
self.assertCommandExist(funcid) |
| 1009 |
|
| 1010 |
funcid2 = bind_class('Test', event, test2, add=True) |
| 1011 |
script = bind_class('Test', event) |
| 1012 |
self.assertIn(funcid, script) |
| 1013 |
self.assertIn(funcid2, script) |
| 1014 |
self.assertCommandExist(funcid) |
| 1015 |
self.assertCommandExist(funcid2) |
| 1016 |
|
| 1017 |
def test_unbind_class(self): |
| 1018 |
event = '<Control-Alt-Key-f>' |
| 1019 |
bind_class = self.root.bind_class |
| 1020 |
unbind_class = self.root.unbind_class |
| 1021 |
self.assertEqual(bind_class('Test'), ()) |
| 1022 |
self.assertEqual(bind_class('Test', event), '') |
| 1023 |
self.addCleanup(unbind_class, 'Test', event) |
| 1024 |
def test1(e): pass |
| 1025 |
def test2(e): pass |
| 1026 |
|
| 1027 |
funcid = bind_class('Test', event, test1) |
| 1028 |
funcid2 = bind_class('Test', event, test2, add=True) |
| 1029 |
|
| 1030 |
self.assertRaises(TypeError, unbind_class) |
| 1031 |
self.assertRaises(TypeError, unbind_class, 'Test') |
| 1032 |
unbind_class('Test', event) |
| 1033 |
self.assertEqual(bind_class('Test', event), '') |
| 1034 |
self.assertEqual(bind_class('Test'), ()) |
| 1035 |
self.assertCommandExist(funcid) |
| 1036 |
self.assertCommandExist(funcid2) |
| 1037 |
|
| 1038 |
unbind_class('Test', event) # idempotent |
| 1039 |
|
| 1040 |
def test_bind_class_rebind(self): |
| 1041 |
event = '<Control-Alt-Key-g>' |
| 1042 |
bind_class = self.root.bind_class |
| 1043 |
unbind_class = self.root.unbind_class |
| 1044 |
self.assertEqual(bind_class('Test'), ()) |
| 1045 |
self.assertEqual(bind_class('Test', event), '') |
| 1046 |
self.addCleanup(unbind_class, 'Test', event) |
| 1047 |
def test1(e): pass |
| 1048 |
def test2(e): pass |
| 1049 |
def test3(e): pass |
| 1050 |
|
| 1051 |
funcid = bind_class('Test', event, test1) |
| 1052 |
funcid2 = bind_class('Test', event, test2, add=True) |
| 1053 |
script = bind_class('Test', event) |
| 1054 |
self.assertIn(funcid2, script) |
| 1055 |
self.assertIn(funcid, script) |
| 1056 |
self.assertCommandExist(funcid) |
| 1057 |
self.assertCommandExist(funcid2) |
| 1058 |
|
| 1059 |
funcid3 = bind_class('Test', event, test3) |
| 1060 |
script = bind_class('Test', event) |
| 1061 |
self.assertNotIn(funcid, script) |
| 1062 |
self.assertNotIn(funcid2, script) |
| 1063 |
self.assertIn(funcid3, script) |
| 1064 |
self.assertCommandExist(funcid) |
| 1065 |
self.assertCommandExist(funcid2) |
| 1066 |
self.assertCommandExist(funcid3) |
| 1067 |
|
| 1068 |
def test_bind_all(self): |
| 1069 |
event = '<Control-Alt-Key-h>' |
| 1070 |
bind_all = self.root.bind_all |
| 1071 |
unbind_all = self.root.unbind_all |
| 1072 |
self.assertNotIn(event, bind_all()) |
| 1073 |
self.assertEqual(bind_all(event), '') |
| 1074 |
self.addCleanup(unbind_all, event) |
| 1075 |
def test1(e): pass |
| 1076 |
def test2(e): pass |
| 1077 |
|
| 1078 |
funcid = bind_all(event, test1) |
| 1079 |
self.assertIn(event, bind_all()) |
| 1080 |
script = bind_all(event) |
| 1081 |
self.assertIn(funcid, script) |
| 1082 |
self.assertCommandExist(funcid) |
| 1083 |
|
| 1084 |
funcid2 = bind_all(event, test2, add=True) |
| 1085 |
script = bind_all(event) |
| 1086 |
self.assertIn(funcid, script) |
| 1087 |
self.assertIn(funcid2, script) |
| 1088 |
self.assertCommandExist(funcid) |
| 1089 |
self.assertCommandExist(funcid2) |
| 1090 |
|
| 1091 |
def test_unbind_all(self): |
| 1092 |
event = '<Control-Alt-Key-i>' |
| 1093 |
bind_all = self.root.bind_all |
| 1094 |
unbind_all = self.root.unbind_all |
| 1095 |
self.assertNotIn(event, bind_all()) |
| 1096 |
self.assertEqual(bind_all(event), '') |
| 1097 |
self.addCleanup(unbind_all, event) |
| 1098 |
def test1(e): pass |
| 1099 |
def test2(e): pass |
| 1100 |
|
| 1101 |
funcid = bind_all(event, test1) |
| 1102 |
funcid2 = bind_all(event, test2, add=True) |
| 1103 |
|
| 1104 |
unbind_all(event) |
| 1105 |
self.assertEqual(bind_all(event), '') |
| 1106 |
self.assertNotIn(event, bind_all()) |
| 1107 |
self.assertCommandExist(funcid) |
| 1108 |
self.assertCommandExist(funcid2) |
| 1109 |
|
| 1110 |
unbind_all(event) # idempotent |
| 1111 |
|
| 1112 |
def test_bind_all_rebind(self): |
| 1113 |
event = '<Control-Alt-Key-j>' |
| 1114 |
bind_all = self.root.bind_all |
| 1115 |
unbind_all = self.root.unbind_all |
| 1116 |
self.assertNotIn(event, bind_all()) |
| 1117 |
self.assertEqual(bind_all(event), '') |
| 1118 |
self.addCleanup(unbind_all, event) |
| 1119 |
def test1(e): pass |
| 1120 |
def test2(e): pass |
| 1121 |
def test3(e): pass |
| 1122 |
|
| 1123 |
funcid = bind_all(event, test1) |
| 1124 |
funcid2 = bind_all(event, test2, add=True) |
| 1125 |
script = bind_all(event) |
| 1126 |
self.assertIn(funcid2, script) |
| 1127 |
self.assertIn(funcid, script) |
| 1128 |
self.assertCommandExist(funcid) |
| 1129 |
self.assertCommandExist(funcid2) |
| 1130 |
|
| 1131 |
funcid3 = bind_all(event, test3) |
| 1132 |
script = bind_all(event) |
| 1133 |
self.assertNotIn(funcid, script) |
| 1134 |
self.assertNotIn(funcid2, script) |
| 1135 |
self.assertIn(funcid3, script) |
| 1136 |
self.assertCommandExist(funcid) |
| 1137 |
self.assertCommandExist(funcid2) |
| 1138 |
self.assertCommandExist(funcid3) |
| 1139 |
|
| 1140 |
def _test_tag_bind(self, w): |
| 1141 |
tag = 'sel' |
| 1142 |
event = '<Control-Alt-Key-a>' |
| 1143 |
w.pack() |
| 1144 |
self.assertRaises(TypeError, w.tag_bind) |
| 1145 |
tag_bind = w._tag_bind if isinstance(w, tkinter.Text) else w.tag_bind |
| 1146 |
if isinstance(w, tkinter.Text): |
| 1147 |
self.assertRaises(TypeError, w.tag_bind, tag) |
| 1148 |
self.assertRaises(TypeError, w.tag_bind, tag, event) |
| 1149 |
self.assertEqual(tag_bind(tag), ()) |
| 1150 |
self.assertEqual(tag_bind(tag, event), '') |
| 1151 |
def test1(e): pass |
| 1152 |
def test2(e): pass |
| 1153 |
|
| 1154 |
funcid = w.tag_bind(tag, event, test1) |
| 1155 |
self.assertEqual(tag_bind(tag), (event,)) |
| 1156 |
script = tag_bind(tag, event) |
| 1157 |
self.assertIn(funcid, script) |
| 1158 |
self.assertCommandExist(funcid) |
| 1159 |
|
| 1160 |
funcid2 = w.tag_bind(tag, event, test2, add=True) |
| 1161 |
script = tag_bind(tag, event) |
| 1162 |
self.assertIn(funcid, script) |
| 1163 |
self.assertIn(funcid2, script) |
| 1164 |
self.assertCommandExist(funcid) |
| 1165 |
self.assertCommandExist(funcid2) |
| 1166 |
|
| 1167 |
def _test_tag_unbind(self, w): |
| 1168 |
tag = 'sel' |
| 1169 |
event = '<Control-Alt-Key-b>' |
| 1170 |
w.pack() |
| 1171 |
tag_bind = w._tag_bind if isinstance(w, tkinter.Text) else w.tag_bind |
| 1172 |
self.assertEqual(tag_bind(tag), ()) |
| 1173 |
self.assertEqual(tag_bind(tag, event), '') |
| 1174 |
def test1(e): pass |
| 1175 |
def test2(e): pass |
| 1176 |
|
| 1177 |
funcid = w.tag_bind(tag, event, test1) |
| 1178 |
funcid2 = w.tag_bind(tag, event, test2, add=True) |
| 1179 |
|
| 1180 |
self.assertRaises(TypeError, w.tag_unbind, tag) |
| 1181 |
w.tag_unbind(tag, event) |
| 1182 |
self.assertEqual(tag_bind(tag, event), '') |
| 1183 |
self.assertEqual(tag_bind(tag), ()) |
| 1184 |
|
| 1185 |
def _test_tag_bind_rebind(self, w): |
| 1186 |
tag = 'sel' |
| 1187 |
event = '<Control-Alt-Key-d>' |
| 1188 |
w.pack() |
| 1189 |
tag_bind = w._tag_bind if isinstance(w, tkinter.Text) else w.tag_bind |
| 1190 |
self.assertEqual(tag_bind(tag), ()) |
| 1191 |
self.assertEqual(tag_bind(tag, event), '') |
| 1192 |
def test1(e): pass |
| 1193 |
def test2(e): pass |
| 1194 |
def test3(e): pass |
| 1195 |
|
| 1196 |
funcid = w.tag_bind(tag, event, test1) |
| 1197 |
funcid2 = w.tag_bind(tag, event, test2, add=True) |
| 1198 |
script = tag_bind(tag, event) |
| 1199 |
self.assertIn(funcid2, script) |
| 1200 |
self.assertIn(funcid, script) |
| 1201 |
self.assertCommandExist(funcid) |
| 1202 |
self.assertCommandExist(funcid2) |
| 1203 |
|
| 1204 |
funcid3 = w.tag_bind(tag, event, test3) |
| 1205 |
script = tag_bind(tag, event) |
| 1206 |
self.assertNotIn(funcid, script) |
| 1207 |
self.assertNotIn(funcid2, script) |
| 1208 |
self.assertIn(funcid3, script) |
| 1209 |
self.assertCommandExist(funcid3) |
| 1210 |
|
| 1211 |
def test_canvas_tag_bind(self): |
| 1212 |
c = tkinter.Canvas(self.frame) |
| 1213 |
self._test_tag_bind(c) |
| 1214 |
|
| 1215 |
def test_canvas_tag_unbind(self): |
| 1216 |
c = tkinter.Canvas(self.frame) |
| 1217 |
self._test_tag_unbind(c) |
| 1218 |
|
| 1219 |
def test_canvas_tag_bind_rebind(self): |
| 1220 |
c = tkinter.Canvas(self.frame) |
| 1221 |
self._test_tag_bind_rebind(c) |
| 1222 |
|
| 1223 |
def test_text_tag_bind(self): |
| 1224 |
t = tkinter.Text(self.frame) |
| 1225 |
self._test_tag_bind(t) |
| 1226 |
|
| 1227 |
def test_text_tag_unbind(self): |
| 1228 |
t = tkinter.Text(self.frame) |
| 1229 |
self._test_tag_unbind(t) |
| 1230 |
|
| 1231 |
def test_text_tag_bind_rebind(self): |
| 1232 |
t = tkinter.Text(self.frame) |
| 1233 |
self._test_tag_bind_rebind(t) |
| 1234 |
|
| 1235 |
def test_bindtags(self): |
| 1236 |
f = self.frame |
| 1237 |
self.assertEqual(self.root.bindtags(), ('.', 'Tk', 'all')) |
| 1238 |
self.assertEqual(f.bindtags(), (str(f), 'Test', '.', 'all')) |
| 1239 |
f.bindtags(('a', 'b c')) |
| 1240 |
self.assertEqual(f.bindtags(), ('a', 'b c')) |
| 1241 |
|
| 1242 |
def test_bind_events(self): |
| 1243 |
event = '<Enter>' |
| 1244 |
root = self.root |
| 1245 |
t = tkinter.Toplevel(root) |
| 1246 |
f = tkinter.Frame(t, class_='Test', width=150, height=100) |
| 1247 |
f.pack() |
| 1248 |
root.wait_visibility() # needed on Windows |
| 1249 |
root.update_idletasks() |
| 1250 |
self.addCleanup(root.unbind_class, 'Test', event) |
| 1251 |
self.addCleanup(root.unbind_class, 'Toplevel', event) |
| 1252 |
self.addCleanup(root.unbind_class, 'tag', event) |
| 1253 |
self.addCleanup(root.unbind_class, 'tag2', event) |
| 1254 |
self.addCleanup(root.unbind_all, event) |
| 1255 |
def test(what): |
| 1256 |
return lambda e: events.append((what, e.widget)) |
| 1257 |
|
| 1258 |
root.bind_all(event, test('all')) |
| 1259 |
root.bind_class('Test', event, test('frame class')) |
| 1260 |
root.bind_class('Toplevel', event, test('toplevel class')) |
| 1261 |
root.bind_class('tag', event, test('tag')) |
| 1262 |
root.bind_class('tag2', event, test('tag2')) |
| 1263 |
f.bind(event, test('frame')) |
| 1264 |
t.bind(event, test('toplevel')) |
| 1265 |
|
| 1266 |
events = [] |
| 1267 |
f.event_generate(event) |
| 1268 |
self.assertEqual(events, [ |
| 1269 |
('frame', f), |
| 1270 |
('frame class', f), |
| 1271 |
('toplevel', f), |
| 1272 |
('all', f), |
| 1273 |
]) |
| 1274 |
|
| 1275 |
events = [] |
| 1276 |
t.event_generate(event) |
| 1277 |
self.assertEqual(events, [ |
| 1278 |
('toplevel', t), |
| 1279 |
('toplevel class', t), |
| 1280 |
('all', t), |
| 1281 |
]) |
| 1282 |
|
| 1283 |
f.bindtags(('tag', 'tag3')) |
| 1284 |
events = [] |
| 1285 |
f.event_generate(event) |
| 1286 |
self.assertEqual(events, [('tag', f)]) |
| 1287 |
|
| 1288 |
|
| 1289 |
class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase): |
| 1290 |
|
| 1291 |
def test_default_root(self): |
| 1292 |
self.assertIs(tkinter._support_default_root, True) |
| 1293 |
self.assertIsNone(tkinter._default_root) |
| 1294 |
root = tkinter.Tk() |
| 1295 |
root2 = tkinter.Tk() |
| 1296 |
root3 = tkinter.Tk() |
| 1297 |
self.assertIs(tkinter._default_root, root) |
| 1298 |
root2.destroy() |
| 1299 |
self.assertIs(tkinter._default_root, root) |
| 1300 |
root.destroy() |
| 1301 |
self.assertIsNone(tkinter._default_root) |
| 1302 |
root3.destroy() |
| 1303 |
self.assertIsNone(tkinter._default_root) |
| 1304 |
|
| 1305 |
def test_no_default_root(self): |
| 1306 |
self.assertIs(tkinter._support_default_root, True) |
| 1307 |
self.assertIsNone(tkinter._default_root) |
| 1308 |
root = tkinter.Tk() |
| 1309 |
self.assertIs(tkinter._default_root, root) |
| 1310 |
tkinter.NoDefaultRoot() |
| 1311 |
self.assertIs(tkinter._support_default_root, False) |
| 1312 |
self.assertNotHasAttr(tkinter, '_default_root') |
| 1313 |
# repeated call is no-op |
| 1314 |
tkinter.NoDefaultRoot() |
| 1315 |
self.assertIs(tkinter._support_default_root, False) |
| 1316 |
self.assertNotHasAttr(tkinter, '_default_root') |
| 1317 |
root.destroy() |
| 1318 |
self.assertIs(tkinter._support_default_root, False) |
| 1319 |
self.assertNotHasAttr(tkinter, '_default_root') |
| 1320 |
root = tkinter.Tk() |
| 1321 |
self.assertIs(tkinter._support_default_root, False) |
| 1322 |
self.assertNotHasAttr(tkinter, '_default_root') |
| 1323 |
root.destroy() |
| 1324 |
|
| 1325 |
def test_getboolean(self): |
| 1326 |
self.assertRaises(RuntimeError, tkinter.getboolean, '1') |
| 1327 |
root = tkinter.Tk() |
| 1328 |
self.assertIs(tkinter.getboolean('1'), True) |
| 1329 |
self.assertRaises(ValueError, tkinter.getboolean, 'yea') |
| 1330 |
root.destroy() |
| 1331 |
tkinter.NoDefaultRoot() |
| 1332 |
self.assertRaises(RuntimeError, tkinter.getboolean, '1') |
| 1333 |
|
| 1334 |
def test_mainloop(self): |
| 1335 |
self.assertRaises(RuntimeError, tkinter.mainloop) |
| 1336 |
root = tkinter.Tk() |
| 1337 |
root.after_idle(root.quit) |
| 1338 |
tkinter.mainloop() |
| 1339 |
root.destroy() |
| 1340 |
tkinter.NoDefaultRoot() |
| 1341 |
self.assertRaises(RuntimeError, tkinter.mainloop) |
| 1342 |
|
| 1343 |
|
| 1344 |
def _info_commands(widget, pattern=None): |
| 1345 |
return widget.tk.splitlist(widget.tk.call('info', 'commands', pattern)) |
| 1346 |
|
| 1347 |
|
| 1348 |
if __name__ == "__main__": |
| 1349 |
unittest.main() |