· 6 years ago · Mar 11, 2020, 05:36 PM
1import tkinter as tk
2from tkinter import ttk
3import requests
4from functools import partial
5
6TITLE_FONT = ("Arial", 16)
7BUTTON_FONT = ("Arial", 12)
8TEXT_FONT = ("Arial", 10)
9listRegion = [
10 "euw1",
11 "kr",
12 "br1",
13 "oc1",
14 "jp1",
15 "na1",
16 "ru",
17 "eun1",
18 "tr1",
19 "la1",
20 "la2",
21]
22
23class Program(tk.Tk):
24
25 def __init__(self, *args, **kwargs):
26
27 tk.Tk.__init__(self, *args, **kwargs)
28
29 tk.Tk.iconbitmap(self, default="lollogo.ico")
30 tk.Tk.wm_title(self, "LoL Stat Tracker")
31
32 container = tk.Frame(self)
33 container.pack(side="top", fill="both", expand = True)
34 container.grid_rowconfigure(0, weight=1)
35 container.grid_columnconfigure(0, weight=1)
36
37 self.frames = {}
38
39 for F in (StartPage, APIPage, SummonerPage, FunctionPage):
40
41 frame = F(container, self)
42
43 self.frames[F] = frame
44
45 frame.grid(row=0, column=0, sticky="nsew")
46
47 self.show_frame(StartPage)
48
49 def show_frame(self, cont):
50
51 frame = self.frames[cont]
52 frame.tkraise()
53
54class StartPage(tk.Frame):
55
56 def __init__(self, parent, controller):
57
58 tk.Frame.__init__(self, parent)
59
60 lblTitle = tk.Label(self, text="Welcome to the LoL Stat Tracker", font=TITLE_FONT)
61 lblTitle.grid(row=0, column=1, pady=10, padx=10)
62
63 lblExplanation = tk.Label(self, text="The aim of this program is to try and help you improve as a player, use the information given by the program to try and help you win more games.", font = TEXT_FONT)
64 lblExplanation.grid(row=1,column=1)
65
66 btnAPI = tk.Button(self, text="Change your API Key", font = BUTTON_FONT,
67 command=lambda: controller.show_frame(APIPage), height="4")
68 btnAPI.grid(row=2, column=0, padx=10, pady=10)
69
70 btnSummoner = tk.Button(self, text="Choose what player you are viewing", font = BUTTON_FONT,
71 command=lambda: controller.show_frame(SummonerPage), height="4")
72 btnSummoner.grid(row=2, column=1, padx=10, pady=10)
73
74 btnQuit = tk.Button(self, text="Quit", font=BUTTON_FONT,
75 command=quit, height="4", width="10")
76 btnQuit.grid(row=2, column=2)
77
78class APIPage(tk.Frame):
79
80 def __init__(self, parent, controller):
81
82 tk.Frame.__init__(self, parent)
83 lblTitle = tk.Label(self, text="API Key Page", font=TITLE_FONT)
84 lblTitle.grid(row=0, column=1)
85
86 lblAPIKey = tk.Label(self, text="API Key:", font=TEXT_FONT)
87 lblAPIKey.grid(row=1,column=0)
88
89 txtAPIKey = tk.Entry(self)
90 txtAPIKey.grid(row=1, column=1)
91
92 btnSubmit = ttk.Button(self, text="Submit",
93 command=lambda: entryAPI(lblConfirmation, txtAPIKey))
94 btnSubmit.grid(row=1, column=2)
95
96 lblConfirmation = tk.Label(self, text="Saved!", fg="lime green")
97 lblConfirmation.grid_forget()
98
99 btnReturn = ttk.Button(self, text="Return to home page",
100 command=lambda:controller.show_frame(StartPage))
101 btnReturn.grid(row=2, column=1)
102
103
104class SummonerPage(tk.Frame):
105
106 def __init__(self, parent, controller):
107
108 tk.Frame.__init__(self, parent)
109
110 lblTitle = tk.Label(self, text="Summoner Selection", font=TITLE_FONT)
111 lblTitle.grid(row=0, column=1)
112
113 lblExplanation = tk.Label(self, text="Please enter your summoner details below so we can locate your account.")
114 lblExplanation.grid(row=1, column=1)
115
116 lblName = tk.Label(self, text="Summoner Name:", font=TEXT_FONT)
117 lblName.grid(row=2, column=0)
118
119 lblConfirmation1 = tk.Label(self, text="This summoner can not be found in this region", font=TEXT_FONT, fg = "red2")
120 lblConfirmation1.grid_forget()
121
122 lblConfirmation2 = tk.Label(self, text="Summoner Found!", font=TEXT_FONT, fg = "lime green")
123 lblConfirmation2.grid_forget()
124
125 txtName = tk.Entry(self)
126 txtName.grid(row=2, column=1)
127
128 lblRegion = tk.Label(self, text="Region:", font=TEXT_FONT)
129 lblRegion.grid(row=3, column=0)
130
131 choiceRegion = tk.StringVar()
132 choiceRegion.set(listRegion[0])
133 cmbRegion = tk.OptionMenu(self, choiceRegion, *listRegion)
134 cmbRegion.config(width =90, font = TEXT_FONT)
135 cmbRegion.grid(row=3, column=1)
136
137 btnSubmit = tk.Button(self, text="Submit", font = TEXT_FONT,
138 command=lambda: entrySummoner(txtName, choiceRegion, lblConfirmation1, lblConfirmation2, btnContinue))
139 btnSubmit.grid(row=3, column=2)
140
141 btnReturn = ttk.Button(self, text="Return to home page",
142 command=lambda: controller.show_frame(StartPage))
143 btnReturn.grid(row=4, column =1)
144
145 btnContinue = tk.Button(self, text="Continue!", font=BUTTON_FONT, height = "4",
146 command=lambda: controller.show_frame(FunctionPage))
147 btnContinue.grid_forget()
148
149class FunctionPage(tk.Frame):
150
151 def __init__(self, parent, controller):
152
153 tk.Frame.__init__(self, parent)
154
155 lblTitle = tk.Label(self, text="What function of the program would you like to use?", font=TITLE_FONT)
156 lblTitle.grid(row=0, column=2)
157
158 lblAccount = tk.Label(self, text="Current Account:", font=BUTTON_FONT)
159 lblAccount.grid(row=1, column=0)
160
161 lblName = tk.Label(self, textvariable=summonerName, font=TEXT_FONT)
162 lblName.grid(row=2, column=0)
163
164 lblLevel = tk.Label(self, textvariable=summonerLevel, font=BUTTON_FONT)
165 lblLevel.grid(row=3, column=0)
166
167
168
169def entryAPI(lblConfirmation, txtAPIKey):
170 API = (txtAPIKey.get())
171 f = open("APIKey.txt", "w")
172 f.write(API)
173 f.close()
174 lblConfirmation.grid(row=1, column=3)
175 return
176
177def entrySummoner(txtName, choiceRegion, lblConfirmation1, lblConfirmation2, btnContinue):
178
179 summonerName = (txtName.get())
180 region = (choiceRegion.get())
181
182 f = open("APIKey.txt", "r")
183 API = f.read()
184 f.close()
185
186 responseJSON = requestSummonerData(region, summonerName, API)
187
188 try:
189 userID = str(responseJSON["id"])
190 accID = str(responseJSON["accountId"])
191 summonerLevel = str(responseJSON["summonerLevel"])
192 lblConfirmation2.grid(row=2, column=2)
193 btnContinue.grid(row=1, column=2)
194 return
195 except:
196 lblConfirmation1.grid(row=2, column=2)
197 return
198
199
200def requestSummonerData(region, summonerName, API):
201 URL = ("https://" + region + ".api.riotgames.com/lol/summoner/v4/summoners/by-name/" + summonerName + "?api_key=" + API)
202 response = requests.get(URL)
203 return response.json()
204
205
206app = Program()
207app.mainloop