Commit d00aff2f authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #22236: Tkinter tests now don't reuse default root window. New root

window is created for every test class.

Fixed Tkinter images copying operations in NoDefaultRoot mode.

Tcl command names generated for "after" callbacks now contains a name of
original function.
parent ee558260
...@@ -6,7 +6,7 @@ from test import support ...@@ -6,7 +6,7 @@ from test import support
support.import_module('_tkinter') support.import_module('_tkinter')
# Make sure tkinter._fix runs to set up the environment # Make sure tkinter._fix runs to set up the environment
support.import_fresh_module('tkinter') tkinter = support.import_fresh_module('tkinter')
# Skip test if tk cannot be initialized. # Skip test if tk cannot be initialized.
support.requires('gui') support.requires('gui')
...@@ -14,20 +14,24 @@ support.requires('gui') ...@@ -14,20 +14,24 @@ support.requires('gui')
from _tkinter import TclError from _tkinter import TclError
from tkinter import ttk from tkinter import ttk
from tkinter.test import runtktests from tkinter.test import runtktests
from tkinter.test.support import get_tk_root
root = None
try: try:
ttk.Button() root = tkinter.Tk()
button = ttk.Button(root)
button.destroy()
del button
except TclError as msg: except TclError as msg:
# assuming ttk is not available # assuming ttk is not available
raise unittest.SkipTest("ttk not available: %s" % msg) raise unittest.SkipTest("ttk not available: %s" % msg)
finally:
if root is not None:
root.destroy()
del root
def test_main(): def test_main():
try: support.run_unittest(
support.run_unittest( *runtktests.get_tests(text=False, packages=['test_ttk']))
*runtktests.get_tests(text=False, packages=['test_ttk']))
finally:
get_tk_root().destroy()
if __name__ == '__main__': if __name__ == '__main__':
test_main() test_main()
...@@ -562,6 +562,7 @@ class Misc: ...@@ -562,6 +562,7 @@ class Misc:
self.deletecommand(name) self.deletecommand(name)
except TclError: except TclError:
pass pass
callit.__name__ = func.__name__
name = self._register(callit) name = self._register(callit)
return self.tk.call('after', ms, name) return self.tk.call('after', ms, name)
def after_idle(self, func, *args): def after_idle(self, func, *args):
...@@ -3351,7 +3352,7 @@ class Image: ...@@ -3351,7 +3352,7 @@ class Image:
master = _default_root master = _default_root
if not master: if not master:
raise RuntimeError('Too early to create image') raise RuntimeError('Too early to create image')
self.tk = master.tk self.tk = getattr(master, 'tk', master)
if not name: if not name:
Image._last_id += 1 Image._last_id += 1
name = "pyimage%r" % (Image._last_id,) # tk itself would use image<x> name = "pyimage%r" % (Image._last_id,) # tk itself would use image<x>
...@@ -3422,20 +3423,20 @@ class PhotoImage(Image): ...@@ -3422,20 +3423,20 @@ class PhotoImage(Image):
# XXX copy -from, -to, ...? # XXX copy -from, -to, ...?
def copy(self): def copy(self):
"""Return a new PhotoImage with the same image as this widget.""" """Return a new PhotoImage with the same image as this widget."""
destImage = PhotoImage() destImage = PhotoImage(master=self.tk)
self.tk.call(destImage, 'copy', self.name) self.tk.call(destImage, 'copy', self.name)
return destImage return destImage
def zoom(self,x,y=''): def zoom(self,x,y=''):
"""Return a new PhotoImage with the same image as this widget """Return a new PhotoImage with the same image as this widget
but zoom it with X and Y.""" but zoom it with X and Y."""
destImage = PhotoImage() destImage = PhotoImage(master=self.tk)
if y=='': y=x if y=='': y=x
self.tk.call(destImage, 'copy', self.name, '-zoom',x,y) self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
return destImage return destImage
def subsample(self,x,y=''): def subsample(self,x,y=''):
"""Return a new PhotoImage based on the same image as this widget """Return a new PhotoImage based on the same image as this widget
but use only every Xth or Yth pixel.""" but use only every Xth or Yth pixel."""
destImage = PhotoImage() destImage = PhotoImage(master=self.tk)
if y=='': y=x if y=='': y=x
self.tk.call(destImage, 'copy', self.name, '-subsample',x,y) self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
return destImage return destImage
......
...@@ -3,30 +3,43 @@ import tkinter ...@@ -3,30 +3,43 @@ import tkinter
import unittest import unittest
from test.support import requires from test.support import requires
def get_tk_root(): class AbstractTkTest:
requires('gui') # raise exception if tk unavailable
try:
root = tkinter._default_root
except AttributeError:
# it is possible to disable default root in Tkinter, although
# I haven't seen people doing it (but apparently someone did it
# here).
root = None
if root is None: @classmethod
# create a new master only if there isn't one already def setUpClass(cls):
root = tkinter.Tk() cls._old_support_default_root = tkinter._support_default_root
destroy_default_root()
tkinter.NoDefaultRoot()
cls.root = tkinter.Tk()
cls.wantobjects = cls.root.wantobjects()
# De-maximize main window.
# Some window managers can maximize new windows.
cls.root.wm_state('normal')
try:
cls.root.wm_attributes('-zoomed', False)
except tkinter.TclError:
pass
return root @classmethod
def tearDownClass(cls):
cls.root.destroy()
cls.root = None
tkinter._default_root = None
tkinter._support_default_root = cls._old_support_default_root
def root_deiconify(): def setUp(self):
root = get_tk_root() self.root.deiconify()
root.deiconify()
def root_withdraw(): def tearDown(self):
root = get_tk_root() for w in self.root.winfo_children():
root.withdraw() w.destroy()
self.root.withdraw()
def destroy_default_root():
if getattr(tkinter, '_default_root', None):
tkinter._default_root.update_idletasks()
tkinter._default_root.destroy()
tkinter._default_root = None
def simulate_mouse_click(widget, x, y): def simulate_mouse_click(widget, x, y):
"""Generate proper events to click at the x, y position (tries to act """Generate proper events to click at the x, y position (tries to act
......
...@@ -2,26 +2,20 @@ import unittest ...@@ -2,26 +2,20 @@ import unittest
import tkinter import tkinter
from tkinter import font from tkinter import font
from test.support import requires, run_unittest from test.support import requires, run_unittest
import tkinter.test.support as support from tkinter.test.support import AbstractTkTest
requires('gui') requires('gui')
class FontTest(unittest.TestCase): class FontTest(AbstractTkTest, unittest.TestCase):
def setUp(self):
support.root_deiconify()
def tearDown(self):
support.root_withdraw()
def test_font_eq(self): def test_font_eq(self):
fontname = "TkDefaultFont" fontname = "TkDefaultFont"
try: try:
f = font.Font(name=fontname, exists=True) f = font.Font(root=self.root, name=fontname, exists=True)
except tkinter._tkinter.TclError: except tkinter._tkinter.TclError:
f = font.Font(name=fontname, exists=False) f = font.Font(root=self.root, name=fontname, exists=False)
font1 = font.nametofont(fontname) font1 = font.Font(root=self.root, name=fontname, exists=True)
font2 = font.nametofont(fontname) font2 = font.Font(root=self.root, name=fontname, exists=True)
self.assertIsNot(font1, font2) self.assertIsNot(font1, font2)
self.assertEqual(font1, font2) self.assertEqual(font1, font2)
self.assertNotEqual(font1, font1.copy()) self.assertNotEqual(font1, font1.copy())
......
...@@ -278,7 +278,7 @@ class PlaceTest(AbstractWidgetTest, unittest.TestCase): ...@@ -278,7 +278,7 @@ class PlaceTest(AbstractWidgetTest, unittest.TestCase):
def create2(self): def create2(self):
t = tkinter.Toplevel(self.root, width=300, height=200, bd=0) t = tkinter.Toplevel(self.root, width=300, height=200, bd=0)
t.wm_geometry('+0+0') t.wm_geometry('300x200+0+0')
f = tkinter.Frame(t, width=154, height=84, bd=2, relief='raised') f = tkinter.Frame(t, width=154, height=84, bd=2, relief='raised')
f.place_configure(x=48, y=38) f.place_configure(x=48, y=38)
f2 = tkinter.Frame(t, width=30, height=60, bd=2, relief='raised') f2 = tkinter.Frame(t, width=30, height=60, bd=2, relief='raised')
...@@ -479,7 +479,6 @@ class PlaceTest(AbstractWidgetTest, unittest.TestCase): ...@@ -479,7 +479,6 @@ class PlaceTest(AbstractWidgetTest, unittest.TestCase):
class GridTest(AbstractWidgetTest, unittest.TestCase): class GridTest(AbstractWidgetTest, unittest.TestCase):
def tearDown(self): def tearDown(self):
super().tearDown()
cols, rows = self.root.grid_size() cols, rows = self.root.grid_size()
for i in range(cols + 1): for i in range(cols + 1):
self.root.grid_columnconfigure(i, weight=0, minsize=0, pad=0, uniform='') self.root.grid_columnconfigure(i, weight=0, minsize=0, pad=0, uniform='')
...@@ -488,10 +487,10 @@ class GridTest(AbstractWidgetTest, unittest.TestCase): ...@@ -488,10 +487,10 @@ class GridTest(AbstractWidgetTest, unittest.TestCase):
self.root.grid_propagate(1) self.root.grid_propagate(1)
if tcl_version >= (8, 5): if tcl_version >= (8, 5):
self.root.grid_anchor('nw') self.root.grid_anchor('nw')
super().tearDown()
def test_grid_configure(self): def test_grid_configure(self):
b = tkinter.Button(self.root) b = tkinter.Button(self.root)
self.addCleanup(b.destroy)
self.assertEqual(b.grid_info(), {}) self.assertEqual(b.grid_info(), {})
b.grid_configure() b.grid_configure()
self.assertEqual(b.grid_info()['in'], self.root) self.assertEqual(b.grid_info()['in'], self.root)
...@@ -578,7 +577,6 @@ class GridTest(AbstractWidgetTest, unittest.TestCase): ...@@ -578,7 +577,6 @@ class GridTest(AbstractWidgetTest, unittest.TestCase):
def test_grid_configure_row(self): def test_grid_configure_row(self):
b = tkinter.Button(self.root) b = tkinter.Button(self.root)
self.addCleanup(b.destroy)
with self.assertRaisesRegex(TclError, 'bad (row|grid) value "-1": ' with self.assertRaisesRegex(TclError, 'bad (row|grid) value "-1": '
'must be a non-negative integer'): 'must be a non-negative integer'):
b.grid_configure(row=-1) b.grid_configure(row=-1)
...@@ -795,7 +793,7 @@ class GridTest(AbstractWidgetTest, unittest.TestCase): ...@@ -795,7 +793,7 @@ class GridTest(AbstractWidgetTest, unittest.TestCase):
self.root.grid_bbox(0, 0, 0, 'x') self.root.grid_bbox(0, 0, 0, 'x')
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
self.root.grid_bbox(0, 0, 0, 0, 0) self.root.grid_bbox(0, 0, 0, 0, 0)
t = tkinter.Toplevel(self.root) t = self.root
# de-maximize # de-maximize
t.wm_geometry('1x1+0+0') t.wm_geometry('1x1+0+0')
t.wm_geometry('') t.wm_geometry('')
...@@ -823,7 +821,7 @@ class GridTest(AbstractWidgetTest, unittest.TestCase): ...@@ -823,7 +821,7 @@ class GridTest(AbstractWidgetTest, unittest.TestCase):
self.root.grid_location('x', 'y') self.root.grid_location('x', 'y')
with self.assertRaisesRegex(TclError, 'bad screen distance "y"'): with self.assertRaisesRegex(TclError, 'bad screen distance "y"'):
self.root.grid_location('1c', 'y') self.root.grid_location('1c', 'y')
t = tkinter.Toplevel(self.root) t = self.root
# de-maximize # de-maximize
t.wm_geometry('1x1+0+0') t.wm_geometry('1x1+0+0')
t.wm_geometry('') t.wm_geometry('')
......
import unittest import unittest
import tkinter import tkinter
from tkinter import ttk
from test import support from test import support
from tkinter.test.support import requires_tcl from tkinter.test.support import AbstractTkTest, requires_tcl
support.requires('gui') support.requires('gui')
class MiscTest(unittest.TestCase): class MiscTest(AbstractTkTest, unittest.TestCase):
def setUp(self):
self.root = ttk.setup_master()
def test_image_types(self): def test_image_types(self):
image_types = self.root.image_types() image_types = self.root.image_types()
...@@ -23,15 +19,13 @@ class MiscTest(unittest.TestCase): ...@@ -23,15 +19,13 @@ class MiscTest(unittest.TestCase):
self.assertIsInstance(image_names, tuple) self.assertIsInstance(image_names, tuple)
class BitmapImageTest(unittest.TestCase): class BitmapImageTest(AbstractTkTest, unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
AbstractTkTest.setUpClass.__func__(cls)
cls.testfile = support.findfile('python.xbm', subdir='imghdrdata') cls.testfile = support.findfile('python.xbm', subdir='imghdrdata')
def setUp(self):
self.root = ttk.setup_master()
def test_create_from_file(self): def test_create_from_file(self):
image = tkinter.BitmapImage('::img::test', master=self.root, image = tkinter.BitmapImage('::img::test', master=self.root,
foreground='yellow', background='blue', foreground='yellow', background='blue',
...@@ -107,16 +101,13 @@ class BitmapImageTest(unittest.TestCase): ...@@ -107,16 +101,13 @@ class BitmapImageTest(unittest.TestCase):
'-foreground {} {} #000000 yellow') '-foreground {} {} #000000 yellow')
class PhotoImageTest(unittest.TestCase): class PhotoImageTest(AbstractTkTest, unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
AbstractTkTest.setUpClass.__func__(cls)
cls.testfile = support.findfile('python.gif', subdir='imghdrdata') cls.testfile = support.findfile('python.gif', subdir='imghdrdata')
def setUp(self):
self.root = ttk.setup_master()
self.wantobjects = self.root.wantobjects()
def create(self): def create(self):
return tkinter.PhotoImage('::img::test', master=self.root, return tkinter.PhotoImage('::img::test', master=self.root,
file=self.testfile) file=self.testfile)
......
import unittest import unittest
import tkinter import tkinter
from tkinter import ttk
from test import support from test import support
from tkinter.test.support import AbstractTkTest
support.requires('gui') support.requires('gui')
class MiscTest(unittest.TestCase): class MiscTest(AbstractTkTest, unittest.TestCase):
def setUp(self):
self.root = ttk.setup_master()
def test_tk_setPalette(self): def test_tk_setPalette(self):
root = self.root root = self.root
......
import unittest import unittest
import tkinter import tkinter
from test.support import requires, run_unittest from test.support import requires, run_unittest
from tkinter.ttk import setup_master from tkinter.test.support import AbstractTkTest
requires('gui') requires('gui')
class TextTest(unittest.TestCase): class TextTest(AbstractTkTest, unittest.TestCase):
def setUp(self): def setUp(self):
self.root = setup_master() super().setUp()
self.text = tkinter.Text(self.root) self.text = tkinter.Text(self.root)
def tearDown(self):
self.text.destroy()
def test_debug(self): def test_debug(self):
text = self.text text = self.text
olddebug = text.debug() olddebug = text.debug()
......
import unittest import unittest
from tkinter import Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tk from tkinter import Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tcl
class Var(Variable): class Var(Variable):
...@@ -16,10 +16,10 @@ class Var(Variable): ...@@ -16,10 +16,10 @@ class Var(Variable):
class TestBase(unittest.TestCase): class TestBase(unittest.TestCase):
def setUp(self): def setUp(self):
self.root = Tk() self.root = Tcl()
def tearDown(self): def tearDown(self):
self.root.destroy() del self.root
class TestVariable(TestBase): class TestVariable(TestBase):
...@@ -81,7 +81,7 @@ class TestVariable(TestBase): ...@@ -81,7 +81,7 @@ class TestVariable(TestBase):
self.root.setvar(b'var\x00name', "value") self.root.setvar(b'var\x00name', "value")
def test_initialize(self): def test_initialize(self):
v = Var() v = Var(self.root)
self.assertFalse(v.side_effect) self.assertFalse(v.side_effect)
v.set("value") v.set("value")
self.assertTrue(v.side_effect) self.assertTrue(v.side_effect)
......
...@@ -66,7 +66,7 @@ class ToplevelTest(AbstractToplevelTest, unittest.TestCase): ...@@ -66,7 +66,7 @@ class ToplevelTest(AbstractToplevelTest, unittest.TestCase):
'takefocus', 'use', 'visual', 'width', 'takefocus', 'use', 'visual', 'width',
) )
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Toplevel(self.root, **kwargs) return tkinter.Toplevel(self.root, **kwargs)
def test_menu(self): def test_menu(self):
...@@ -105,7 +105,7 @@ class FrameTest(AbstractToplevelTest, unittest.TestCase): ...@@ -105,7 +105,7 @@ class FrameTest(AbstractToplevelTest, unittest.TestCase):
'relief', 'takefocus', 'visual', 'width', 'relief', 'takefocus', 'visual', 'width',
) )
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Frame(self.root, **kwargs) return tkinter.Frame(self.root, **kwargs)
...@@ -120,7 +120,7 @@ class LabelFrameTest(AbstractToplevelTest, unittest.TestCase): ...@@ -120,7 +120,7 @@ class LabelFrameTest(AbstractToplevelTest, unittest.TestCase):
'takefocus', 'text', 'visual', 'width', 'takefocus', 'text', 'visual', 'width',
) )
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.LabelFrame(self.root, **kwargs) return tkinter.LabelFrame(self.root, **kwargs)
def test_labelanchor(self): def test_labelanchor(self):
...@@ -158,7 +158,7 @@ class LabelTest(AbstractLabelTest, unittest.TestCase): ...@@ -158,7 +158,7 @@ class LabelTest(AbstractLabelTest, unittest.TestCase):
'underline', 'width', 'wraplength', 'underline', 'width', 'wraplength',
) )
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Label(self.root, **kwargs) return tkinter.Label(self.root, **kwargs)
...@@ -175,7 +175,7 @@ class ButtonTest(AbstractLabelTest, unittest.TestCase): ...@@ -175,7 +175,7 @@ class ButtonTest(AbstractLabelTest, unittest.TestCase):
'state', 'takefocus', 'text', 'textvariable', 'state', 'takefocus', 'text', 'textvariable',
'underline', 'width', 'wraplength') 'underline', 'width', 'wraplength')
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Button(self.root, **kwargs) return tkinter.Button(self.root, **kwargs)
def test_default(self): def test_default(self):
...@@ -199,7 +199,7 @@ class CheckbuttonTest(AbstractLabelTest, unittest.TestCase): ...@@ -199,7 +199,7 @@ class CheckbuttonTest(AbstractLabelTest, unittest.TestCase):
'underline', 'variable', 'width', 'wraplength', 'underline', 'variable', 'width', 'wraplength',
) )
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Checkbutton(self.root, **kwargs) return tkinter.Checkbutton(self.root, **kwargs)
...@@ -227,7 +227,7 @@ class RadiobuttonTest(AbstractLabelTest, unittest.TestCase): ...@@ -227,7 +227,7 @@ class RadiobuttonTest(AbstractLabelTest, unittest.TestCase):
'underline', 'value', 'variable', 'width', 'wraplength', 'underline', 'value', 'variable', 'width', 'wraplength',
) )
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Radiobutton(self.root, **kwargs) return tkinter.Radiobutton(self.root, **kwargs)
def test_value(self): def test_value(self):
...@@ -250,7 +250,7 @@ class MenubuttonTest(AbstractLabelTest, unittest.TestCase): ...@@ -250,7 +250,7 @@ class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
) )
_conv_pixels = staticmethod(pixels_round) _conv_pixels = staticmethod(pixels_round)
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Menubutton(self.root, **kwargs) return tkinter.Menubutton(self.root, **kwargs)
def test_direction(self): def test_direction(self):
...@@ -268,7 +268,7 @@ class MenubuttonTest(AbstractLabelTest, unittest.TestCase): ...@@ -268,7 +268,7 @@ class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
'crashes with Cocoa Tk (issue19733)') 'crashes with Cocoa Tk (issue19733)')
def test_image(self): def test_image(self):
widget = self.create() widget = self.create()
image = tkinter.PhotoImage('image1') image = tkinter.PhotoImage(master=self.root, name='image1')
self.checkParam(widget, 'image', image, conv=str) self.checkParam(widget, 'image', image, conv=str)
errmsg = 'image "spam" doesn\'t exist' errmsg = 'image "spam" doesn\'t exist'
with self.assertRaises(tkinter.TclError) as cm: with self.assertRaises(tkinter.TclError) as cm:
...@@ -303,7 +303,7 @@ class MenubuttonTest(AbstractLabelTest, unittest.TestCase): ...@@ -303,7 +303,7 @@ class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
class OptionMenuTest(MenubuttonTest, unittest.TestCase): class OptionMenuTest(MenubuttonTest, unittest.TestCase):
def _create(self, default='b', values=('a', 'b', 'c'), **kwargs): def create(self, default='b', values=('a', 'b', 'c'), **kwargs):
return tkinter.OptionMenu(self.root, None, default, *values, **kwargs) return tkinter.OptionMenu(self.root, None, default, *values, **kwargs)
...@@ -322,7 +322,7 @@ class EntryTest(AbstractWidgetTest, unittest.TestCase): ...@@ -322,7 +322,7 @@ class EntryTest(AbstractWidgetTest, unittest.TestCase):
'validate', 'validatecommand', 'width', 'xscrollcommand', 'validate', 'validatecommand', 'width', 'xscrollcommand',
) )
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Entry(self.root, **kwargs) return tkinter.Entry(self.root, **kwargs)
def test_disabledbackground(self): def test_disabledbackground(self):
...@@ -396,7 +396,7 @@ class SpinboxTest(EntryTest, unittest.TestCase): ...@@ -396,7 +396,7 @@ class SpinboxTest(EntryTest, unittest.TestCase):
'width', 'wrap', 'xscrollcommand', 'width', 'wrap', 'xscrollcommand',
) )
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Spinbox(self.root, **kwargs) return tkinter.Spinbox(self.root, **kwargs)
test_show = None test_show = None
...@@ -490,9 +490,9 @@ class TextTest(AbstractWidgetTest, unittest.TestCase): ...@@ -490,9 +490,9 @@ class TextTest(AbstractWidgetTest, unittest.TestCase):
'xscrollcommand', 'yscrollcommand', 'xscrollcommand', 'yscrollcommand',
) )
if tcl_version < (8, 5): if tcl_version < (8, 5):
wantobjects = False _stringify = True
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Text(self.root, **kwargs) return tkinter.Text(self.root, **kwargs)
def test_autoseparators(self): def test_autoseparators(self):
...@@ -644,9 +644,9 @@ class CanvasTest(AbstractWidgetTest, unittest.TestCase): ...@@ -644,9 +644,9 @@ class CanvasTest(AbstractWidgetTest, unittest.TestCase):
) )
_conv_pixels = round _conv_pixels = round
wantobjects = False _stringify = True
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Canvas(self.root, **kwargs) return tkinter.Canvas(self.root, **kwargs)
def test_closeenough(self): def test_closeenough(self):
...@@ -699,7 +699,7 @@ class ListboxTest(AbstractWidgetTest, unittest.TestCase): ...@@ -699,7 +699,7 @@ class ListboxTest(AbstractWidgetTest, unittest.TestCase):
'takefocus', 'width', 'xscrollcommand', 'yscrollcommand', 'takefocus', 'width', 'xscrollcommand', 'yscrollcommand',
) )
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Listbox(self.root, **kwargs) return tkinter.Listbox(self.root, **kwargs)
def test_activestyle(self): def test_activestyle(self):
...@@ -709,7 +709,7 @@ class ListboxTest(AbstractWidgetTest, unittest.TestCase): ...@@ -709,7 +709,7 @@ class ListboxTest(AbstractWidgetTest, unittest.TestCase):
def test_listvariable(self): def test_listvariable(self):
widget = self.create() widget = self.create()
var = tkinter.DoubleVar() var = tkinter.DoubleVar(self.root)
self.checkVariableParam(widget, 'listvariable', var) self.checkVariableParam(widget, 'listvariable', var)
def test_selectmode(self): def test_selectmode(self):
...@@ -831,7 +831,7 @@ class ScaleTest(AbstractWidgetTest, unittest.TestCase): ...@@ -831,7 +831,7 @@ class ScaleTest(AbstractWidgetTest, unittest.TestCase):
) )
default_orient = 'vertical' default_orient = 'vertical'
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Scale(self.root, **kwargs) return tkinter.Scale(self.root, **kwargs)
def test_bigincrement(self): def test_bigincrement(self):
...@@ -897,10 +897,10 @@ class ScrollbarTest(AbstractWidgetTest, unittest.TestCase): ...@@ -897,10 +897,10 @@ class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
'takefocus', 'troughcolor', 'width', 'takefocus', 'troughcolor', 'width',
) )
_conv_pixels = round _conv_pixels = round
wantobjects = False _stringify = True
default_orient = 'vertical' default_orient = 'vertical'
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Scrollbar(self.root, **kwargs) return tkinter.Scrollbar(self.root, **kwargs)
def test_activerelief(self): def test_activerelief(self):
...@@ -946,7 +946,7 @@ class PanedWindowTest(AbstractWidgetTest, unittest.TestCase): ...@@ -946,7 +946,7 @@ class PanedWindowTest(AbstractWidgetTest, unittest.TestCase):
) )
default_orient = 'horizontal' default_orient = 'horizontal'
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.PanedWindow(self.root, **kwargs) return tkinter.PanedWindow(self.root, **kwargs)
def test_handlepad(self): def test_handlepad(self):
...@@ -1104,7 +1104,7 @@ class MenuTest(AbstractWidgetTest, unittest.TestCase): ...@@ -1104,7 +1104,7 @@ class MenuTest(AbstractWidgetTest, unittest.TestCase):
) )
_conv_pixels = noconv _conv_pixels = noconv
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Menu(self.root, **kwargs) return tkinter.Menu(self.root, **kwargs)
def test_postcommand(self): def test_postcommand(self):
...@@ -1173,7 +1173,7 @@ class MessageTest(AbstractWidgetTest, unittest.TestCase): ...@@ -1173,7 +1173,7 @@ class MessageTest(AbstractWidgetTest, unittest.TestCase):
) )
_conv_pad_pixels = noconv _conv_pad_pixels = noconv
def _create(self, **kwargs): def create(self, **kwargs):
return tkinter.Message(self.root, **kwargs) return tkinter.Message(self.root, **kwargs)
def test_aspect(self): def test_aspect(self):
......
...@@ -2,15 +2,15 @@ import unittest ...@@ -2,15 +2,15 @@ import unittest
import tkinter import tkinter
from tkinter import ttk from tkinter import ttk
from test.support import requires, run_unittest from test.support import requires, run_unittest
from tkinter.test.support import AbstractTkTest
import tkinter.test.support as support
requires('gui') requires('gui')
class StyleTest(unittest.TestCase): class StyleTest(AbstractTkTest, unittest.TestCase):
def setUp(self): def setUp(self):
self.style = ttk.Style() super().setUp()
self.style = ttk.Style(self.root)
def test_configure(self): def test_configure(self):
...@@ -25,7 +25,7 @@ class StyleTest(unittest.TestCase): ...@@ -25,7 +25,7 @@ class StyleTest(unittest.TestCase):
style = self.style style = self.style
style.map('TButton', background=[('active', 'background', 'blue')]) style.map('TButton', background=[('active', 'background', 'blue')])
self.assertEqual(style.map('TButton', 'background'), self.assertEqual(style.map('TButton', 'background'),
[('active', 'background', 'blue')] if style.tk.wantobjects() else [('active', 'background', 'blue')] if self.wantobjects else
[('active background', 'blue')]) [('active background', 'blue')])
self.assertIsInstance(style.map('TButton'), dict) self.assertIsInstance(style.map('TButton'), dict)
......
This diff is collapsed.
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
import unittest import unittest
import sys import sys
import tkinter import tkinter
from tkinter.ttk import setup_master, Scale from tkinter.ttk import Scale
from tkinter.test.support import (tcl_version, requires_tcl, get_tk_patchlevel, from tkinter.test.support import (AbstractTkTest, tcl_version, requires_tcl,
pixels_conv, tcl_obj_eq) get_tk_patchlevel, pixels_conv, tcl_obj_eq)
import test.support import test.support
...@@ -22,33 +22,26 @@ if get_tk_patchlevel()[:3] == (8, 5, 11): ...@@ -22,33 +22,26 @@ if get_tk_patchlevel()[:3] == (8, 5, 11):
_sentinel = object() _sentinel = object()
class AbstractWidgetTest: class AbstractWidgetTest(AbstractTkTest):
_conv_pixels = staticmethod(pixels_round) _conv_pixels = staticmethod(pixels_round)
_conv_pad_pixels = None _conv_pad_pixels = None
wantobjects = True _stringify = False
def setUp(self): @property
self.root = setup_master() def scaling(self):
self.scaling = float(self.root.call('tk', 'scaling')) try:
if not self.root.wantobjects(): return self._scaling
self.wantobjects = False except AttributeError:
self._scaling = float(self.root.call('tk', 'scaling'))
def tearDown(self): return self._scaling
for w in self.root.winfo_children():
w.destroy()
def _str(self, value): def _str(self, value):
if self.wantobjects and tcl_version >= (8, 6): if not self._stringify and self.wantobjects and tcl_version >= (8, 6):
return value return value
if isinstance(value, tuple): if isinstance(value, tuple):
return ' '.join(map(self._str, value)) return ' '.join(map(self._str, value))
return str(value) return str(value)
def create(self, **kwargs):
widget = self._create(**kwargs)
self.addCleanup(widget.destroy)
return widget
def assertEqual2(self, actual, expected, msg=None, eq=object.__eq__): def assertEqual2(self, actual, expected, msg=None, eq=object.__eq__):
if eq(actual, expected): if eq(actual, expected):
return return
...@@ -61,7 +54,7 @@ class AbstractWidgetTest: ...@@ -61,7 +54,7 @@ class AbstractWidgetTest:
expected = value expected = value
if conv: if conv:
expected = conv(expected) expected = conv(expected)
if not self.wantobjects: if self._stringify or not self.wantobjects:
if isinstance(expected, tuple): if isinstance(expected, tuple):
expected = tkinter._join(expected) expected = tkinter._join(expected)
else: else:
...@@ -193,7 +186,7 @@ class AbstractWidgetTest: ...@@ -193,7 +186,7 @@ class AbstractWidgetTest:
errmsg=errmsg) errmsg=errmsg)
def checkImageParam(self, widget, name): def checkImageParam(self, widget, name):
image = tkinter.PhotoImage('image1') image = tkinter.PhotoImage(master=self.root, name='image1')
self.checkParam(widget, name, image, conv=str) self.checkParam(widget, name, image, conv=str)
self.checkInvalidParam(widget, name, 'spam', self.checkInvalidParam(widget, name, 'spam',
errmsg='image "spam" doesn\'t exist') errmsg='image "spam" doesn\'t exist')
...@@ -414,7 +407,7 @@ class StandardOptionsTests: ...@@ -414,7 +407,7 @@ class StandardOptionsTests:
def test_textvariable(self): def test_textvariable(self):
widget = self.create() widget = self.create()
var = tkinter.StringVar() var = tkinter.StringVar(self.root)
self.checkVariableParam(widget, 'textvariable', var) self.checkVariableParam(widget, 'textvariable', var)
def test_troughcolor(self): def test_troughcolor(self):
...@@ -475,7 +468,7 @@ class StandardOptionsTests: ...@@ -475,7 +468,7 @@ class StandardOptionsTests:
def test_variable(self): def test_variable(self):
widget = self.create() widget = self.create()
var = tkinter.DoubleVar() var = tkinter.DoubleVar(self.root)
self.checkVariableParam(widget, 'variable', var) self.checkVariableParam(widget, 'variable', var)
......
...@@ -27,7 +27,9 @@ Core and Builtins ...@@ -27,7 +27,9 @@ Core and Builtins
Library Library
------- -------
-- Issue #22191: Fix warnings.__all__. - Issue #22236: Fixed Tkinter images copying operations in NoDefaultRoot mode.
- Issue #22191: Fix warnings.__all__.
- Issue #15696: Add a __sizeof__ implementation for mmap objects on Windows. - Issue #15696: Add a __sizeof__ implementation for mmap objects on Windows.
...@@ -279,6 +281,9 @@ IDLE ...@@ -279,6 +281,9 @@ IDLE
Tests Tests
----- -----
- Issue #22236: Tkinter tests now don't reuse default root window. New root
window is created for every test class.
- Issue #20746: Fix test_pdb to run in refleak mode (-R). Patch by Xavier - Issue #20746: Fix test_pdb to run in refleak mode (-R). Patch by Xavier
de Gaye. de Gaye.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment