Python Forum
[PyQt] Increase text size and change color based on temp
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Increase text size and change color based on temp
#1
I have a weather app I want to do something with. I've adapted it from somehwere else and would like to adapt it further, but I am not sure how to adapt it.

Here are the two things I would like to accomplish:
1. Make the temperature and condition font larger.
2. Make the background change color based on the temperature.

Here is what I have so far:
import requests
from tkinter import *
import io
from PIL import ImageTk, Image
import urlopen

root=Tk()

root.title("Weather Widget")
root.resizable(0,0)
root.wm_attributes('-topmost',1)
root.geometry("375x150")

#main area display
info = StringVar()

weather = Message(root, textvariable = info, width = 200)   
info.set("What's your weather?")            
weather.grid(row=1, column=1)

#image_address = 'http://openweathermap.org/img/w/13n.png'
#image_open = urlopen(image_address)
#image_file = io.BytesIO(image_open.read())
#image_png = Image.open(image_file)
#image_tk = ImageTk.PhotoImage(image_png.convert("RGB"))
#label_img = Label(root,image=image_tk)
#label_img.grid(row=1, column=2)


#function that gets weather
def getWeather():
    #Replace YOURAPIKEYHERE with the API key you obtain by signing up at https://openweathermap.org/
    api_address = 'http://api.openweathermap.org/data/2.5/weather?appid=ee23153f55519f1ee97a23899dfcda0c&units=imperial&zip='
    country_code = ',us'
    zip_code = e.get()
    
    url = api_address + zip_code + country_code
    json_data = requests.get(url).json()
    
    if 'message' in json_data:
        info.set('Invalid zip code.')
    
    else:
        city = json_data['name']
        #description = json_data['weather'][0]['description']
        #icon = json_data['weather'][0]['icon']
        temperature = str(json_data['main']['temp']) + u'\u00b0' + " F"
    
        info.set(city + "\n" + temperature)
        
        # now prep image
#        image_main = 'http://openweathermap.org/img/w/'
#       image_address = image_main + str(icon) + '.png'
#        image_open = urlopen(image_address)
#        image_file = io.BytesIO(image_open.read())
#        image_png = Image.open(image_file)
#        image_tk2 = ImageTk.PhotoImage(image_png.convert("RGB")) 
#        label_img.configure(image=image_tk2)    
#        label_img.image = image_tk2 


label=Label(root,text='Enter your zip code:  ')
label.grid(row=3, column=1, sticky='E')

e=Entry(root)
e.grid(row=3, column=2)

sub = Button(root,text='Get Weather', command = getWeather)
sub.grid(row=4, column=2)

root.mainloop()
Reply
#2
Add this in your getWeather function. Then use condition to change.

weather['fg'] = 'red'
weather['font'] = 'sans 16 normal'
weather['bg'] = 'pink'
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Thank you. SO far so good. Now I just want to learn how to change the color in the little box based on the temperature. I know what colors I want. I just want to know how to do it so that I can do it myself.

Can someone provide an example of that?
Reply
#4
Rough example and not tested.
if temp <= 32:
    weather['bg'] = 'blue'
elif temp >= 65 and temp <= 80:
    weather['bg'] = 'green'
else:
    weather['bg'] = 'brown'
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
This is what I came up with, but it didn't work.

def getWeather():
    #Replace YOURAPIKEYHERE with the API key you obtain by signing up at https://openweathermap.org/
    api_address = 'http://api.openweathermap.org/data/2.5/weather?appid=ee23153f55519f1ee97a23899dfcda0c&units=imperial&zip='
    country_code = ',us'
    zip_code = e.get()
    
    url = api_address + zip_code + country_code
    json_data = requests.get(url).json()
    
    if 'message' in json_data:
        info.set('Invalid zip code.')
    
    else:
        city = json_data['name']
        #description = json_data['weather'][0]['description']
        #icon = json_data['weather'][0]['icon']
        temperature = str(json_data['main']['temp']) + u'\u00b0' + " F"
        #weather['fg'] = 'red'
        weather['font'] = 'Courier 18 normal'
        #weather['bg'] = 'pink'
    
        info.set(city + "\n" + temperature)
        
        if ['temp'] <= 119 and ['temp'] >= 110:
            weather['bg'] = 'burgundy'
        elif ['temp'] <= 109 and ['temp'] >=100:
            weather['bg'] = 'brown'
        elif ['temp'] <= 99 and ['temp'] >= 90:
            weather['bg'] = 'red'
        elif ['temp'] <= 89 and ['temp'] >= 80:
            weather['bg'] = 'orange'
Reply
#6
if json_data['main']['temp'] >= 110 and json_data['main']['temp'] <= 119:
    weather['bg'] = 'burgundy'
elif json_data['main']['temp'] >=100 and json_data['main']['temp'] <= 109:
    weather['bg'] = 'brown'
elif json_data['main']['temp'] >= 90 and json_data['main']['temp'] <=99:
    weather['bg'] = 'red'
elif json_data['main']['temp'] >= 80 and json_data['main']['temp'] <=89:
    weather['bg'] = 'orange'
else:
    weather['bg'] = 'pink'
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't change the colour of Tk button text Pilover 6 14,389 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,665 Aug-23-2022, 09:11 PM
Last Post: Extra
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,562 May-23-2022, 03:44 PM
Last Post: bigmac
Question [Tkinter] Change Treeview column color? water 3 9,284 Mar-04-2022, 11:20 AM
Last Post: Larz60+
  Can't get tkinter button to change color based on changes in data dford 4 3,314 Feb-13-2022, 01:57 PM
Last Post: dford
  how to change font size barryjo 4 3,695 Jan-26-2022, 08:46 PM
Last Post: menator01
  tkinter change the text of the checkbox zazas321 1 3,726 Sep-17-2021, 06:19 AM
Last Post: zazas321
  Updating button text based upon different variable values knoxvilles_joker 0 2,195 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  How to dynamically change radiobutton text kenwatts275 2 3,256 Mar-05-2021, 02:25 AM
Last Post: deanhystad
  Tkinter menu font size -method to change tonycat 2 7,653 Oct-11-2020, 02:43 AM
Last Post: tonycat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020