[Python] How do I call methods of a PyQt5 QMainWindow subclass (like go_back) from another...

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I've created a simple web browser using PyQt5 in a file called browser.py. Here's a snippet of the code:

    # browser.py
    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://google.com'))
    self.setCentralWidget(self.browser)
    self.showMaximized()

    def go_back(self):
    self.browser.back()

    def go_forward(self):
    self.browser.forward()


    Now in another file main.py, I'm importing the MainWindow class like this:

    from browser import MainWindow


    My question is: how do I call methods like go_back() from main.py? I’m a bit confused about how to handle the self in those method definitions when calling them externally.

    Any help is appreciated!

    Continue reading...

Compartilhe esta Página