· 6 years ago · Mar 12, 2020, 03:46 PM
1from tkinter import *
2import pyowm
3from tkinter import messagebox
4import keyboard
5
6owm = pyowm.OWM('Your API key')
7
8#this is what where the GUi is essentially created, the 3 code lines below all of them are not essential only the first line is
9root = Tk()
10root.title("Assistant")
11root.geometry("300x200")
12
13#this is the fnction I created that makes the window that displays the temperature and such
14def check_weather():
15 ne = owm.weather_at_place('city, country')
16 neg = owm.three_hours_forecast('city, country')
17 weather = ne.get_weather()
18 temperature = weather.get_temperature('celsius')['temp']
19 rain = neg.will_have_rain()
20
21 weatherr = Tk()
22 weatherr.title("Weather Assistant")
23 weatherr.geometry("300x200")
24 temperature_label = Label(weatherr, text=f"Temperature: {temperature}")
25 rain_label = Label(weatherr, text=f"Will it rain: {rain}")
26 weather_label = Label(weatherr, text="Weather in your region:", padx=30, pady=20)
27
28 weather_label.pack()
29 temperature_label.pack()
30 rain_label.pack()
31
32#this function checks if I entered the correct password
33def login():
34 with open('password.txt', 'r') as f:
35 data = f.readlines()
36 pword = data[0].rstrip()
37 if password_entry.get() != pword:
38 messagebox.showinfo("Error", "Incorrect Password!")
39 #incorrect = Label(root, text="Incorrect password!")
40 #incorrect.pack()
41 if password_entry.get() == pword:
42 root.destroy()
43 root2 = Tk()
44 root2.title("Assistant")
45 root2.geometry("300x200")
46
47 loggedin = Label(root2, text="Logged in as Alexander!")
48 weather_button = Button(root2, text="Check weather", command=check_weather)
49 loggedin.pack()
50 weather_button.pack()
51
52 root2.mainloop()
53
54text = Label(root, text="Please enter password")
55loginButton = Button(root, text="Confirm Password", command=login)
56password_entry = Entry(root)
57
58if keyboard.is_pressed('e'):
59 root.destroy()
60
61#this is how you actually display things on the window! you can use for example text.grid(row=0, column=0) but I used .pack() which is better in my oppinion to be honest!
62
63text.pack()
64password_entry.pack()
65loginButton.pack()
66password_entry.focus()
67#password_entry.grid(row=1, column=0, columnspan=3)
68#loginButton.grid(row=2, column=2)
69#text.grid(row=0, column=0)
70
71#this is what keeps the code running and looping until you close the window, same goes for the other 2 windows above
72
73root.mainloop()