Error: AttributeError: 'Widget' object has no attribute 'browser'
-
wrote on 24 Jan 2023, 19:46 last edited by
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:
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:
Porém assim, eu não consigo implementar em uma interface já criada no form.ui
-
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.
-
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.
-
@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.
-
@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.
wrote on 25 Jan 2023, 15:27 last edited by SGaist@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())
-
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())
-
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())
wrote on 25 Jan 2023, 23:25 last edited by@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' -
Remove the parent parameter. It's not needed since browser will be reparented when added to the layout.
1/8