您好我不是Python或GUI开发方面的专家,对不起,如果这个问题已经发布,我没有找到类似的东西。
我正在使用Qt Designer开发我编写的软件的GUI,但我有一个问题可视化图。 我在这里展示了代码的简化版本。
使用Qt Designer我生成了3个按钮,每个按钮对应一个函数(正弦,余弦和正切)。 如果我单击按钮“sin”我想看到正弦函数,当我点击“cos”按钮时,我想在前一个画布上重叠这个其他函数,所以在同一个图上有正弦和余弦。 和另一个切线,并同时在屏幕上显示它们。
我不想在GUI中使用Matplotlib小部件,而是在单击按钮时出现弹出窗口。
我知道当所有东西都在同一个函数或类似函数时怎么做,但因为ShowSin(self)和ShowCos(self)是2个不同的函数,我不知道该怎么做。 例如,我修改了在ShowSin(自我)中plt.show之后删除括号的代码,当我点击按钮“sin”时没有任何反应,当我点击“cos”我得到我想要的,2个函数重叠,但我我想点击它的按钮时会看到正弦。
我也不明白为什么我会获得灰色背景(正如你从附件中看到的那样),这不是一个真正的问题,但我想消除。
你有什么建议吗?
我在Mac OS上使用Python 3.4,Matplotlib 1.5.1和PySide 1.2.4。
非常感谢!
Ciccio
具有灰色背景的程序图像:
from PySide import QtGui, QtCore
import sys
import os
import matplotlib.pyplot as plt
from gui.trig import *
import numpy as np
class Trig(QtGui.QMainWindow, Ui_Dialog):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
# Set the variable
self.x = np.arange(0, 2*np.pi, 0.1)
# Set the buttons
self.ui.sinButton.clicked.connect(self.ShowSin)
self.ui.cosButton.clicked.connect(self.ShowCos)
self.ui.tanButton.clicked.connect(self.ShowTan)
#---------------------------------------------------------
def ShowSin(self):
sinx = np.sin(self.x)
plt.figure('Data')
plt.plot(self.x, sinx, label='sin(x)')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.legend()
plt.grid()
plt.show()
#---------------------------------------------------------
def ShowCos(self):
cosx = np.cos(self.x)
plt.figure('Data')
plt.plot(self.x, cosx, label='cos(x)')
plt.xlabel('x')
plt.ylabel('cos(x)')
plt.legend()
plt.grid()
plt.show()
#---------------------------------------------------------
def ShowTan(self):
tanx = np.tan(self.x)
plt.figure('tan')
plt.plot(self.x, tanx, label='tan(x)')
plt.xlabel('x')
plt.ylabel('tan(x)')
plt.legend()
plt.grid()
plt.show()
#---------------------------------------------------------
def main():
app = QtGui.QApplication(sys.argv)
form = Trig()
form.show()
sys.exit(app.exec_())
#app.exec_()
if __name__ == '__main__':
main()
这是trig.py文件:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'trig.ui'
#
# Created: Tue Mar 29 14:03:31 2016
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!
from PySide import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(387, 281)
self.widget = QtGui.QWidget(Dialog)
self.widget.setGeometry(QtCore.QRect(120, 80, 115, 100))
self.widget.setObjectName("widget")
self.verticalLayout = QtGui.QVBoxLayout(self.widget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.sinButton = QtGui.QPushButton(self.widget)
self.sinButton.setObjectName("sinButton")
self.verticalLayout.addWidget(self.sinButton)
self.cosButton = QtGui.QPushButton(self.widget)
self.cosButton.setObjectName("cosButton")
self.verticalLayout.addWidget(self.cosButton)
self.tanButton = QtGui.QPushButton(self.widget)
self.tanButton.setObjectName("tanButton")
self.verticalLayout.addWidget(self.tanButton)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.sinButton.setText(QtGui.QApplication.translate("Dialog", "sin", None, QtGui.QApplication.UnicodeUTF8))
self.cosButton.setText(QtGui.QApplication.translate("Dialog", "cos", None, QtGui.QApplication.UnicodeUTF8))
self.tanButton.setText(QtGui.QApplication.translate("Dialog", "tan", None, QtGui.QApplication.UnicodeUTF8))