首页 > 解决方案 > 打开带有按钮的窗口时出现属性错误。'DataInput' 对象没有属性 'graph'

问题描述

我从 PyQt5 中的图形界面主题开始。

我已经定义了两个窗口,第一个是主窗口,另一个是包含一个按钮的子窗口,该按钮调用一个函数,通过相应事件的“单击”方法制作图形。

我的问题是,执行时出现以下错误:'DataInput' object has no attribute 'graph'

在这里我留下 .UI 文件:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.uic import loadUi

import matplotlib.pyplot as plt

# CLASS MAIN WINDOW 
# ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
class mainWindowInicial(QMainWindow):
    def __init__(self, parent=None):
        super(mainWindowInicial,self).__init__(parent)
        loadUi('Gui/mainwindow.ui', self)
        self.actionData.triggered.connect(self.open_dataInput)      

    # EVENTS ==================================================================
    def open_dataInput(self):
        DataInput(self).show()

def graphics():

    plt.plot((0,5),(0,5))
    plt.show()

class DataInput(QMainWindow):

    def __init__(self, parent=None):
        super(DataInput,self).__init__(parent)
        loadUi('Gui/datawindow.ui', self)
        
        self.pushButton_Graph.clicked.connect(self.graph)
        
        def graph():
            graphics()
        
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = mainWindowInicial()
    widget.show()
    sys.exit(app.exec_())

如果有人更了解 PyQt5,我将不胜感激您的帮助,最好的问候。

标签: pythonpython-3.xpyqt5

解决方案


推荐阅读