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

Commit 5f5c2d9

Browse files
committed
Image resize for cleaner animation
1 parent 3344136 commit 5f5c2d9

File tree

4 files changed

+8
-253
lines changed

4 files changed

+8
-253
lines changed

src/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async def refill(self):
7474
image_bytes = await res.read()
7575
# open and the image in pillow
7676
im = Image.open(io.BytesIO(image_bytes))
77-
im = im.resize((400, 440), Image.NEAREST)
77+
im = im.resize((400, 280), Image.NEAREST)
7878
# make the image a tkinter image
7979
image = ImageTk.PhotoImage(im)
8080
# update the cat data dict

src/front.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def __make_item(self, name, value):
108108
return item
109109

110110
def load(self, data: dict):
111-
print(data)
111+
# print(data)
112112
for name, val in data.items():
113113
item = self.__make_item(name, val)
114114
item.pack()

src/main.py

Lines changed: 1 addition & 248 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import configparser
22
import tkinter as tk
3-
import asyncio
43
from pygame import mixer
5-
from .cache import Cache
6-
from .animate import Animater, Coord
7-
from .front import Front
84

5+
from .front import Front
96
from . import SETTINGS
107

118

@@ -29,247 +26,3 @@ def __init__(self, *args, **kwds):
2926
self.front = Front(self)
3027

3128
self.front.pack(fill='both', expand=True)
32-
33-
34-
class Tinder:
35-
'''The main class for the application.'''
36-
37-
def __init__(self):
38-
# setup for pygame mixer
39-
mixer.init()
40-
41-
# get settings
42-
cp = configparser.ConfigParser()
43-
cp.read(SETTINGS)
44-
45-
# for now, let's just look up the DEV settings
46-
# can change this later
47-
# configparser will use values from DEFAULT section if none provided elsewhere
48-
if 'DEV' in cp.sections():
49-
self.config = cp['DEV']
50-
else:
51-
self.config = cp['DEFAULT']
52-
53-
# setting up the tkinter root
54-
self.root = tk.Tk()
55-
self.root.title(self.config['main.title'])
56-
self.root.geometry(self.config['main.geometry'])
57-
self.root.minsize(1000, 1000)
58-
self.root.maxsize(1000, 1000)
59-
self.root.configure(background=self.config['main.background'])
60-
# self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
61-
62-
# getting screen width and height for use with teleporting window/jumpscare
63-
self.screen_x = self.root.winfo_screenwidth()
64-
self.screen_y = self.root.winfo_screenheight()
65-
66-
# setting class variables to be used later
67-
self.jumpscare = False
68-
self.loop = asyncio.get_event_loop()
69-
self.cache = Cache(self.root)
70-
self.window = Animater(self.root)
71-
72-
def start(self):
73-
'''Starts the Tinder application'''
74-
75-
# getting a cache of cat inf
76-
# starting the program loop
77-
self.new_image()
78-
79-
def all_children(self):
80-
'''Used to get all children of the root window
81-
82-
Returns
83-
-------
84-
A `list` of tkinter objects that are children of the root window'''
85-
86-
# get children
87-
children = self.root.winfo_children()
88-
# loop through the children
89-
for item in children:
90-
# if the child has children, add them to the list
91-
if item.winfo_children():
92-
children.extend(item.winfo_children())
93-
# return the full list of children
94-
return children
95-
96-
def new_image(self, cat=None):
97-
'''Main functionality of the application
98-
99-
param
100-
cat: dict -- if you want to run the function without getting a new cat'''
101-
102-
# if the previous image was a jumpscare, resize the window and reset the variable
103-
if self.jumpscare:
104-
self.root.maxsize(400, 500)
105-
self.jumpscare = False
106-
107-
# get all children of the root window
108-
widget_list = self.all_children()
109-
for item in widget_list:
110-
# forget packs all of the children (This clears the window)
111-
item.pack_forget()
112-
113-
# make a new Frame
114-
self.frame = tk.Frame(self.window, bg="black")
115-
116-
# if a dict wasn't passed to the function, get a dict from self.cats
117-
if not cat:
118-
try:
119-
cat = self.cache.cats.pop(0)
120-
except IndexError:
121-
# the cache is empty so refill it
122-
self.loop.run_until_complete(self.cache.refill())
123-
cat = self.cache.cats.pop(0)
124-
125-
# getting base cat variables from the dict
126-
image = cat["image"]
127-
jumpscare = cat["jumpscare"]
128-
129-
# if the image is not a jumpscare, add a Text widget with the cat name
130-
if not jumpscare:
131-
# since the image is not a jumpscare, get regular cat variables
132-
cat_name = cat["name"]
133-
gender = cat["gender"].capitalize()
134-
hobbies = cat["hobbies"]
135-
age = cat["age"]
136-
location = cat["location"]
137-
138-
# make the Text widget
139-
name = tk.Text(self.frame, width=40, height=1)
140-
# tag to make all text in the widget centered
141-
name.tag_configure("center", justify=tk.CENTER)
142-
# insert the cat name into the widget
143-
name.insert("1.0", cat_name)
144-
# add the centered tag to all text in the widget
145-
name.tag_add("center", "1.0", tk.END)
146-
# disable the widget so the user can't type in it
147-
name.configure(state="disabled")
148-
# pack the widget on the top of the frame
149-
name.pack(side=tk.TOP)
150-
151-
# make a Label widget with the cat/jumpscare image and pack it
152-
tk.Label(self.frame, image=image).pack(side=tk.TOP)
153-
154-
# the image is a jumpscare, so do jumpscare things
155-
if jumpscare:
156-
# remember that this image is a jumpscare
157-
self.jumpscare = True
158-
159-
# allow the root window to get bigger
160-
self.root.maxsize(self.screen_x, self.screen_y)
161-
# make the root window bigger (makes jumpscare image scarier)
162-
self.root.geometry(f"{self.screen_x}x{self.screen_y}+0+0")
163-
164-
# play a jumpscare sound
165-
soundpath = SOUNDS / "jumpscare.mp3"
166-
mixer.music.load(str(soundpath))
167-
mixer.music.play()
168-
169-
# make a button to allow the user to pass through the image
170-
# Note: since everyone likes scary monsters, only make a Like button
171-
tk.Button(
172-
self.frame, text=self.config['like.text'], background="green",
173-
command=self.new_image).pack(side=tk.BOTTOM)
174-
175-
# image was not a jumpscare, don't do jumpscare things
176-
else:
177-
# setting up like and dislike buttons on opposite sides of the screen
178-
tk.Button(
179-
self.frame, text=self.config['like.text'], background="green",
180-
command=self.new_image).pack(side=tk.RIGHT)
181-
tk.Button(
182-
self.frame, text=self.config['dislike.text'], background="red",
183-
command=self.new_image).pack(side=tk.LEFT)
184-
185-
# defining button functions
186-
def back_to_photo():
187-
'''Resets the window with the same cat for when the user
188-
goes to bio and clicks back'''
189-
190-
# calls the new image function and passes the current cat dict
191-
self.window.clear()
192-
self.window.add_motion(self.bioid, (Coord(0, 500),), speed=3)
193-
self.window.start()
194-
self.new_image(cat)
195-
196-
def get_bio():
197-
'''Creates the Bio Widget for the current cat'''
198-
199-
# get all children of the root window
200-
widget_list = self.all_children()
201-
for item in widget_list:
202-
# forget packs all of the children (This clears the window)
203-
item.pack_forget()
204-
205-
# make a new Frame for the bio
206-
self.window = Animater(self.root)
207-
self.frame = tk.Frame(self.window, bg="black", height=450, width=400)
208-
209-
# makes a Text widget on the Frame
210-
bio = tk.Text(self.frame)
211-
212-
# inserting all of the Bio to the text widget
213-
bio.insert(tk.END, f"Name: {cat_name} \n")
214-
bio.insert(tk.END, f"Age: {age} \n")
215-
bio.insert(tk.END, f"Gender: {gender} \n")
216-
bio.insert(tk.END, f"Location: {location} \n")
217-
bio.insert(tk.END, f"{hobbies} \n")
218-
# disabling the widget so users can't edit it
219-
bio.configure(state="disabled")
220-
# packing the bio
221-
bio.pack(side=tk.TOP)
222-
223-
# setting up like/dislike/Back to Photo buttons on the bio screen
224-
tk.Button(
225-
self.frame, text=self.config['like.text'],
226-
background=self.config['like.background'],
227-
command=self.new_image).pack(side=tk.RIGHT)
228-
tk.Button(
229-
self.frame, text=self.config['dislike.text'],
230-
background=self.config['dislike.background'],
231-
command=self.new_image).pack(side=tk.LEFT)
232-
tk.Button(
233-
self.root, text=self.config['back.text'],
234-
background=self.config['back.background'],
235-
command=back_to_photo).pack(side=tk.BOTTOM)
236-
237-
# packing the frame
238-
self.bioid = self.window.create_window((0, 0), window=self.frame, anchor='nw')
239-
end = Coord(0, 0)
240-
self.window.add_motion(self.bioid, (end,), speed=3)
241-
self.window.pack(fill='both', expand=True)
242-
self.window.start()
243-
244-
# making and packing the Bio button for users to look at the cat's bio
245-
tk.Button(
246-
self.frame, text=self.config['bio.text'], background=self.config['bio.background'],
247-
command=get_bio).pack(side=tk.BOTTOM)
248-
249-
# packing the frame
250-
self.window.create_window((0, 0))
251-
self.frame.pack()
252-
253-
# starting the main tkinter loop
254-
self.root.mainloop()
255-
256-
def on_closing(self):
257-
'''Teleports the window if the user tries to close the app using the red X'''
258-
259-
# checks if the image is a jumpscare (if so, can't teleport the
260-
# window because it takes up the entire screen)
261-
return
262-
if not self.jumpscare:
263-
# get the max x and y values that the window can teleport
264-
# to without going off the screen
265-
max_x, max_y = self.screen_x - 400, self.screen_y - 500
266-
# getting the random x and y values to teleport to
267-
x, y = randint(0, max_x), randint(0, max_y)
268-
# moving the window to those x and y coordinates
269-
self.root.geometry(f"+{x}+{y}")
270-
271-
272-
# checks if this file is the main file being run
273-
if __name__ == "__main__":
274-
# start the application
275-
Tinder().start()

src/widget.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def __init__(self, *args, **kwds):
6464

6565
class PrimaryButton(tk.Button):
6666
DEFAULT = {
67-
'height': 2,
68-
'width': 10
67+
'height': 3,
68+
'width': 15
6969
}
7070

7171
def __init__(self, *args, **kwds):
@@ -77,7 +77,9 @@ def __init__(self, *args, **kwds):
7777

7878
class PrimaryLabel(tk.Label):
7979
DEFAULT = {
80-
'font': ('Courier', 17)
80+
'font': ('Courier', 25),
81+
'bg': 'black',
82+
'fg': 'gray'
8183
}
8284

8385
def __init__(self, *args, **kwds):

0 commit comments

Comments
 (0)