A
A
Andrey Andreev2021-01-16 14:58:59
Python
Andrey Andreev, 2021-01-16 14:58:59

Pyqt5 The program that should show the weather?

The program on pyqt5 made the design in qt designer, converted from ui to .py the program starts but does not display how many degrees. I ALREADY TRIED: replace from city to place, but it didn’t work, it seems there are no literal errors, what to do, here is the code.

import sys, pyowm
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from weather import Ui_MainWindow

#create app
app = QtWidgets.QApplication(sys.argv)

# init
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()

def get_weather_city():
  owm = pyowm.OWM('31dd6799e9426588099cc987780b7eb6')
  city = ui.lineEdit.text()

  observation = owm.weather_at_place(city)
  w = observation.get_weather()
  temperature = w.get_temperature ('celsius')['temp']

  ui.label.setText( f'Температура: {temperature}')

ui.pushButton.clicked.connect(get_weather_city)

sys.exit(app.exec_())

6002d4ff72b82001314798.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex F, 2021-01-16
@delvin-fil

Example
from PyQt5 import QtWidgets, uic
import sys
from pyowm import OWM

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('weather.ui', self)
        city = "Ленинск-Кузнецкий"
        owm = OWM('31dd6799e9426588099cc987780b7eb6')
        mgr = owm.weather_manager()
        observation = mgr.weather_at_place(city)
        w = observation.weather
        temperature = w.temperature('celsius')['temp']
        self.label.setText( f'В городе: {city} сейчас температура: {temperature}')
        self.show()
app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()

There is nothing in weather.ui except Label.
How to add LineEdit and BushButton, I think, you will figure it out yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question