test_widgets.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. import unittest
  2. import tkinter
  3. from tkinter import TclError
  4. import os
  5. import sys
  6. from test.support import requires
  7. from tkinter.test.support import (tcl_version, requires_tcl,
  8. get_tk_patchlevel, widget_eq)
  9. from tkinter.test.widget_tests import (
  10. add_standard_options, noconv, pixels_round,
  11. AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests,
  12. setUpModule)
  13. requires('gui')
  14. def float_round(x):
  15. return float(round(x))
  16. class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests):
  17. _conv_pad_pixels = noconv
  18. def test_class(self):
  19. widget = self.create()
  20. self.assertEqual(widget['class'],
  21. widget.__class__.__name__.title())
  22. self.checkInvalidParam(widget, 'class', 'Foo',
  23. errmsg="can't modify -class option after widget is created")
  24. widget2 = self.create(class_='Foo')
  25. self.assertEqual(widget2['class'], 'Foo')
  26. def test_colormap(self):
  27. widget = self.create()
  28. self.assertEqual(widget['colormap'], '')
  29. self.checkInvalidParam(widget, 'colormap', 'new',
  30. errmsg="can't modify -colormap option after widget is created")
  31. widget2 = self.create(colormap='new')
  32. self.assertEqual(widget2['colormap'], 'new')
  33. def test_container(self):
  34. widget = self.create()
  35. self.assertEqual(widget['container'], 0 if self.wantobjects else '0')
  36. self.checkInvalidParam(widget, 'container', 1,
  37. errmsg="can't modify -container option after widget is created")
  38. widget2 = self.create(container=True)
  39. self.assertEqual(widget2['container'], 1 if self.wantobjects else '1')
  40. def test_visual(self):
  41. widget = self.create()
  42. self.assertEqual(widget['visual'], '')
  43. self.checkInvalidParam(widget, 'visual', 'default',
  44. errmsg="can't modify -visual option after widget is created")
  45. widget2 = self.create(visual='default')
  46. self.assertEqual(widget2['visual'], 'default')
  47. @add_standard_options(StandardOptionsTests)
  48. class ToplevelTest(AbstractToplevelTest, unittest.TestCase):
  49. OPTIONS = (
  50. 'background', 'borderwidth',
  51. 'class', 'colormap', 'container', 'cursor', 'height',
  52. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  53. 'menu', 'padx', 'pady', 'relief', 'screen',
  54. 'takefocus', 'use', 'visual', 'width',
  55. )
  56. def create(self, **kwargs):
  57. return tkinter.Toplevel(self.root, **kwargs)
  58. def test_menu(self):
  59. widget = self.create()
  60. menu = tkinter.Menu(self.root)
  61. self.checkParam(widget, 'menu', menu, eq=widget_eq)
  62. self.checkParam(widget, 'menu', '')
  63. def test_screen(self):
  64. widget = self.create()
  65. self.assertEqual(widget['screen'], '')
  66. try:
  67. display = os.environ['DISPLAY']
  68. except KeyError:
  69. self.skipTest('No $DISPLAY set.')
  70. self.checkInvalidParam(widget, 'screen', display,
  71. errmsg="can't modify -screen option after widget is created")
  72. widget2 = self.create(screen=display)
  73. self.assertEqual(widget2['screen'], display)
  74. def test_use(self):
  75. widget = self.create()
  76. self.assertEqual(widget['use'], '')
  77. parent = self.create(container=True)
  78. wid = hex(parent.winfo_id())
  79. with self.subTest(wid=wid):
  80. widget2 = self.create(use=wid)
  81. self.assertEqual(widget2['use'], wid)
  82. @add_standard_options(StandardOptionsTests)
  83. class FrameTest(AbstractToplevelTest, unittest.TestCase):
  84. OPTIONS = (
  85. 'background', 'borderwidth',
  86. 'class', 'colormap', 'container', 'cursor', 'height',
  87. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  88. 'padx', 'pady', 'relief', 'takefocus', 'visual', 'width',
  89. )
  90. def create(self, **kwargs):
  91. return tkinter.Frame(self.root, **kwargs)
  92. @add_standard_options(StandardOptionsTests)
  93. class LabelFrameTest(AbstractToplevelTest, unittest.TestCase):
  94. OPTIONS = (
  95. 'background', 'borderwidth',
  96. 'class', 'colormap', 'container', 'cursor',
  97. 'font', 'foreground', 'height',
  98. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  99. 'labelanchor', 'labelwidget', 'padx', 'pady', 'relief',
  100. 'takefocus', 'text', 'visual', 'width',
  101. )
  102. def create(self, **kwargs):
  103. return tkinter.LabelFrame(self.root, **kwargs)
  104. def test_labelanchor(self):
  105. widget = self.create()
  106. self.checkEnumParam(widget, 'labelanchor',
  107. 'e', 'en', 'es', 'n', 'ne', 'nw',
  108. 's', 'se', 'sw', 'w', 'wn', 'ws')
  109. self.checkInvalidParam(widget, 'labelanchor', 'center')
  110. def test_labelwidget(self):
  111. widget = self.create()
  112. label = tkinter.Label(self.root, text='Mupp', name='foo')
  113. self.checkParam(widget, 'labelwidget', label, expected='.foo')
  114. label.destroy()
  115. class AbstractLabelTest(AbstractWidgetTest, IntegerSizeTests):
  116. _conv_pixels = noconv
  117. def test_highlightthickness(self):
  118. widget = self.create()
  119. self.checkPixelsParam(widget, 'highlightthickness',
  120. 0, 1.3, 2.6, 6, -2, '10p')
  121. @add_standard_options(StandardOptionsTests)
  122. class LabelTest(AbstractLabelTest, unittest.TestCase):
  123. OPTIONS = (
  124. 'activebackground', 'activeforeground', 'anchor',
  125. 'background', 'bitmap', 'borderwidth', 'compound', 'cursor',
  126. 'disabledforeground', 'font', 'foreground', 'height',
  127. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  128. 'image', 'justify', 'padx', 'pady', 'relief', 'state',
  129. 'takefocus', 'text', 'textvariable',
  130. 'underline', 'width', 'wraplength',
  131. )
  132. def create(self, **kwargs):
  133. return tkinter.Label(self.root, **kwargs)
  134. @add_standard_options(StandardOptionsTests)
  135. class ButtonTest(AbstractLabelTest, unittest.TestCase):
  136. OPTIONS = (
  137. 'activebackground', 'activeforeground', 'anchor',
  138. 'background', 'bitmap', 'borderwidth',
  139. 'command', 'compound', 'cursor', 'default',
  140. 'disabledforeground', 'font', 'foreground', 'height',
  141. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  142. 'image', 'justify', 'overrelief', 'padx', 'pady', 'relief',
  143. 'repeatdelay', 'repeatinterval',
  144. 'state', 'takefocus', 'text', 'textvariable',
  145. 'underline', 'width', 'wraplength')
  146. def create(self, **kwargs):
  147. return tkinter.Button(self.root, **kwargs)
  148. def test_default(self):
  149. widget = self.create()
  150. self.checkEnumParam(widget, 'default', 'active', 'disabled', 'normal')
  151. @add_standard_options(StandardOptionsTests)
  152. class CheckbuttonTest(AbstractLabelTest, unittest.TestCase):
  153. OPTIONS = (
  154. 'activebackground', 'activeforeground', 'anchor',
  155. 'background', 'bitmap', 'borderwidth',
  156. 'command', 'compound', 'cursor',
  157. 'disabledforeground', 'font', 'foreground', 'height',
  158. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  159. 'image', 'indicatoron', 'justify',
  160. 'offrelief', 'offvalue', 'onvalue', 'overrelief',
  161. 'padx', 'pady', 'relief', 'selectcolor', 'selectimage', 'state',
  162. 'takefocus', 'text', 'textvariable',
  163. 'tristateimage', 'tristatevalue',
  164. 'underline', 'variable', 'width', 'wraplength',
  165. )
  166. def create(self, **kwargs):
  167. return tkinter.Checkbutton(self.root, **kwargs)
  168. def test_offvalue(self):
  169. widget = self.create()
  170. self.checkParams(widget, 'offvalue', 1, 2.3, '', 'any string')
  171. def test_onvalue(self):
  172. widget = self.create()
  173. self.checkParams(widget, 'onvalue', 1, 2.3, '', 'any string')
  174. @add_standard_options(StandardOptionsTests)
  175. class RadiobuttonTest(AbstractLabelTest, unittest.TestCase):
  176. OPTIONS = (
  177. 'activebackground', 'activeforeground', 'anchor',
  178. 'background', 'bitmap', 'borderwidth',
  179. 'command', 'compound', 'cursor',
  180. 'disabledforeground', 'font', 'foreground', 'height',
  181. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  182. 'image', 'indicatoron', 'justify', 'offrelief', 'overrelief',
  183. 'padx', 'pady', 'relief', 'selectcolor', 'selectimage', 'state',
  184. 'takefocus', 'text', 'textvariable',
  185. 'tristateimage', 'tristatevalue',
  186. 'underline', 'value', 'variable', 'width', 'wraplength',
  187. )
  188. def create(self, **kwargs):
  189. return tkinter.Radiobutton(self.root, **kwargs)
  190. def test_value(self):
  191. widget = self.create()
  192. self.checkParams(widget, 'value', 1, 2.3, '', 'any string')
  193. @add_standard_options(StandardOptionsTests)
  194. class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
  195. OPTIONS = (
  196. 'activebackground', 'activeforeground', 'anchor',
  197. 'background', 'bitmap', 'borderwidth',
  198. 'compound', 'cursor', 'direction',
  199. 'disabledforeground', 'font', 'foreground', 'height',
  200. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  201. 'image', 'indicatoron', 'justify', 'menu',
  202. 'padx', 'pady', 'relief', 'state',
  203. 'takefocus', 'text', 'textvariable',
  204. 'underline', 'width', 'wraplength',
  205. )
  206. _conv_pixels = staticmethod(pixels_round)
  207. def create(self, **kwargs):
  208. return tkinter.Menubutton(self.root, **kwargs)
  209. def test_direction(self):
  210. widget = self.create()
  211. self.checkEnumParam(widget, 'direction',
  212. 'above', 'below', 'flush', 'left', 'right')
  213. def test_height(self):
  214. widget = self.create()
  215. self.checkIntegerParam(widget, 'height', 100, -100, 0, conv=str)
  216. test_highlightthickness = StandardOptionsTests.test_highlightthickness
  217. @unittest.skipIf(sys.platform == 'darwin',
  218. 'crashes with Cocoa Tk (issue19733)')
  219. def test_image(self):
  220. widget = self.create()
  221. image = tkinter.PhotoImage(master=self.root, name='image1')
  222. self.checkParam(widget, 'image', image, conv=str)
  223. errmsg = 'image "spam" doesn\'t exist'
  224. with self.assertRaises(tkinter.TclError) as cm:
  225. widget['image'] = 'spam'
  226. if errmsg is not None:
  227. self.assertEqual(str(cm.exception), errmsg)
  228. with self.assertRaises(tkinter.TclError) as cm:
  229. widget.configure({'image': 'spam'})
  230. if errmsg is not None:
  231. self.assertEqual(str(cm.exception), errmsg)
  232. def test_menu(self):
  233. widget = self.create()
  234. menu = tkinter.Menu(widget, name='menu')
  235. self.checkParam(widget, 'menu', menu, eq=widget_eq)
  236. menu.destroy()
  237. def test_padx(self):
  238. widget = self.create()
  239. self.checkPixelsParam(widget, 'padx', 3, 4.4, 5.6, '12m')
  240. self.checkParam(widget, 'padx', -2, expected=0)
  241. def test_pady(self):
  242. widget = self.create()
  243. self.checkPixelsParam(widget, 'pady', 3, 4.4, 5.6, '12m')
  244. self.checkParam(widget, 'pady', -2, expected=0)
  245. def test_width(self):
  246. widget = self.create()
  247. self.checkIntegerParam(widget, 'width', 402, -402, 0, conv=str)
  248. class OptionMenuTest(MenubuttonTest, unittest.TestCase):
  249. def create(self, default='b', values=('a', 'b', 'c'), **kwargs):
  250. return tkinter.OptionMenu(self.root, None, default, *values, **kwargs)
  251. @add_standard_options(IntegerSizeTests, StandardOptionsTests)
  252. class EntryTest(AbstractWidgetTest, unittest.TestCase):
  253. OPTIONS = (
  254. 'background', 'borderwidth', 'cursor',
  255. 'disabledbackground', 'disabledforeground',
  256. 'exportselection', 'font', 'foreground',
  257. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  258. 'insertbackground', 'insertborderwidth',
  259. 'insertofftime', 'insertontime', 'insertwidth',
  260. 'invalidcommand', 'justify', 'readonlybackground', 'relief',
  261. 'selectbackground', 'selectborderwidth', 'selectforeground',
  262. 'show', 'state', 'takefocus', 'textvariable',
  263. 'validate', 'validatecommand', 'width', 'xscrollcommand',
  264. )
  265. def create(self, **kwargs):
  266. return tkinter.Entry(self.root, **kwargs)
  267. def test_disabledbackground(self):
  268. widget = self.create()
  269. self.checkColorParam(widget, 'disabledbackground')
  270. def test_insertborderwidth(self):
  271. widget = self.create(insertwidth=100)
  272. self.checkPixelsParam(widget, 'insertborderwidth',
  273. 0, 1.3, 2.6, 6, -2, '10p')
  274. # insertborderwidth is bounded above by a half of insertwidth.
  275. self.checkParam(widget, 'insertborderwidth', 60, expected=100//2)
  276. def test_insertwidth(self):
  277. widget = self.create()
  278. self.checkPixelsParam(widget, 'insertwidth', 1.3, 3.6, '10p')
  279. self.checkParam(widget, 'insertwidth', 0.1, expected=2)
  280. self.checkParam(widget, 'insertwidth', -2, expected=2)
  281. if pixels_round(0.9) <= 0:
  282. self.checkParam(widget, 'insertwidth', 0.9, expected=2)
  283. else:
  284. self.checkParam(widget, 'insertwidth', 0.9, expected=1)
  285. def test_invalidcommand(self):
  286. widget = self.create()
  287. self.checkCommandParam(widget, 'invalidcommand')
  288. self.checkCommandParam(widget, 'invcmd')
  289. def test_readonlybackground(self):
  290. widget = self.create()
  291. self.checkColorParam(widget, 'readonlybackground')
  292. def test_show(self):
  293. widget = self.create()
  294. self.checkParam(widget, 'show', '*')
  295. self.checkParam(widget, 'show', '')
  296. self.checkParam(widget, 'show', ' ')
  297. def test_state(self):
  298. widget = self.create()
  299. self.checkEnumParam(widget, 'state',
  300. 'disabled', 'normal', 'readonly')
  301. def test_validate(self):
  302. widget = self.create()
  303. self.checkEnumParam(widget, 'validate',
  304. 'all', 'key', 'focus', 'focusin', 'focusout', 'none')
  305. def test_validatecommand(self):
  306. widget = self.create()
  307. self.checkCommandParam(widget, 'validatecommand')
  308. self.checkCommandParam(widget, 'vcmd')
  309. @add_standard_options(StandardOptionsTests)
  310. class SpinboxTest(EntryTest, unittest.TestCase):
  311. OPTIONS = (
  312. 'activebackground', 'background', 'borderwidth',
  313. 'buttonbackground', 'buttoncursor', 'buttondownrelief', 'buttonuprelief',
  314. 'command', 'cursor', 'disabledbackground', 'disabledforeground',
  315. 'exportselection', 'font', 'foreground', 'format', 'from',
  316. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  317. 'increment',
  318. 'insertbackground', 'insertborderwidth',
  319. 'insertofftime', 'insertontime', 'insertwidth',
  320. 'invalidcommand', 'justify', 'relief', 'readonlybackground',
  321. 'repeatdelay', 'repeatinterval',
  322. 'selectbackground', 'selectborderwidth', 'selectforeground',
  323. 'state', 'takefocus', 'textvariable', 'to',
  324. 'validate', 'validatecommand', 'values',
  325. 'width', 'wrap', 'xscrollcommand',
  326. )
  327. def create(self, **kwargs):
  328. return tkinter.Spinbox(self.root, **kwargs)
  329. test_show = None
  330. def test_buttonbackground(self):
  331. widget = self.create()
  332. self.checkColorParam(widget, 'buttonbackground')
  333. def test_buttoncursor(self):
  334. widget = self.create()
  335. self.checkCursorParam(widget, 'buttoncursor')
  336. def test_buttondownrelief(self):
  337. widget = self.create()
  338. self.checkReliefParam(widget, 'buttondownrelief')
  339. def test_buttonuprelief(self):
  340. widget = self.create()
  341. self.checkReliefParam(widget, 'buttonuprelief')
  342. def test_format(self):
  343. widget = self.create()
  344. self.checkParam(widget, 'format', '%2f')
  345. self.checkParam(widget, 'format', '%2.2f')
  346. self.checkParam(widget, 'format', '%.2f')
  347. self.checkParam(widget, 'format', '%2.f')
  348. self.checkInvalidParam(widget, 'format', '%2e-1f')
  349. self.checkInvalidParam(widget, 'format', '2.2')
  350. self.checkInvalidParam(widget, 'format', '%2.-2f')
  351. self.checkParam(widget, 'format', '%-2.02f')
  352. self.checkParam(widget, 'format', '% 2.02f')
  353. self.checkParam(widget, 'format', '% -2.200f')
  354. self.checkParam(widget, 'format', '%09.200f')
  355. self.checkInvalidParam(widget, 'format', '%d')
  356. def test_from(self):
  357. widget = self.create()
  358. self.checkParam(widget, 'to', 100.0)
  359. self.checkFloatParam(widget, 'from', -10, 10.2, 11.7)
  360. self.checkInvalidParam(widget, 'from', 200,
  361. errmsg='-to value must be greater than -from value')
  362. def test_increment(self):
  363. widget = self.create()
  364. self.checkFloatParam(widget, 'increment', -1, 1, 10.2, 12.8, 0)
  365. def test_to(self):
  366. widget = self.create()
  367. self.checkParam(widget, 'from', -100.0)
  368. self.checkFloatParam(widget, 'to', -10, 10.2, 11.7)
  369. self.checkInvalidParam(widget, 'to', -200,
  370. errmsg='-to value must be greater than -from value')
  371. def test_values(self):
  372. # XXX
  373. widget = self.create()
  374. self.assertEqual(widget['values'], '')
  375. self.checkParam(widget, 'values', 'mon tue wed thur')
  376. self.checkParam(widget, 'values', ('mon', 'tue', 'wed', 'thur'),
  377. expected='mon tue wed thur')
  378. self.checkParam(widget, 'values', (42, 3.14, '', 'any string'),
  379. expected='42 3.14 {} {any string}')
  380. self.checkParam(widget, 'values', '')
  381. def test_wrap(self):
  382. widget = self.create()
  383. self.checkBooleanParam(widget, 'wrap')
  384. def test_bbox(self):
  385. widget = self.create()
  386. self.assertIsBoundingBox(widget.bbox(0))
  387. self.assertRaises(tkinter.TclError, widget.bbox, 'noindex')
  388. self.assertRaises(tkinter.TclError, widget.bbox, None)
  389. self.assertRaises(TypeError, widget.bbox)
  390. self.assertRaises(TypeError, widget.bbox, 0, 1)
  391. def test_selection_element(self):
  392. widget = self.create()
  393. self.assertEqual(widget.selection_element(), "none")
  394. widget.selection_element("buttonup")
  395. self.assertEqual(widget.selection_element(), "buttonup")
  396. widget.selection_element("buttondown")
  397. self.assertEqual(widget.selection_element(), "buttondown")
  398. @add_standard_options(StandardOptionsTests)
  399. class TextTest(AbstractWidgetTest, unittest.TestCase):
  400. OPTIONS = (
  401. 'autoseparators', 'background', 'blockcursor', 'borderwidth',
  402. 'cursor', 'endline', 'exportselection',
  403. 'font', 'foreground', 'height',
  404. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  405. 'inactiveselectbackground', 'insertbackground', 'insertborderwidth',
  406. 'insertofftime', 'insertontime', 'insertunfocussed', 'insertwidth',
  407. 'maxundo', 'padx', 'pady', 'relief',
  408. 'selectbackground', 'selectborderwidth', 'selectforeground',
  409. 'setgrid', 'spacing1', 'spacing2', 'spacing3', 'startline', 'state',
  410. 'tabs', 'tabstyle', 'takefocus', 'undo', 'width', 'wrap',
  411. 'xscrollcommand', 'yscrollcommand',
  412. )
  413. if tcl_version < (8, 5):
  414. _stringify = True
  415. def create(self, **kwargs):
  416. return tkinter.Text(self.root, **kwargs)
  417. def test_autoseparators(self):
  418. widget = self.create()
  419. self.checkBooleanParam(widget, 'autoseparators')
  420. @requires_tcl(8, 5)
  421. def test_blockcursor(self):
  422. widget = self.create()
  423. self.checkBooleanParam(widget, 'blockcursor')
  424. @requires_tcl(8, 5)
  425. def test_endline(self):
  426. widget = self.create()
  427. text = '\n'.join('Line %d' for i in range(100))
  428. widget.insert('end', text)
  429. self.checkParam(widget, 'endline', 200, expected='')
  430. self.checkParam(widget, 'endline', -10, expected='')
  431. self.checkInvalidParam(widget, 'endline', 'spam',
  432. errmsg='expected integer but got "spam"')
  433. self.checkParam(widget, 'endline', 50)
  434. self.checkParam(widget, 'startline', 15)
  435. self.checkInvalidParam(widget, 'endline', 10,
  436. errmsg='-startline must be less than or equal to -endline')
  437. def test_height(self):
  438. widget = self.create()
  439. self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, '3c')
  440. self.checkParam(widget, 'height', -100, expected=1)
  441. self.checkParam(widget, 'height', 0, expected=1)
  442. def test_maxundo(self):
  443. widget = self.create()
  444. self.checkIntegerParam(widget, 'maxundo', 0, 5, -1)
  445. @requires_tcl(8, 5)
  446. def test_inactiveselectbackground(self):
  447. widget = self.create()
  448. self.checkColorParam(widget, 'inactiveselectbackground')
  449. @requires_tcl(8, 6)
  450. def test_insertunfocussed(self):
  451. widget = self.create()
  452. self.checkEnumParam(widget, 'insertunfocussed',
  453. 'hollow', 'none', 'solid')
  454. def test_selectborderwidth(self):
  455. widget = self.create()
  456. self.checkPixelsParam(widget, 'selectborderwidth',
  457. 1.3, 2.6, -2, '10p', conv=noconv,
  458. keep_orig=tcl_version >= (8, 5))
  459. def test_spacing1(self):
  460. widget = self.create()
  461. self.checkPixelsParam(widget, 'spacing1', 20, 21.4, 22.6, '0.5c')
  462. self.checkParam(widget, 'spacing1', -5, expected=0)
  463. def test_spacing2(self):
  464. widget = self.create()
  465. self.checkPixelsParam(widget, 'spacing2', 5, 6.4, 7.6, '0.1c')
  466. self.checkParam(widget, 'spacing2', -1, expected=0)
  467. def test_spacing3(self):
  468. widget = self.create()
  469. self.checkPixelsParam(widget, 'spacing3', 20, 21.4, 22.6, '0.5c')
  470. self.checkParam(widget, 'spacing3', -10, expected=0)
  471. @requires_tcl(8, 5)
  472. def test_startline(self):
  473. widget = self.create()
  474. text = '\n'.join('Line %d' for i in range(100))
  475. widget.insert('end', text)
  476. self.checkParam(widget, 'startline', 200, expected='')
  477. self.checkParam(widget, 'startline', -10, expected='')
  478. self.checkInvalidParam(widget, 'startline', 'spam',
  479. errmsg='expected integer but got "spam"')
  480. self.checkParam(widget, 'startline', 10)
  481. self.checkParam(widget, 'endline', 50)
  482. self.checkInvalidParam(widget, 'startline', 70,
  483. errmsg='-startline must be less than or equal to -endline')
  484. def test_state(self):
  485. widget = self.create()
  486. if tcl_version < (8, 5):
  487. self.checkParams(widget, 'state', 'disabled', 'normal')
  488. else:
  489. self.checkEnumParam(widget, 'state', 'disabled', 'normal')
  490. def test_tabs(self):
  491. widget = self.create()
  492. if get_tk_patchlevel() < (8, 5, 11):
  493. self.checkParam(widget, 'tabs', (10.2, 20.7, '1i', '2i'),
  494. expected=('10.2', '20.7', '1i', '2i'))
  495. else:
  496. self.checkParam(widget, 'tabs', (10.2, 20.7, '1i', '2i'))
  497. self.checkParam(widget, 'tabs', '10.2 20.7 1i 2i',
  498. expected=('10.2', '20.7', '1i', '2i'))
  499. self.checkParam(widget, 'tabs', '2c left 4c 6c center',
  500. expected=('2c', 'left', '4c', '6c', 'center'))
  501. self.checkInvalidParam(widget, 'tabs', 'spam',
  502. errmsg='bad screen distance "spam"',
  503. keep_orig=tcl_version >= (8, 5))
  504. @requires_tcl(8, 5)
  505. def test_tabstyle(self):
  506. widget = self.create()
  507. self.checkEnumParam(widget, 'tabstyle', 'tabular', 'wordprocessor')
  508. def test_undo(self):
  509. widget = self.create()
  510. self.checkBooleanParam(widget, 'undo')
  511. def test_width(self):
  512. widget = self.create()
  513. self.checkIntegerParam(widget, 'width', 402)
  514. self.checkParam(widget, 'width', -402, expected=1)
  515. self.checkParam(widget, 'width', 0, expected=1)
  516. def test_wrap(self):
  517. widget = self.create()
  518. if tcl_version < (8, 5):
  519. self.checkParams(widget, 'wrap', 'char', 'none', 'word')
  520. else:
  521. self.checkEnumParam(widget, 'wrap', 'char', 'none', 'word')
  522. def test_bbox(self):
  523. widget = self.create()
  524. self.assertIsBoundingBox(widget.bbox('1.1'))
  525. self.assertIsNone(widget.bbox('end'))
  526. self.assertRaises(tkinter.TclError, widget.bbox, 'noindex')
  527. self.assertRaises(tkinter.TclError, widget.bbox, None)
  528. self.assertRaises(TypeError, widget.bbox)
  529. self.assertRaises(TypeError, widget.bbox, '1.1', 'end')
  530. @add_standard_options(PixelSizeTests, StandardOptionsTests)
  531. class CanvasTest(AbstractWidgetTest, unittest.TestCase):
  532. OPTIONS = (
  533. 'background', 'borderwidth',
  534. 'closeenough', 'confine', 'cursor', 'height',
  535. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  536. 'insertbackground', 'insertborderwidth',
  537. 'insertofftime', 'insertontime', 'insertwidth',
  538. 'offset', 'relief', 'scrollregion',
  539. 'selectbackground', 'selectborderwidth', 'selectforeground',
  540. 'state', 'takefocus',
  541. 'xscrollcommand', 'xscrollincrement',
  542. 'yscrollcommand', 'yscrollincrement', 'width',
  543. )
  544. _conv_pixels = round
  545. _stringify = True
  546. def create(self, **kwargs):
  547. return tkinter.Canvas(self.root, **kwargs)
  548. def test_closeenough(self):
  549. widget = self.create()
  550. self.checkFloatParam(widget, 'closeenough', 24, 2.4, 3.6, -3,
  551. conv=float)
  552. def test_confine(self):
  553. widget = self.create()
  554. self.checkBooleanParam(widget, 'confine')
  555. def test_offset(self):
  556. widget = self.create()
  557. self.assertEqual(widget['offset'], '0,0')
  558. self.checkParams(widget, 'offset',
  559. 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'center')
  560. self.checkParam(widget, 'offset', '10,20')
  561. self.checkParam(widget, 'offset', '#5,6')
  562. self.checkInvalidParam(widget, 'offset', 'spam')
  563. def test_scrollregion(self):
  564. widget = self.create()
  565. self.checkParam(widget, 'scrollregion', '0 0 200 150')
  566. self.checkParam(widget, 'scrollregion', (0, 0, 200, 150),
  567. expected='0 0 200 150')
  568. self.checkParam(widget, 'scrollregion', '')
  569. self.checkInvalidParam(widget, 'scrollregion', 'spam',
  570. errmsg='bad scrollRegion "spam"')
  571. self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200, 'spam'))
  572. self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200))
  573. self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200, 150, 0))
  574. def test_state(self):
  575. widget = self.create()
  576. self.checkEnumParam(widget, 'state', 'disabled', 'normal',
  577. errmsg='bad state value "{}": must be normal or disabled')
  578. def test_xscrollincrement(self):
  579. widget = self.create()
  580. self.checkPixelsParam(widget, 'xscrollincrement',
  581. 40, 0, 41.2, 43.6, -40, '0.5i')
  582. def test_yscrollincrement(self):
  583. widget = self.create()
  584. self.checkPixelsParam(widget, 'yscrollincrement',
  585. 10, 0, 11.2, 13.6, -10, '0.1i')
  586. @add_standard_options(IntegerSizeTests, StandardOptionsTests)
  587. class ListboxTest(AbstractWidgetTest, unittest.TestCase):
  588. OPTIONS = (
  589. 'activestyle', 'background', 'borderwidth', 'cursor',
  590. 'disabledforeground', 'exportselection',
  591. 'font', 'foreground', 'height',
  592. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  593. 'justify', 'listvariable', 'relief',
  594. 'selectbackground', 'selectborderwidth', 'selectforeground',
  595. 'selectmode', 'setgrid', 'state',
  596. 'takefocus', 'width', 'xscrollcommand', 'yscrollcommand',
  597. )
  598. def create(self, **kwargs):
  599. return tkinter.Listbox(self.root, **kwargs)
  600. def test_activestyle(self):
  601. widget = self.create()
  602. self.checkEnumParam(widget, 'activestyle',
  603. 'dotbox', 'none', 'underline')
  604. test_justify = requires_tcl(8, 6, 5)(StandardOptionsTests.test_justify)
  605. def test_listvariable(self):
  606. widget = self.create()
  607. var = tkinter.DoubleVar(self.root)
  608. self.checkVariableParam(widget, 'listvariable', var)
  609. def test_selectmode(self):
  610. widget = self.create()
  611. self.checkParam(widget, 'selectmode', 'single')
  612. self.checkParam(widget, 'selectmode', 'browse')
  613. self.checkParam(widget, 'selectmode', 'multiple')
  614. self.checkParam(widget, 'selectmode', 'extended')
  615. def test_state(self):
  616. widget = self.create()
  617. self.checkEnumParam(widget, 'state', 'disabled', 'normal')
  618. def test_itemconfigure(self):
  619. widget = self.create()
  620. with self.assertRaisesRegex(TclError, 'item number "0" out of range'):
  621. widget.itemconfigure(0)
  622. colors = 'red orange yellow green blue white violet'.split()
  623. widget.insert('end', *colors)
  624. for i, color in enumerate(colors):
  625. widget.itemconfigure(i, background=color)
  626. with self.assertRaises(TypeError):
  627. widget.itemconfigure()
  628. with self.assertRaisesRegex(TclError, 'bad listbox index "red"'):
  629. widget.itemconfigure('red')
  630. self.assertEqual(widget.itemconfigure(0, 'background'),
  631. ('background', 'background', 'Background', '', 'red'))
  632. self.assertEqual(widget.itemconfigure('end', 'background'),
  633. ('background', 'background', 'Background', '', 'violet'))
  634. self.assertEqual(widget.itemconfigure('@0,0', 'background'),
  635. ('background', 'background', 'Background', '', 'red'))
  636. d = widget.itemconfigure(0)
  637. self.assertIsInstance(d, dict)
  638. for k, v in d.items():
  639. self.assertIn(len(v), (2, 5))
  640. if len(v) == 5:
  641. self.assertEqual(v, widget.itemconfigure(0, k))
  642. self.assertEqual(v[4], widget.itemcget(0, k))
  643. def check_itemconfigure(self, name, value):
  644. widget = self.create()
  645. widget.insert('end', 'a', 'b', 'c', 'd')
  646. widget.itemconfigure(0, **{name: value})
  647. self.assertEqual(widget.itemconfigure(0, name)[4], value)
  648. self.assertEqual(widget.itemcget(0, name), value)
  649. with self.assertRaisesRegex(TclError, 'unknown color name "spam"'):
  650. widget.itemconfigure(0, **{name: 'spam'})
  651. def test_itemconfigure_background(self):
  652. self.check_itemconfigure('background', '#ff0000')
  653. def test_itemconfigure_bg(self):
  654. self.check_itemconfigure('bg', '#ff0000')
  655. def test_itemconfigure_fg(self):
  656. self.check_itemconfigure('fg', '#110022')
  657. def test_itemconfigure_foreground(self):
  658. self.check_itemconfigure('foreground', '#110022')
  659. def test_itemconfigure_selectbackground(self):
  660. self.check_itemconfigure('selectbackground', '#110022')
  661. def test_itemconfigure_selectforeground(self):
  662. self.check_itemconfigure('selectforeground', '#654321')
  663. def test_box(self):
  664. lb = self.create()
  665. lb.insert(0, *('el%d' % i for i in range(8)))
  666. lb.pack()
  667. self.assertIsBoundingBox(lb.bbox(0))
  668. self.assertIsNone(lb.bbox(-1))
  669. self.assertIsNone(lb.bbox(10))
  670. self.assertRaises(TclError, lb.bbox, 'noindex')
  671. self.assertRaises(TclError, lb.bbox, None)
  672. self.assertRaises(TypeError, lb.bbox)
  673. self.assertRaises(TypeError, lb.bbox, 0, 1)
  674. def test_curselection(self):
  675. lb = self.create()
  676. lb.insert(0, *('el%d' % i for i in range(8)))
  677. lb.selection_clear(0, tkinter.END)
  678. lb.selection_set(2, 4)
  679. lb.selection_set(6)
  680. self.assertEqual(lb.curselection(), (2, 3, 4, 6))
  681. self.assertRaises(TypeError, lb.curselection, 0)
  682. def test_get(self):
  683. lb = self.create()
  684. lb.insert(0, *('el%d' % i for i in range(8)))
  685. self.assertEqual(lb.get(0), 'el0')
  686. self.assertEqual(lb.get(3), 'el3')
  687. self.assertEqual(lb.get('end'), 'el7')
  688. self.assertEqual(lb.get(8), '')
  689. self.assertEqual(lb.get(-1), '')
  690. self.assertEqual(lb.get(3, 5), ('el3', 'el4', 'el5'))
  691. self.assertEqual(lb.get(5, 'end'), ('el5', 'el6', 'el7'))
  692. self.assertEqual(lb.get(5, 0), ())
  693. self.assertEqual(lb.get(0, 0), ('el0',))
  694. self.assertRaises(TclError, lb.get, 'noindex')
  695. self.assertRaises(TclError, lb.get, None)
  696. self.assertRaises(TypeError, lb.get)
  697. self.assertRaises(TclError, lb.get, 'end', 'noindex')
  698. self.assertRaises(TypeError, lb.get, 1, 2, 3)
  699. self.assertRaises(TclError, lb.get, 2.4)
  700. @add_standard_options(PixelSizeTests, StandardOptionsTests)
  701. class ScaleTest(AbstractWidgetTest, unittest.TestCase):
  702. OPTIONS = (
  703. 'activebackground', 'background', 'bigincrement', 'borderwidth',
  704. 'command', 'cursor', 'digits', 'font', 'foreground', 'from',
  705. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  706. 'label', 'length', 'orient', 'relief',
  707. 'repeatdelay', 'repeatinterval',
  708. 'resolution', 'showvalue', 'sliderlength', 'sliderrelief', 'state',
  709. 'takefocus', 'tickinterval', 'to', 'troughcolor', 'variable', 'width',
  710. )
  711. default_orient = 'vertical'
  712. def create(self, **kwargs):
  713. return tkinter.Scale(self.root, **kwargs)
  714. def test_bigincrement(self):
  715. widget = self.create()
  716. self.checkFloatParam(widget, 'bigincrement', 12.4, 23.6, -5)
  717. def test_digits(self):
  718. widget = self.create()
  719. self.checkIntegerParam(widget, 'digits', 5, 0)
  720. def test_from(self):
  721. widget = self.create()
  722. self.checkFloatParam(widget, 'from', 100, 14.9, 15.1, conv=float_round)
  723. def test_label(self):
  724. widget = self.create()
  725. self.checkParam(widget, 'label', 'any string')
  726. self.checkParam(widget, 'label', '')
  727. def test_length(self):
  728. widget = self.create()
  729. self.checkPixelsParam(widget, 'length', 130, 131.2, 135.6, '5i')
  730. def test_resolution(self):
  731. widget = self.create()
  732. self.checkFloatParam(widget, 'resolution', 4.2, 0, 6.7, -2)
  733. def test_showvalue(self):
  734. widget = self.create()
  735. self.checkBooleanParam(widget, 'showvalue')
  736. def test_sliderlength(self):
  737. widget = self.create()
  738. self.checkPixelsParam(widget, 'sliderlength',
  739. 10, 11.2, 15.6, -3, '3m')
  740. def test_sliderrelief(self):
  741. widget = self.create()
  742. self.checkReliefParam(widget, 'sliderrelief')
  743. def test_tickinterval(self):
  744. widget = self.create()
  745. self.checkFloatParam(widget, 'tickinterval', 1, 4.3, 7.6, 0,
  746. conv=float_round)
  747. self.checkParam(widget, 'tickinterval', -2, expected=2,
  748. conv=float_round)
  749. def test_to(self):
  750. widget = self.create()
  751. self.checkFloatParam(widget, 'to', 300, 14.9, 15.1, -10,
  752. conv=float_round)
  753. @add_standard_options(PixelSizeTests, StandardOptionsTests)
  754. class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
  755. OPTIONS = (
  756. 'activebackground', 'activerelief',
  757. 'background', 'borderwidth',
  758. 'command', 'cursor', 'elementborderwidth',
  759. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  760. 'jump', 'orient', 'relief',
  761. 'repeatdelay', 'repeatinterval',
  762. 'takefocus', 'troughcolor', 'width',
  763. )
  764. _conv_pixels = round
  765. _stringify = True
  766. default_orient = 'vertical'
  767. def create(self, **kwargs):
  768. return tkinter.Scrollbar(self.root, **kwargs)
  769. def test_activerelief(self):
  770. widget = self.create()
  771. self.checkReliefParam(widget, 'activerelief')
  772. def test_elementborderwidth(self):
  773. widget = self.create()
  774. self.checkPixelsParam(widget, 'elementborderwidth', 4.3, 5.6, -2, '1m')
  775. def test_orient(self):
  776. widget = self.create()
  777. self.checkEnumParam(widget, 'orient', 'vertical', 'horizontal',
  778. errmsg='bad orientation "{}": must be vertical or horizontal')
  779. def test_activate(self):
  780. sb = self.create()
  781. for e in ('arrow1', 'slider', 'arrow2'):
  782. sb.activate(e)
  783. self.assertEqual(sb.activate(), e)
  784. sb.activate('')
  785. self.assertIsNone(sb.activate())
  786. self.assertRaises(TypeError, sb.activate, 'arrow1', 'arrow2')
  787. def test_set(self):
  788. sb = self.create()
  789. sb.set(0.2, 0.4)
  790. self.assertEqual(sb.get(), (0.2, 0.4))
  791. self.assertRaises(TclError, sb.set, 'abc', 'def')
  792. self.assertRaises(TclError, sb.set, 0.6, 'def')
  793. self.assertRaises(TclError, sb.set, 0.6, None)
  794. self.assertRaises(TypeError, sb.set, 0.6)
  795. self.assertRaises(TypeError, sb.set, 0.6, 0.7, 0.8)
  796. @add_standard_options(StandardOptionsTests)
  797. class PanedWindowTest(AbstractWidgetTest, unittest.TestCase):
  798. OPTIONS = (
  799. 'background', 'borderwidth', 'cursor',
  800. 'handlepad', 'handlesize', 'height',
  801. 'opaqueresize', 'orient',
  802. 'proxybackground', 'proxyborderwidth', 'proxyrelief',
  803. 'relief',
  804. 'sashcursor', 'sashpad', 'sashrelief', 'sashwidth',
  805. 'showhandle', 'width',
  806. )
  807. default_orient = 'horizontal'
  808. def create(self, **kwargs):
  809. return tkinter.PanedWindow(self.root, **kwargs)
  810. def test_handlepad(self):
  811. widget = self.create()
  812. self.checkPixelsParam(widget, 'handlepad', 5, 6.4, 7.6, -3, '1m')
  813. def test_handlesize(self):
  814. widget = self.create()
  815. self.checkPixelsParam(widget, 'handlesize', 8, 9.4, 10.6, -3, '2m',
  816. conv=noconv)
  817. def test_height(self):
  818. widget = self.create()
  819. self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, -100, 0, '1i',
  820. conv=noconv)
  821. def test_opaqueresize(self):
  822. widget = self.create()
  823. self.checkBooleanParam(widget, 'opaqueresize')
  824. @requires_tcl(8, 6, 5)
  825. def test_proxybackground(self):
  826. widget = self.create()
  827. self.checkColorParam(widget, 'proxybackground')
  828. @requires_tcl(8, 6, 5)
  829. def test_proxyborderwidth(self):
  830. widget = self.create()
  831. self.checkPixelsParam(widget, 'proxyborderwidth',
  832. 0, 1.3, 2.9, 6, -2, '10p',
  833. conv=noconv)
  834. @requires_tcl(8, 6, 5)
  835. def test_proxyrelief(self):
  836. widget = self.create()
  837. self.checkReliefParam(widget, 'proxyrelief')
  838. def test_sashcursor(self):
  839. widget = self.create()
  840. self.checkCursorParam(widget, 'sashcursor')
  841. def test_sashpad(self):
  842. widget = self.create()
  843. self.checkPixelsParam(widget, 'sashpad', 8, 1.3, 2.6, -2, '2m')
  844. def test_sashrelief(self):
  845. widget = self.create()
  846. self.checkReliefParam(widget, 'sashrelief')
  847. def test_sashwidth(self):
  848. widget = self.create()
  849. self.checkPixelsParam(widget, 'sashwidth', 10, 11.1, 15.6, -3, '1m',
  850. conv=noconv)
  851. def test_showhandle(self):
  852. widget = self.create()
  853. self.checkBooleanParam(widget, 'showhandle')
  854. def test_width(self):
  855. widget = self.create()
  856. self.checkPixelsParam(widget, 'width', 402, 403.4, 404.6, -402, 0, '5i',
  857. conv=noconv)
  858. def create2(self):
  859. p = self.create()
  860. b = tkinter.Button(p)
  861. c = tkinter.Button(p)
  862. p.add(b)
  863. p.add(c)
  864. return p, b, c
  865. def test_paneconfigure(self):
  866. p, b, c = self.create2()
  867. self.assertRaises(TypeError, p.paneconfigure)
  868. d = p.paneconfigure(b)
  869. self.assertIsInstance(d, dict)
  870. for k, v in d.items():
  871. self.assertEqual(len(v), 5)
  872. self.assertEqual(v, p.paneconfigure(b, k))
  873. self.assertEqual(v[4], p.panecget(b, k))
  874. def check_paneconfigure(self, p, b, name, value, expected, stringify=False):
  875. conv = lambda x: x
  876. if not self.wantobjects or stringify:
  877. expected = str(expected)
  878. if self.wantobjects and stringify:
  879. conv = str
  880. p.paneconfigure(b, **{name: value})
  881. self.assertEqual(conv(p.paneconfigure(b, name)[4]), expected)
  882. self.assertEqual(conv(p.panecget(b, name)), expected)
  883. def check_paneconfigure_bad(self, p, b, name, msg):
  884. with self.assertRaisesRegex(TclError, msg):
  885. p.paneconfigure(b, **{name: 'badValue'})
  886. def test_paneconfigure_after(self):
  887. p, b, c = self.create2()
  888. self.check_paneconfigure(p, b, 'after', c, str(c))
  889. self.check_paneconfigure_bad(p, b, 'after',
  890. 'bad window path name "badValue"')
  891. def test_paneconfigure_before(self):
  892. p, b, c = self.create2()
  893. self.check_paneconfigure(p, b, 'before', c, str(c))
  894. self.check_paneconfigure_bad(p, b, 'before',
  895. 'bad window path name "badValue"')
  896. def test_paneconfigure_height(self):
  897. p, b, c = self.create2()
  898. self.check_paneconfigure(p, b, 'height', 10, 10,
  899. stringify=get_tk_patchlevel() < (8, 5, 11))
  900. self.check_paneconfigure_bad(p, b, 'height',
  901. 'bad screen distance "badValue"')
  902. @requires_tcl(8, 5)
  903. def test_paneconfigure_hide(self):
  904. p, b, c = self.create2()
  905. self.check_paneconfigure(p, b, 'hide', False, 0)
  906. self.check_paneconfigure_bad(p, b, 'hide',
  907. 'expected boolean value but got "badValue"')
  908. def test_paneconfigure_minsize(self):
  909. p, b, c = self.create2()
  910. self.check_paneconfigure(p, b, 'minsize', 10, 10)
  911. self.check_paneconfigure_bad(p, b, 'minsize',
  912. 'bad screen distance "badValue"')
  913. def test_paneconfigure_padx(self):
  914. p, b, c = self.create2()
  915. self.check_paneconfigure(p, b, 'padx', 1.3, 1)
  916. self.check_paneconfigure_bad(p, b, 'padx',
  917. 'bad screen distance "badValue"')
  918. def test_paneconfigure_pady(self):
  919. p, b, c = self.create2()
  920. self.check_paneconfigure(p, b, 'pady', 1.3, 1)
  921. self.check_paneconfigure_bad(p, b, 'pady',
  922. 'bad screen distance "badValue"')
  923. def test_paneconfigure_sticky(self):
  924. p, b, c = self.create2()
  925. self.check_paneconfigure(p, b, 'sticky', 'nsew', 'nesw')
  926. self.check_paneconfigure_bad(p, b, 'sticky',
  927. 'bad stickyness value "badValue": must '
  928. 'be a string containing zero or more of '
  929. 'n, e, s, and w')
  930. @requires_tcl(8, 5)
  931. def test_paneconfigure_stretch(self):
  932. p, b, c = self.create2()
  933. self.check_paneconfigure(p, b, 'stretch', 'alw', 'always')
  934. self.check_paneconfigure_bad(p, b, 'stretch',
  935. 'bad stretch "badValue": must be '
  936. 'always, first, last, middle, or never')
  937. def test_paneconfigure_width(self):
  938. p, b, c = self.create2()
  939. self.check_paneconfigure(p, b, 'width', 10, 10,
  940. stringify=get_tk_patchlevel() < (8, 5, 11))
  941. self.check_paneconfigure_bad(p, b, 'width',
  942. 'bad screen distance "badValue"')
  943. @add_standard_options(StandardOptionsTests)
  944. class MenuTest(AbstractWidgetTest, unittest.TestCase):
  945. OPTIONS = (
  946. 'activebackground', 'activeborderwidth', 'activeforeground',
  947. 'background', 'borderwidth', 'cursor',
  948. 'disabledforeground', 'font', 'foreground',
  949. 'postcommand', 'relief', 'selectcolor', 'takefocus',
  950. 'tearoff', 'tearoffcommand', 'title', 'type',
  951. )
  952. _conv_pixels = noconv
  953. def create(self, **kwargs):
  954. return tkinter.Menu(self.root, **kwargs)
  955. def test_postcommand(self):
  956. widget = self.create()
  957. self.checkCommandParam(widget, 'postcommand')
  958. def test_tearoff(self):
  959. widget = self.create()
  960. self.checkBooleanParam(widget, 'tearoff')
  961. def test_tearoffcommand(self):
  962. widget = self.create()
  963. self.checkCommandParam(widget, 'tearoffcommand')
  964. def test_title(self):
  965. widget = self.create()
  966. self.checkParam(widget, 'title', 'any string')
  967. def test_type(self):
  968. widget = self.create()
  969. self.checkEnumParam(widget, 'type',
  970. 'normal', 'tearoff', 'menubar')
  971. def test_entryconfigure(self):
  972. m1 = self.create()
  973. m1.add_command(label='test')
  974. self.assertRaises(TypeError, m1.entryconfigure)
  975. with self.assertRaisesRegex(TclError, 'bad menu entry index "foo"'):
  976. m1.entryconfigure('foo')
  977. d = m1.entryconfigure(1)
  978. self.assertIsInstance(d, dict)
  979. for k, v in d.items():
  980. self.assertIsInstance(k, str)
  981. self.assertIsInstance(v, tuple)
  982. self.assertEqual(len(v), 5)
  983. self.assertEqual(v[0], k)
  984. self.assertEqual(m1.entrycget(1, k), v[4])
  985. m1.destroy()
  986. def test_entryconfigure_label(self):
  987. m1 = self.create()
  988. m1.add_command(label='test')
  989. self.assertEqual(m1.entrycget(1, 'label'), 'test')
  990. m1.entryconfigure(1, label='changed')
  991. self.assertEqual(m1.entrycget(1, 'label'), 'changed')
  992. def test_entryconfigure_variable(self):
  993. m1 = self.create()
  994. v1 = tkinter.BooleanVar(self.root)
  995. v2 = tkinter.BooleanVar(self.root)
  996. m1.add_checkbutton(variable=v1, onvalue=True, offvalue=False,
  997. label='Nonsense')
  998. self.assertEqual(str(m1.entrycget(1, 'variable')), str(v1))
  999. m1.entryconfigure(1, variable=v2)
  1000. self.assertEqual(str(m1.entrycget(1, 'variable')), str(v2))
  1001. @add_standard_options(PixelSizeTests, StandardOptionsTests)
  1002. class MessageTest(AbstractWidgetTest, unittest.TestCase):
  1003. OPTIONS = (
  1004. 'anchor', 'aspect', 'background', 'borderwidth',
  1005. 'cursor', 'font', 'foreground',
  1006. 'highlightbackground', 'highlightcolor', 'highlightthickness',
  1007. 'justify', 'padx', 'pady', 'relief',
  1008. 'takefocus', 'text', 'textvariable', 'width',
  1009. )
  1010. _conv_pad_pixels = noconv
  1011. def create(self, **kwargs):
  1012. return tkinter.Message(self.root, **kwargs)
  1013. def test_aspect(self):
  1014. widget = self.create()
  1015. self.checkIntegerParam(widget, 'aspect', 250, 0, -300)
  1016. tests_gui = (
  1017. ButtonTest, CanvasTest, CheckbuttonTest, EntryTest,
  1018. FrameTest, LabelFrameTest,LabelTest, ListboxTest,
  1019. MenubuttonTest, MenuTest, MessageTest, OptionMenuTest,
  1020. PanedWindowTest, RadiobuttonTest, ScaleTest, ScrollbarTest,
  1021. SpinboxTest, TextTest, ToplevelTest,
  1022. )
  1023. if __name__ == '__main__':
  1024. unittest.main()