Skip to content
This repository was archived by the owner on Mar 31, 2020. It is now read-only.

Commit 6850a74

Browse files
committed
Changed View to auto-get drawtype
1 parent e6ff599 commit 6850a74

File tree

4 files changed

+44
-47
lines changed

4 files changed

+44
-47
lines changed

res/docs/questions.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"What is your least favorite color?": ["#0801d5", "#ee018d", "#03a130", "#db0013", "#fd9501"],
3-
2+
"What is your least favorite color?": ["#0801d5", "#ee018d", "#03a130", "#db0013", "#fd9501"]
43
}

src/main.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import configparser
22
import tkinter as tk
33
from contextlib import suppress
4-
from pygame import mixer
5-
64

75
from .front import Front
86
from .splash import Splash
@@ -20,14 +18,13 @@ def __init__(self, *args, **kwds):
2018
title = self.appconfig.pop('title')
2119
super().__init__(*args, **kwds)
2220
self.title = title
23-
mixer.init()
2421

2522
self.geometry = '400x500'
2623
self.minsize(400, 500)
2724
self.maxsize(400, 500)
2825

2926
self.splash = Splash(self)
30-
self.splash.pack()
27+
self.splash.pack(expand=True, fill='both')
3128
# self.front = Front(self)
3229
# self.front.pack(fill='both', expand=True)
3330

src/splash.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import tkinter as tk
22
import json
33

4-
from .view import Window
4+
from .view import Window, View
55
from . import widget, DOCS, IMAGES
66

77

@@ -29,10 +29,19 @@ def load(self, choices):
2929

3030
class Splash(widget.PrimaryFrame):
3131

32-
intro = (DOCS / 'intro.txt').read_text().replace('\n', ' ').split(' ')
32+
intro = (DOCS / 'intro.txt').read_text() # .replace('\n', ' ').split(' ')
33+
with (DOCS / 'questions.json').open() as fp:
34+
questions = json.load(fp)
3335

3436
def init(self):
3537
self.window = Window(self)
36-
Question(self)
37-
38-
38+
self.window.pack(expand=True, fill='both')
39+
40+
self.intro = View(
41+
self.window,
42+
text=self.intro,
43+
width=self.window.winfo_reqwidth(),
44+
font=('sys', 14),
45+
fill='white'
46+
)
47+
self.window.set_view(self.intro)

src/view.py

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import tkinter as tk
24
from typing import TypeVar
35
from enum import Enum
@@ -7,26 +9,6 @@
79
from .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-
3012
class 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

Comments
 (0)