Professional Documents
Culture Documents
import tkinter as tk
import pyowm
class WeatherInfo(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.wm_title('Weather')
self.temp = tk.StringVar(self,value='')
self.humid = tk.StringVar(self,value='')
self.status = tk.StringVar(self,value='')
self.sunrise = tk.StringVar(self,value='')
self.sunset = tk.StringVar(self,value='')
self.ask.pack(fill='both',expand='yes')
self.kw = tk.Entry(self.ask)
self.kw.pack(side = tk.RIGHT)
self.rb.pack()
self.owm = pyowm.OWM('*CENSORED*')
self.output.pack(fill='both',expand='yes')
tk.Label(self.output, textvariable=self.humid).pack()
tk.Label(self.output, textvariable=self.status).pack()
tk.Label(self.output, textvariable=self.sunrise).pack()
tk.Label(self.output, textvariable=self.sunset).pack()
tk.Label(self.output, textvariable=self.ld).pack()
tk.Label(self.output, textvariable=self.ln).pack()
button.pack(side=tk.BOTTOM)
def search(self):
obs = self.owm.weather_at_place(self.kw.get())
try:
w = obs.get_weather()
self.sunrise.set('Sunrise at
'+datetime.datetime.fromtimestamp(w.get_sunrise_time()).strftime('%H:%M:%S'))
self.sunset.set('Sunset at '+datetime.datetime.fromtimestamp(w.get_sunset_time()).strftime('%H:
%M:%S'))
except:
def _quit(self):
self.quit()
self.destroy()
app = WeatherInfo()
app.mainloop()
import json
def inputInt(prompt):
while True:
try:
answer = int(input(prompt))
except ValueError:
continue
break
return answer
def inputSomething(prompt):
answer = input(prompt)
return answer
# Open "data.txt" in write mode and write the data to it in JSON format.
def saveChanges(dataList):
f = open('data.txt', 'w')
json.dump(dataList, f, indent=4)
f.close()
# If the file does not exist or does not contain JSON data, set "data" to an empty list instead.
try:
f = open('data.txt', 'r')
data = json.load(f)
f.close()
data = []
# Print welcome message, then enter the endless loop which prompts the user for a choice.
while True:
if choice == 'a':
item = {}
data.append(item)
saveChanges(data)
#Loop through data and print items with search term in name
if search in item['name'].lower():
while True:
try:
except IndexError:
continue
break
while True:
try:
del data[inputInt(prompt)]
except IndexError:
continue
break
saveChanges(data)
print('Goodbye!')
break
else:
print('Invalid choice.')
GUI:
import tkinter
import tkinter.messagebox
import tkinter.font
import json
import random
class ProgramGUI:
def __init__(self):
self.main = tkinter.Tk()
self.main.title('Food Quiz!')
self.main.geometry('450x225')
self.main.configure(bg='#5c9ead')
#Load data from data.txt, show error message & terminate if file does not exist or is invalid
try:
self.data = json.load(self.__f)
self.__f.close()
self.main.destroy()
return
self.qsAsked = 0
self.qsCorrect = 0
self.name1 = tkinter.StringVar()
self.name2 = tkinter.StringVar()
self.componentLab = tkinter.StringVar()
#Create frames
self.nameBox.pack(pady=10)
self.questionBox.pack(pady=10)
self.buttonBox.pack(pady=20)
self.showQuestion()
tkinter.mainloop()
def showQuestion(self):
self.items = random.sample(self.data, 2)
self.component = random.choice(self.components)
#Update labels
self.name1.set(self.items[0].get('name'))
self.name2.set(self.items[1].get('name'))
self.componentLab.set(self.component)
self.qsAsked += 1
self.val1 = self.items[0].get(self.component)
self.val2 = self.items[1].get(self.component)
#Define left button check (left > right), right button check (right > left) and middle button check
(both values are within 10% of each other, assuming val1 is > 0)
self.buttons = {'left': self.val1 > self.val2, 'right': self.val2 > self.val1, 'middle': self.val1 * 10 / 11 <=
self.val2 <= self.val1 * 11 / 10}
if self.buttons.get(clickedButton):
self.qsCorrect +=1
else:
self.showQuestion()
gui = ProgramGUI()