AttributeError: 'NoneType' object has no attribute 'registerObject' inPyQt

64 views Asked by At

I want to know the weather based on the location clicked on a google map. My codes are below. However, I get the error 'NoneType' object has no attribute 'registerObject'. I did research on this error, but it wasn't enough. I couldn't solve the problem. I'm not a professional programmer.I have no idea how and what to do. Please can you help me? Thank you in advance for your help.


class WeatherWidget(QWidget):
    def __init__(self):
        super().__init__()

        self.layout = QVBoxLayout(self)
        self.setGeometry(100, 100, 800, 600)

        # Web tarayıcısını oluştur
        self.browser = QWebEngineView()
        self.browser.settings().setAttribute(QWebEngineSettings.JavascriptEnabled, True)
        self.layout.addWidget(self.browser)

        # Hava durumu bilgilerini göstermek için etiket
        self.weather_label = QLabel()
        self.layout.addWidget(self.weather_label)

        # Google Maps API anahtarı
        # Bu kısmı kendi API anahtarınızla değiştirin.
        google_maps_api_key = "AIzaSyAZ5IyeWsFNY4E78H1oFYx4mUnJijeC4Uw"

        # Harita URL'sini oluştur
        map_url = "https://www.google.com/maps/d/embed?mid=1LVbyUHCzWPw5VV4t1DWRKrUsof8wGyo&ehbc=2E312F&noprof=1".format(
            google_maps_api_key)

        # HTML içeriği
        html_content = """ 
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>Weather Map</title>
                <script src="{}" async defer></script>
                <script>
                    function initMap()
                    {{
                        var map = new google.maps.Map(document.getElementById('map'), 
                        {{
                            center: {{lat: 0, lng: 0}},
                            zoom: 2
                        }});

                        // Haritada tıklama olayını dinle
                        map.addListener('click', function(event) 
                        {{
                            var latitude = event.latLng.lat();
                            var longitude = event.latLng.lng();

                            // Tıklanan konumu Python uygulamasına bildir
                            window.pywebchannel_1.emit('mapClick', latitude, longitude);
                        }});
                    }}
                </script>
            </head>
            <body>
                <div id="map" style="height: 400px; width: 100%;"></div>
            </body>
            </html>
        """.format(map_url)

        # HTML içeriğini yükle
        self.browser.setHtml(html_content, QUrl("about:blank"))

        # Web tarayıcısından Python'a mesaj almak için bağlantı
        self.browser.page().webChannel().registerObject("pywebchannel_1", self)

    # Tıklanan konumu işle
    def mapClick(self, latitude, longitude):
        self.getWeatherInfo(latitude, longitude)

    # Hava durumu bilgilerini al
    def getWeatherInfo(self, latitude, longitude):
        # OpenWeatherMap API anahtarı
        # Bu kısmı kendi API anahtarınızla değiştirin.
        openweathermap_api_key = "1628c373efa95d37cc8de29f8c882d34"

        # OpenWeatherMap API'si için istek yap
        url = f"http://api.openweathermap.org/data/2.5/weather?lat={latitude}&lon={longitude}&appid={openweathermap_api_key}&units=metric"
        response = requests.get(url)
        data = response.json()

0

There are 0 answers