def test_colors(): # these must work teek.Color(1, 2, 255) teek.Color(1, 2, 0) with pytest.raises(ValueError): teek.Color(1, 2, 256) with pytest.raises(ValueError): teek.Color(1, 2, -1) with pytest.raises(TypeError): teek.Color(1, 2, 3, 4) with pytest.raises(TypeError): teek.Color() blue1 = teek.Color(0, 0, 255) blue2 = teek.Color('blue') white = teek.Color('white') assert repr(blue1).startswith("<Color '#0000ff': ") assert repr(blue2).startswith("<Color 'blue': ") assert blue1 == blue2 assert blue1 != 'Woot Woot!' assert hash(blue1) == hash(blue2) assert hash(blue1) != hash(white) the_dict = {blue1: 'hi'} assert the_dict[blue1] == 'hi' assert the_dict[blue2] == 'hi' # __hash__ works correctly with pytest.raises(KeyError): the_dict[white]
def test_item_config_usage(): canvas = teek.Canvas(teek.Window()) rect = canvas.create_rectangle(100, 100, 200, 200, dash='-') assert rect.config['dash'] == '-' assert rect.config['fill'] is None rect.config['fill'] = 'blue' assert rect.config['fill'] == teek.Color('blue')
def test_color(fake_dialog_command): with fake_dialog_command('tk_chooseColor', {}, '#ffffff'): assert teek.dialog.color() == teek.Color('white') window = teek.Window() with fake_dialog_command( 'tk_chooseColor', { '-title': 'toot', '-initialcolor': teek.Color('maroon').to_tcl(), '-parent': window.toplevel.to_tcl() }, '#ffffff'): assert teek.dialog.color( initialcolor=teek.Color('maroon'), parent=window, title='toot', ) == teek.Color('white') with fake_dialog_command('tk_chooseColor', {}, ''): assert teek.dialog.color() is None
def test_menuitem_objects(): item = teek.MenuItem("Click me", print, activeforeground=teek.Color(1, 2, 3)) assert repr(item) == ("<MenuItem('Click me', <built-in function print>): " "type='command', not added to a menu yet>") menu = teek.Menu([item]) assert repr(item) == ("<MenuItem('Click me', <built-in function print>): " "type='command', added to a menu>") # the menu should cache the item assert menu[0] is item assert menu[0] is menu[0]
import teek window = teek.Window("Text Widget Demo") text = teek.Text(window) text.pack(fill='both', expand=True) text.insert(text.start, "hello world") hello_tag = text.get_tag('hello_tag') hello_tag['foreground'] = teek.Color('red') hello_tag.add(text.start, text.start.forward(chars=5)) world_tag = text.get_tag('world_tag') world_tag['foreground'] = teek.Color('green') world_tag.add(text.end.back(chars=5), text.end) # move cursor after hello text.marks['insert'] = text.start.forward(chars=5) window.on_delete_window.connect(teek.quit) teek.run()
def test_tags(): text = teek.Text(teek.Window()) text.insert(text.start, "asd toot boo") assert {tag.name for tag in text.get_all_tags()} == {'sel'} assert text.get_tag('asd').name == 'asd' # do any tag Tcl call that ensures the asd tag exists text.get_tag('asd')['foreground'] assert {tag.name for tag in text.get_all_tags()} == {'sel', 'asd'} for tag in [text.get_tag('asd'), text.get_tag('sel')]: assert tag is text.get_tag(tag.name) # returns same tag obj every time tag.add((1, 4), (1, 8)) assert tag.ranges() == [((1, 4), (1, 8))] flatten = itertools.chain.from_iterable assert all( isinstance(index, type(text.start)) for index in flatten(tag.ranges())) assert tag.nextrange((1, 0)) == ((1, 4), (1, 8)) assert tag.nextrange((1, 0), (1, 4)) is None for index in tag.nextrange((1, 0)): assert isinstance(index, type(text.start)) tag.remove() assert tag.ranges() == [] tag.add((1, 4), (1, 8)) tag.remove((0, 0), (100, 200)) assert tag.ranges() == [] # all tags must have the same options option_frozensets = set() for tag in text.get_all_tags(): option_frozensets.add(frozenset(tag.keys())) assert len(option_frozensets) == 1 # they are unique # because nothing else covers this assert len(text.get_tag('asd')) == len(list(option_frozensets)[0]) toot = text.get_tag('toot') toot.add((1, 4), text.end) assert toot.ranges() != [] toot.delete() assert toot not in text.get_all_tags() assert toot.ranges() == [] assert toot not in text.get_all_tags() # if it's set to a string, it must still be a Color object when getting toot['foreground'] = 'black' assert toot in text.get_all_tags() assert isinstance(toot['foreground'], teek.Color) assert toot['foreground'] == teek.Color(0, 0, 0) toot['foreground'] = teek.Color('blue') assert toot['foreground'] == teek.Color('blue') # misc other tag properties assert toot == toot assert toot != teek.Text(teek.Window()).get_tag('toot') # different widget assert toot != 123 assert hash(toot) == hash(toot) assert repr(toot) == "<Text widget tag 'toot'>" with pytest.raises(TypeError): del toot['foreground'] tag_names = {'sel', 'asd', 'toot'} for tag_name in tag_names: text.get_tag(tag_name).add((1, 4), (1, 8)) assert {tag.name for tag in text.get_all_tags((1, 6))} == tag_names