PyQt5, AttributeError: ‘MainWindow’ object has no attribute ‘navigate_url’

  attributeerror, browser, pyqt5, python

I’m following Programming Hero’s Web Browser tutorial https://www.youtube.com/watch?v=z-5bZ8EoKu4 and everything has been working great until I implemented the URL bar function. I get the error following:

AttributeError: ‘MainWindow’ object has no attribute ‘navigate_url’

Heres the code:

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl("http://duckduckgo.com"))
        self.setCentralWidget(self.browser)
        self.showMaximized()


        # Navigation Bar
        navbar = QToolBar()
        self.addToolBar(navbar)

        back_btn = QAction("Back", self)
        back_btn.triggered.connect(self.browser.back)
        navbar.addAction(back_btn)

        forward_btn =QAction("Forward", self)
        forward_btn.triggered.connect(self.browser.forward)
        navbar.addAction(forward_btn)

        reload_btn = QAction("Reload", self)
        reload_btn.triggered.connect(self.browser.reload)
        navbar.addAction(reload_btn)

        home_btn = QAction("Home", self)
        home_btn.triggered.connect(self.navigate_home)
        navbar.addAction(home_btn)

        self.url_bar = QLineEdit()
        self.url_bar.returnPressed.connect(self.navigate_url)
        navbar.addWidget(self.url_bar)



    def navigate_home(self):
        self.browser.setUrl(QUrl("https://duckduckgo.com"))

    def navigate_to_url(self):
        url = self.url_bar.text()
        self.browser.setUrl(QUrl(url))


app = QApplication(sys.argv)
QApplication.setApplicationName("Crystal Browser")
window = MainWindow()
app.exec_()

Source: Python Questions

LEAVE A COMMENT