-
Notifications
You must be signed in to change notification settings - Fork 94
/
weather_app.rb
64 lines (55 loc) · 2.21 KB
/
weather_app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'Qt'
require 'httparty'
class WeatherApp < Qt::Widget
def initialize
super
self.window_title = 'Sk. Salahuddin Morning 01 Batch'
self.fixed_size = Qt::Size.new(520, 270)
@layout = Qt::VBoxLayout.new(self)
@title_label = Qt::Label.new('Weather App', self)
@title_label.alignment = Qt::AlignCenter
@title_label.font = Qt::Font.new('Arial', 18, Qt::Font::Bold)
@layout.add_widget(@title_label)
@input_layout = Qt::HBoxLayout.new
@input_label = Qt::Label.new('Enter City Name:', self)
@input_label.font = Qt::Font.new('Arial', 12)
@input_edit = Qt::LineEdit.new(self)
@input_edit.font = Qt::Font.new('Arial', 12)
@input_layout.add_widget(@input_label)
@input_layout.add_widget(@input_edit)
@layout.add_layout(@input_layout)
@submit_button = Qt::PushButton.new('Submit', self)
@submit_button.font = Qt::Font.new('Arial', 12)
@submit_button.connect(SIGNAL :clicked) { update_weather }
@layout.add_widget(@submit_button)
@weather_layout = Qt::VBoxLayout.new
@temperature_label = Qt::Label.new(self)
@temperature_label.alignment = Qt::AlignCenter
@temperature_label.font = Qt::Font.new('Arial', 14)
@weather_layout.add_widget(@temperature_label)
@description_label = Qt::Label.new(self)
@description_label.alignment = Qt::AlignCenter
@description_label.font = Qt::Font.new('Arial', 14)
@weather_layout.add_widget(@description_label)
@layout.add_layout(@weather_layout)
end
def update_weather
city = @input_edit.text
url = "https://api.openweathermap.org/data/2.5/weather?q=#{city}&appid=#{ENV['OPENWEATHERMAP_API_KEY']}&units=metric"
response = HTTParty.get(url)
data = JSON.parse(response.body)
if data['main'].nil?
@temperature_label.text = 'City not found'
@description_label.text = ''
else
temp = data['main']['temp']
description = data['weather'][0]['description']
@temperature_label.text = "Temperature: #{temp} °C"
@description_label.text = "Description: #{description.capitalize}"
end
end
end
app = Qt::Application.new(ARGV)
weather_app = WeatherApp.new
weather_app.show
app.exec