英文:
plotting on PyQt6
问题
这是您提供的代码的翻译部分:
我之前在这里写过,但我删除了问题,因为没有具体信息。现在我会重新提出。
我有一个使用PyQt6开发的应用程序。它包含三个用于切换的按钮:如果您单击“2页”按钮,将显示两个带有“2页”标签的页面,类似于“3页”按钮。问题是:第一页应该显示一个绘图,但我遇到了以下错误:
`AttributeError: 'QWidget' object has no attribute 'plot'。`
我对面向对象编程还不太擅长,所以我不太明白如何修复它。我使用pyqtgraph库进行绘图。
由Qt Designer生成的代码。
这是我编写的逻辑代码。
```python
import sys
from PyQt6 import QtWidgets, QtGui, QtCore
from untdwdfd import Ui_MainWindow
from pyqtgraph import PlotWidget
import pyqtgraph as pg
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
QtCore.QMetaObject.connectSlotsByName(self)
self.pizdec.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
@QtCore.pyqtSlot()
def on_pushButton_clicked(self):
self.graph.setCurrentIndex(0)
@QtCore.pyqtSlot()
def on_pushButton_3_clicked(self):
self.graph.setCurrentIndex(1)
@QtCore.pyqtSlot()
def on_pushButton_2_clicked(self):
self.graph.setCurrentIndex(2)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
如果您有其他问题或需要进一步的帮助,请随时提问。
英文:
I already wrote here before, but I deleted the question, because there were no specifics. Now I'll bring it in.
I have an application that I have developed in PyQt6. It contains three buttons for switching: if you click on the " 2 page" button, there will be 2 pages with the inscription 2 page and similarly with the "3 page" button. The question is: page 1 should display a plot, in the pizdec area, however I get the following error:
AttributeError: 'QWidget' object has no attribute 'plot'.
I'm not really good at OOP yet, so I don't quite understand how to fix it. For plotting i use pyqtgraph library.
Code generated by Qt Designer.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(1495, 788)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setStyleSheet("\n"
"background-color: rgb(222, 222, 222);")
self.centralwidget = QtWidgets.QWidget(parent=MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(parent=self.centralwidget)
self.frame.setGeometry(QtCore.QRect(0, 0, 131, 761))
self.frame.setStyleSheet("background-color: rgb(21, 21, 21);")
self.frame.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame.setObjectName("frame")
self.verticalLayoutWidget = QtWidgets.QWidget(parent=self.frame)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 80, 111, 261))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(parent=self.verticalLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
self.pushButton.setStyleSheet("border: 0px solid;\n"
"color: rgb(255, 255, 255);\n"
"")
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.pushButton_3 = QtWidgets.QPushButton(parent=self.verticalLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_3.sizePolicy().hasHeightForWidth())
self.pushButton_3.setSizePolicy(sizePolicy)
self.pushButton_3.setStyleSheet("border: 0px solid;\n"
"color: rgb(255, 255, 255);")
self.pushButton_3.setObjectName("pushButton_3")
self.verticalLayout.addWidget(self.pushButton_3)
self.pushButton_2 = QtWidgets.QPushButton(parent=self.verticalLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_2.sizePolicy().hasHeightForWidth())
self.pushButton_2.setSizePolicy(sizePolicy)
self.pushButton_2.setStyleSheet("border: 0px solid;\n"
"color: rgb(255, 255, 255);")
self.pushButton_2.setObjectName("pushButton_2")
self.verticalLayout.addWidget(self.pushButton_2)
self.frame_2 = QtWidgets.QFrame(parent=self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(130, 0, 1371, 31))
self.frame_2.setStyleSheet("background-color: rgb(25, 25, 25);")
self.frame_2.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame_2.setObjectName("frame_2")
self.frame_3 = QtWidgets.QFrame(parent=self.centralwidget)
self.frame_3.setGeometry(QtCore.QRect(130, 30, 1371, 731))
self.frame_3.setStyleSheet("background-color: rgb(71, 71, 71);\n"
"background-color: rgb(34, 34, 34);")
self.frame_3.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame_3.setObjectName("frame_3")
self.graph = QtWidgets.QStackedWidget(parent=self.frame_3)
self.graph.setGeometry(QtCore.QRect(0, 10, 1361, 721))
self.graph.setObjectName("graph")
self.page = QtWidgets.QWidget()
self.page.setObjectName("page")
self.pizdec = QtWidgets.QWidget(parent=self.page)
self.pizdec.setGeometry(QtCore.QRect(70, 20, 1231, 441))
self.pizdec.setObjectName("pizdec")
self.graph.addWidget(self.page)
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.label_2 = QtWidgets.QLabel(parent=self.page_2)
self.label_2.setGeometry(QtCore.QRect(210, 100, 831, 401))
font = QtGui.QFont()
font.setPointSize(80)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.graph.addWidget(self.page_2)
self.page_3 = QtWidgets.QWidget()
self.page_3.setObjectName("page_3")
self.label_3 = QtWidgets.QLabel(parent=self.page_3)
self.label_3.setGeometry(QtCore.QRect(300, 100, 831, 401))
font = QtGui.QFont()
font.setPointSize(80)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.graph.addWidget(self.page_3)
self.frame_3.raise_()
self.frame.raise_()
self.frame_2.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(parent=MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1495, 26))
self.menubar.setObjectName("menubar")
self.menuw3rrewd = QtWidgets.QMenu(parent=self.menubar)
self.menuw3rrewd.setObjectName("menuw3rrewd")
MainWindow.setMenuBar(self.menubar)
self.menubar.addAction(self.menuw3rrewd.menuAction())
self.retranslateUi(MainWindow)
self.graph.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "1 page"))
self.pushButton_3.setText(_translate("MainWindow", "2 page"))
self.pushButton_2.setText(_translate("MainWindow", "3 page"))
self.label_2.setText(_translate("MainWindow", "2 page"))
self.label_3.setText(_translate("MainWindow", "3 page"))
self.menuw3rrewd.setTitle(_translate("MainWindow", "w3rrewd"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec())
Here's the logic code I wrote.
import sys
from PyQt6 import QtWidgets, QtGui, QtCore
from untdwdfd import Ui_MainWindow
from pyqtgraph import PlotWidget
import pyqtgraph as pg
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
QtCore.QMetaObject.connectSlotsByName(self)
self.pizdec.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
@QtCore.pyqtSlot()
def on_pushButton_clicked(self):
self.graph.setCurrentIndex(0)
@QtCore.pyqtSlot()
def on_pushButton_3_clicked(self):
self.graph.setCurrentIndex(1)
@QtCore.pyqtSlot()
def on_pushButton_2_clicked(self):
self.graph.setCurrentIndex(2)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
答案1
得分: 0
If you look at your UI
file you might notice that self.pizdec
is defined as a QWidget
.
如果你查看你的 UI
文件,你可能会注意到 self.pizdec
被定义为一个 QWidget
。
And then in your logic file you are calling:
然后在你的逻辑文件中,你调用了:
You are receiving the error because QWidget
doesn't have a plot
method.
你收到了错误,因为 QWidget
没有 plot
方法。
You technically shouldn't edit the uic files, so what you can do is edit your UI in designer and leave an empty space where self.pizdec
is and then add the widget in your logic file.
从技术上讲,你不应该编辑 uic 文件,所以你可以在设计师中编辑你的 UI,在 self.pizdec
的位置留出一个空白空间,然后在你的逻辑文件中添加小部件。
For example:
例如:
import sys
from PyQt6 import QtWidgets, QtGui, QtCore
from untdwdfd import Ui_MainWindow
from pyqtgraph import PlotWidget
import pyqtgraph as pg
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
QtCore.QMetaObject.connectSlotsByName(self)
self.pizdec = PlotWidget(parent=self.page)
self.pizdec.setGeometry(QtCore.QRect(70, 20, 1231, 441))
self.pizdec.setObjectName("pizdec")
self.pizdec.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
@QtCore.pyqtSlot()
def on_pushButton_clicked(self):
self.graph.setCurrentIndex(0)
@QtCore.pyqtSlot()
def on_pushButton_3_clicked(self):
self.graph.setCurrentIndex(1)
@QtCore.pyqtSlot()
def on_pushButton_2_clicked(self):
self.graph.setCurrentIndex(2)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
[![在此输入图像描述][1]][1]
<details>
<summary>英文:</summary>
If you look at your `UI` file you might notice that `self.pizdec` is defined as a `QWidget`.
#line 80ish
self.pizdec = QtWidgets.QWidget(parent=self.page)
self.pizdec.setGeometry(QtCore.QRect(70, 20, 1231, 441))
self.pizdec.setObjectName("pizdec")
And then in your logic file you are calling:
self.pizdec.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
You are receiving the error because `QWidget` doesn't have a `plot` method.
You technically shouldn't edit the uic files, so what you can do is edit your ui in designer and leave an empty space where `self.pizdec` is and then add the widget in your logic file.
For example:
import sys
from PyQt6 import QtWidgets, QtGui, QtCore
from untdwdfd import Ui_MainWindow
from pyqtgraph import PlotWidget
import pyqtgraph as pg
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def init(self):
super().init()
self.setupUi(self)
QtCore.QMetaObject.connectSlotsByName(self)
self.pizdec = QPlotWidget(parent=self.page)
self.pizdec.setGeometry(QtCore.QRect(70, 20, 1231, 441))
self.pizdec.setObjectName("pizdec")
self.pizdec.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
@QtCore.pyqtSlot()
def on_pushButton_clicked(self):
self.graph.setCurrentIndex(0)
@QtCore.pyqtSlot()
def on_pushButton_3_clicked(self):
self.graph.setCurrentIndex(1)
@QtCore.pyqtSlot()
def on_pushButton_2_clicked(self):
self.graph.setCurrentIndex(2)
if name == 'main':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/JwIgs.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论