Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error: AttributeError: 'Widget' object has no attribute 'browser'
Forum Update on Monday, May 27th 2025

Error: AttributeError: 'Widget' object has no attribute 'browser'

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    juninhoo
    wrote on 24 Jan 2023, 19:46 last edited by
    #1

    Hi Guys, I'm trying to create a program that allows me to plot a graph from the plotly library in the interface in QT, but I'm having some problems with it.

    I already managed to do this, but without using the form.ui part, and creating a simple interface, but I need to implement these graphics in interfaces that are already created and it happens that it always generates a new error.

    Now, the interface is like this: Image 01.png

    And I have this problem "Error: AttributeError: 'Widget' object has no attribute 'browser'".

    O único jeito que deu certo foi usando o segundo código:
    Image 02.png

    Porém assim, eu não consigo implementar em uma interface já criada no form.ui

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 24 Jan 2023, 19:51 last edited by
      #2

      Hi and welcome to devnet,

      browser is an attribute of the UI_Buttons class. However, the way you call show_graph in your lambda is wrong. You don't call it on an instance of UI_Buttons but you call that method explicitly passing your Widget instance to it as self parameter hence your error.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply 24 Jan 2023, 20:39
      0
      • S SGaist
        24 Jan 2023, 19:51

        Hi and welcome to devnet,

        browser is an attribute of the UI_Buttons class. However, the way you call show_graph in your lambda is wrong. You don't call it on an instance of UI_Buttons but you call that method explicitly passing your Widget instance to it as self parameter hence your error.

        J Offline
        J Offline
        juninhoo
        wrote on 24 Jan 2023, 20:39 last edited by
        #3

        @SGaist Boa tarde, não sei se entendi muito bem. Você poderia detalhar mais isso, por favor!

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 25 Jan 2023, 12:11 last edited by
          #4

          @juninhoo said in Error: AttributeError: 'Widget' object has no attribute 'browser':

          Boa tarde, não sei se entendi muito bem. Você poderia detalhar mais isso, por favor!

          Please use English when writing here as it is the reference language. If you would like to use Portuguese, there's is a dedicated sub-forum in the international category.

          As for your issue, I was referring to your first image (next time, please post code as text, it will be easier to read as well as to copy if required).

          If you want to use your UI_Buttons widget, make an instance of it and add it to whatever layout exists in your Ui_Widget class. Then connect your button to that object's show_graph method.

          ui_buttons = UI_Buttons()
          
          self.ui.<some_layout_name>.addWidget(ui_buttons)
          
          self.ui.button.clicked.connect(ui_buttons.show_graph)
          

          On a side note, please use meaningful names for your classes, that will help a lot to understand what they are about.

          You do not even need to split these into two different classes. From the looks of it, what you want to do is to add your QWebEngineWidget object to your designer based widget which you can do in a similar fashion as what I wrote above.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply 25 Jan 2023, 15:27
          1
          • S SGaist
            25 Jan 2023, 12:11

            @juninhoo said in Error: AttributeError: 'Widget' object has no attribute 'browser':

            Boa tarde, não sei se entendi muito bem. Você poderia detalhar mais isso, por favor!

            Please use English when writing here as it is the reference language. If you would like to use Portuguese, there's is a dedicated sub-forum in the international category.

            As for your issue, I was referring to your first image (next time, please post code as text, it will be easier to read as well as to copy if required).

            If you want to use your UI_Buttons widget, make an instance of it and add it to whatever layout exists in your Ui_Widget class. Then connect your button to that object's show_graph method.

            ui_buttons = UI_Buttons()
            
            self.ui.<some_layout_name>.addWidget(ui_buttons)
            
            self.ui.button.clicked.connect(ui_buttons.show_graph)
            

            On a side note, please use meaningful names for your classes, that will help a lot to understand what they are about.

            You do not even need to split these into two different classes. From the looks of it, what you want to do is to add your QWebEngineWidget object to your designer based widget which you can do in a similar fashion as what I wrote above.

            J Offline
            J Offline
            juninhoo
            wrote on 25 Jan 2023, 15:27 last edited by SGaist
            #5

            @SGaist said in Error: AttributeError: 'Widget' object has no attribute 'browser':

            ui_buttons = UI_Buttons()

            Hello, sorry for my mistakes, I'm new here. Sorry for the difficulty in understanding, I started programming in qt a little while ago, and I'm having some difficulties.

            I'll send the code as you suggested, and if you can detail a little more how the code should be I would be very grateful.

            CODE:

            # This Python file uses the following encoding: utf-8
            import sys
            
            from PySide6.QtWidgets import QApplication, QWidget
            
            # Important:
            # You need to run the following command to generate the ui_form.py file
            #     pyside6-uic form.ui -o ui_form.py, or
            #     pyside2-uic form.ui -o ui_form.py
            
            from ui_form import Ui_Widget
            from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
            import plotly.graph_objects as go
            
            class Widget(QWidget):
                def __init__(self, parent=None):
                    super().__init__(parent)
                    self.ui = Ui_Widget()
                    self.ui.setupUi(self)
            
                def show_graph(self):
                    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
                    y1 = [20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17]
                    y2 = [19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16]
            
                    fig = go.Figure()
                    fig.add_trace(go.Bar(x=months, y=y1, name='Primary Product', marker_color='aqua'))
                    fig.add_trace(go.Bar(x=months, y=y2, name='Secondary Product', marker_color='lightsalmon'))
                    fig.update_layout(barmode='group', xaxis_tickangle=-45)
                    self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
            
            class UI_Buttons():
                def __init__(self, parent=None):
                    super().__init__(parent)
            
                    ui_buttons = UI_Buttons()
                    self.ui.layout.addWidget(ui_buttons)
            
                    self.button = QtWidgets.QPushButton('Plot', self)
                    self.browser = QtWebEngineWidgets.QWebEngineView(self)
            
                    layout = QtWidgets.QVBoxLayout(self)
                    layout.addWidget(self.button, alignment=QtCore.Qt.AlignHCenter)
                    layout.addWidget(self.browser)
            
                    self.ui.layout.addWidget(ui_buttons)
                    self.ui.button.clicked.connect(ui_buttons.show_graph)
            
            if __name__ == "__main__":
                app = QApplication(sys.argv)
                widget = Widget()
                widget.show()
                sys.exit(app.exec())
            
            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 25 Jan 2023, 19:18 last edited by
              #6

              No you did not (please use coding tags when sharing code snippet).

              Anyway, here is something way simpler:

              # This Python file uses the following encoding: utf-8
              import sys
              
              from PySide6.QtWidgets import QApplication, QWidget
              
              # Important:
              # You need to run the following command to generate the ui_form.py file
              #     pyside6-uic form.ui -o ui_form.py, or
              #     pyside2-uic form.ui -o ui_form.py
              
              from ui_form import Ui_Widget
              from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
              import plotly.graph_objects as go
              
              class Widget(QWidget):
                  def __init__(self, parent=None):
                      super().__init__(parent)
                      self.ui = Ui_Widget()
                      self.ui.setupUi(self)
              
                      self.browser = QtWebEngineWidgets.QWebEngineView(self)
                      self.ui.layout.addWidget(self.browser)  # Change layout if it's not the right object name
                      self.ui.button.clicked.connect(self.show_graph)
              
                  def show_graph(self):
                      months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
                      y1 = [20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17]
                      y2 = [19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16]
              
                      fig = go.Figure()
                      fig.add_trace(go.Bar(x=months, y=y1, name='Primary Product', marker_color='aqua'))
                      fig.add_trace(go.Bar(x=months, y=y2, name='Secondary Product', marker_color='lightsalmon'))
                      fig.update_layout(barmode='group', xaxis_tickangle=-45)
                      self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
              
              if __name__ == "__main__":
                  app = QApplication(sys.argv)
                  widget = Widget()
                  widget.show()
                  sys.exit(app.exec())
              

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply 25 Jan 2023, 23:25
              0
              • S SGaist
                25 Jan 2023, 19:18

                No you did not (please use coding tags when sharing code snippet).

                Anyway, here is something way simpler:

                # This Python file uses the following encoding: utf-8
                import sys
                
                from PySide6.QtWidgets import QApplication, QWidget
                
                # Important:
                # You need to run the following command to generate the ui_form.py file
                #     pyside6-uic form.ui -o ui_form.py, or
                #     pyside2-uic form.ui -o ui_form.py
                
                from ui_form import Ui_Widget
                from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
                import plotly.graph_objects as go
                
                class Widget(QWidget):
                    def __init__(self, parent=None):
                        super().__init__(parent)
                        self.ui = Ui_Widget()
                        self.ui.setupUi(self)
                
                        self.browser = QtWebEngineWidgets.QWebEngineView(self)
                        self.ui.layout.addWidget(self.browser)  # Change layout if it's not the right object name
                        self.ui.button.clicked.connect(self.show_graph)
                
                    def show_graph(self):
                        months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
                        y1 = [20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17]
                        y2 = [19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16]
                
                        fig = go.Figure()
                        fig.add_trace(go.Bar(x=months, y=y1, name='Primary Product', marker_color='aqua'))
                        fig.add_trace(go.Bar(x=months, y=y2, name='Secondary Product', marker_color='lightsalmon'))
                        fig.update_layout(barmode='group', xaxis_tickangle=-45)
                        self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
                
                if __name__ == "__main__":
                    app = QApplication(sys.argv)
                    widget = Widget()
                    widget.show()
                    sys.exit(app.exec())
                
                J Offline
                J Offline
                juninhoo
                wrote on 25 Jan 2023, 23:25 last edited by
                #7

                @SGaist Sorry again! I tried using this code but there is another issue now.

                Menssage:
                Traceback (most recent call last):
                File "C:\Users\Junl\OneDrive\Doc\Plotly\widget.py", line 39, in <module>
                widget = Widget()
                File "C:\Users\Jun\OneDrive\Doc\Plotly\widget.py", line 21, in init
                self.browser = QtWebEngineWidgets.QWebEngineView(self)
                TypeError: QWebEngineView(parent: QWidget = None): argument 1 has unexpected type 'Widget'

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 26 Jan 2023, 20:20 last edited by
                  #8

                  Remove the parent parameter. It's not needed since browser will be reparented when added to the layout.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  1/8

                  24 Jan 2023, 19:46

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved