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

Commit 6eb0074

Browse files
author
Michael Wall
committed
First cut at pulling out settings into a file
1 parent 06d498d commit 6eb0074

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

src/mainwindow.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import configparser
12
import tkinter as tk
23
from PIL import Image, ImageTk
34
import io
@@ -18,13 +19,25 @@ def __init__(self):
1819
# getting the directory folder for use later when opening files
1920
self.dir = os.path.dirname(os.path.realpath(__file__))
2021

22+
# get settings
23+
cp = configparser.ConfigParser()
24+
cp.read('settings.ini')
25+
26+
# for now, let's just look up the DEV settings
27+
# can change this later
28+
# configparser will use values from DEFAULT section if none provided elsewhere
29+
if 'DEV' in cp.sections():
30+
self.config = cp['DEV']
31+
else:
32+
self.config = cp['DEFAULT']
33+
2134
# setting up the tkinter root
2235
self.root = tk.Tk()
23-
self.root.title("Cat Tinder")
24-
self.root.geometry("400x500")
36+
self.root.title(self.config['main.title'])
37+
self.root.geometry(self.config['main.geometry'])
2538
self.root.minsize(400, 500)
2639
self.root.maxsize(400, 500)
27-
self.root.configure(background='black')
40+
self.root.configure(background=self.config['main.background'])
2841
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
2942

3043
# getting screen width and height for use with teleporting window/jumpscare
@@ -37,6 +50,8 @@ def __init__(self):
3750
self.session = None
3851
self.cats = list()
3952

53+
self.frame = tk.Frame()
54+
4055
def start(self):
4156
'''Starts the Tinder application'''
4257

@@ -53,15 +68,16 @@ async def get_cache(self):
5368
if not self.session:
5469
self.session = aiohttp.ClientSession()
5570

71+
cachesize = int(self.config['cachesize'])
5672
# Run 10 times to get 10 cats
57-
for i in range(10):
73+
for i in range(cachesize):
5874
# initialize a dict of cat data
5975
cat_data = dict()
6076

6177
# randomly make jumpscares happen, but not on the first image
62-
if randint(1, 10) == 5 and i:
78+
if randint(1, cachesize) == 5 and i:
6379
# get a random number for an image
64-
image_number = randint(1, 10)
80+
image_number = randint(1, cachesize)
6581
# open and resize the image using Pillow
6682
im = Image.open(os.path.join(self.dir, os.path.join("res",
6783
os.path.join("images", f"{image_number}.jpg"))))
@@ -76,7 +92,7 @@ async def get_cache(self):
7692
cat_data.update({"jumpscare": False})
7793

7894
# get a url from The Cat API
79-
async with self.session.get('https://api.thecatapi.com/v1/images/search') as res:
95+
async with self.session.get(self.config['catapi']) as res:
8096
data = await res.json()
8197
url = data[0]['url']
8298
# get image data from that url
@@ -91,8 +107,7 @@ async def get_cache(self):
91107
cat_data.update({"image": image})
92108

93109
# get a random name
94-
async with self.session.get(
95-
"https://www.pawclub.com.au/assets/js/namesTemp.json") as res:
110+
async with self.session.get(self.config['namefile']) as res:
96111
data = await res.json()
97112
# get a random letter for the name
98113
# Note: website doesn't have any b names which is why it is left out here
@@ -104,11 +119,7 @@ async def get_cache(self):
104119
cat_data.update({"gender": cat["gender"]})
105120

106121
# get 5 random hobbies
107-
async with self.session.get(
108-
"https://gist.githubusercontent.com/mbejda/" +
109-
"453fdb77ef8d4d3b3a67/raw/e8334f09109dc212892406e25fdee03efdc23f56/" +
110-
"hobbies.txt"
111-
) as res:
122+
async with self.session.get(self.config['hobbiesfile']) as res:
112123
text = await res.text()
113124
# split the raw text of hobbies into a list
114125
all_hobbies = text.split("\n")
@@ -276,21 +287,21 @@ def get_bio():
276287

277288
# setting up like/dislike/Back to Photo buttons on the bio screen
278289
tk.Button(
279-
self.frame, text="Like", background="green",
290+
self.frame, text=self.config['like.text'], background=self.config['like.background'],
280291
command=self.new_image).pack(side=tk.RIGHT)
281292
tk.Button(
282-
self.frame, text="Dislike", background="red",
293+
self.frame, text=self.config['dislike.text'], background=self.config['dislike.background'],
283294
command=self.new_image).pack(side=tk.LEFT)
284295
tk.Button(
285-
self.root, text="Back To Photo", background="blue",
296+
self.root, text=self.config['back.text'], background=self.config['back.background'],
286297
command=back_to_photo).pack(side=tk.BOTTOM)
287298

288299
# packing the frame
289300
self.frame.pack()
290301

291302
# making and packing the Bio button for users to look at the cat's bio
292303
tk.Button(
293-
self.frame, text="Bio", background="blue",
304+
self.frame, text=self.config['bio.text'], background=self.config['bio.background'],
294305
command=get_bio).pack(side=tk.BOTTOM)
295306

296307
# packing the frame

src/settings.ini

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[DEFAULT]
2+
main.title = Cat Tinder
3+
main.geometry = 400x500
4+
main.background = black
5+
cachesize = 10
6+
catapi = https://api.thecatapi.com/v1/images/search
7+
namefile = https://www.pawclub.com.au/assets/js/namesTemp.json
8+
hobbiesfile = https://gist.githubusercontent.com/mbejda/453fdb77ef8d4d3b3a67/raw/e8334f09109dc212892406e25fdee03efdc23f56/hobbies.txt
9+
like.text = Like
10+
like.background = red
11+
dislike.text = Dislike
12+
dislike.background = red
13+
back.text = Back to Photo
14+
back.background = blue
15+
bio.text = Bio
16+
bio.background = blue
17+
18+
[DEV]
19+
main.title = DEV MODE CAT TINDER

0 commit comments

Comments
 (0)