1+ from __future__ import annotations
2+
13import tkinter as tk
24from typing import TypeVar
35from enum import Enum
79from .animate import Coord , Animater , Direction
810
911
10- class ViewType (Enum ):
11- IMAGE = 'IMAGE'
12- WIDGET = 'WIDGET'
13-
14-
15- T = TypeVar ('T' , ImageTk .PhotoImage , tk .Widget )
16-
17-
18- class View :
19-
20- def __init__ (self , data : T , viewtype : ViewType ):
21- self .data = data
22- if not isinstance (viewtype , ViewType ):
23- viewtype = ViewType (viewtype .upper ()) # Breaks if not string
24- self .viewtype = viewtype
25-
26- def __getattr__ (self , name ):
27- return getattr (self .data , name )
28-
29-
3012class Window (widget .PrimaryCanvas ):
3113 animation_speed = 4
3214 current = None
@@ -38,23 +20,8 @@ def init(self):
3820 def __coord (self , id ):
3921 return Coord (* self .coords (id )[:2 ])
4022
41- def __set_image (self , image : ImageTk .PhotoImage , coord : Coord ):
42- return self .create_image (
43- coord , image = image , anchor = 'nw'
44- )
45-
46- def __set_widget (self , widget : tk .Widget , coord : Coord ):
47- return self .create_window (
48- coord , window = widget , anchor = 'nw'
49- )
50-
5123 def __set (self , view : View , coord : Coord ):
52- if view .viewtype == ViewType .IMAGE :
53- wid = self .__set_image (view .data , coord )
54- elif view .viewtype == ViewType .WIDGET :
55- wid = self .__set_widget (view .data , coord )
56- else :
57- raise NotImplementedError
24+ wid = view .draw (coord , anchor = 'nw' )
5825 self .views [view ] = wid
5926 return wid
6027
@@ -115,3 +82,28 @@ def active(self):
11582 @property
11683 def origin (self ):
11784 return Coord (self .canvasx (0 ), self .canvasy (0 ))
85+
86+
87+ class DrawType (Enum ):
88+ IMAGE = 'create_image'
89+ WIDGET = 'create_window'
90+ TEXT = 'create_text'
91+
92+
93+ class View :
94+
95+ def __init__ (self , window : Window , ** kwds ):
96+ self .window = window
97+ self .kwds = kwds
98+ self .drawtype = self .data = None
99+ for k , v in self .kwds .items ():
100+ k = k .upper ()
101+ if hasattr (DrawType , k ):
102+ self .drawtype = DrawType [k ]
103+ self .data = v
104+ if self .drawtype is None :
105+ raise NotImplementedError
106+
107+ def draw (self , * args , ** kwds ):
108+ fn = getattr (self .window , self .drawtype .value )
109+ return fn (* args , ** {** self .kwds , ** kwds })
0 commit comments