Is displayed on the control chart PyQt5 added in the Qt Designer

Previous my introduction about how to display dynamic charts in the window , but this situation is to create a window interface for pure use code, if it is necessary to design a more complex window, still have the need to use Qt Designer to create ui file! But dragging in the ui interface controls will not be able to use the above code to achieve, so this tell you about how to make the ui file control can display graphics, very simple, look down.
First, we need to create in Qt Designer in a main window, add a widget controls and a button, as shown below, while the py file of the complete code placed under Part I of this project directory, and name MatplotlibWidget.
Here Insert Picture Description
Then click on the object bar right widget, select Promote to, in a window pops up as shown below to add the class name, the py file name MatplotlibWidget that before, remember to and file name of the same, the header file is, and then click Add, then Added successfully! As shown below Second, check the class name, then click on the bottom right corner of Promote, you can see the widget's QWidget class as well as from the original into a MatplotlibWidget, see Figure III.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Finally, the window layout look, you can convert files to py ui file to run the display window of the code shown below, added directly to this document on the line chatter, then create a new province py file, you can see on the run a dynamic chart drawn up.

if __name__ == "__main__":
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Here Insert Picture Description
As for the [Open] button also can be applied on, you can start setting up a chart is not displayed, click on [Open] button after the display, as follows:

# 这部分写在setupUi()函数下
# 让图表先不显示
self.widget.setVisible(False)
# 连接【打开】按钮
self.pushButton.clicked.connect(self.pushButton_Clicked)

# 这部分写在主窗口类中
# 设置【打开】按钮
def pushButton_Clicked(self):
    # 设置甘特图可见
    self.widget.setVisible(True)

You can see the beginning of a run and did not display the chart, click the button will be displayed only after the above results and the same chart. As long as you remember to save the file that py charting, and then use the above method can be very easily add a chart you want it in the ui file!
Here Insert Picture Description

Published 17 original articles · won praise 3 · Views 1797

Guess you like

Origin blog.csdn.net/weixin_43350361/article/details/105422432