当前位置: 动力学知识库 > 问答 > 编程问答 >

python - How to display a qt dialog by clicking

问题描述:

So I have written a program in which when you click on a point in a plot opens a window with the concrete coordinates. Now I want that when I click on any point in the plot that it opens also in addition a window (which I designed earlier with Qt designer) which simply shows an empty plot. How do I make this?

this is the script:

from PyQt4.uic import loadUiType

from PyQt4 import QtCore, QtGui, uic

from PyQt4.QtGui import QApplication, QDialog

from matplotlib.figure import Figure

from matplotlib.backends.backend_qt4agg import (

FigureCanvasQTAgg as FigureCanvas,

NavigationToolbar2QT as NavigationToolbar)

import matplotlib.pyplot as plt

import sys

Ui_MainWindow, QMainWindow = loadUiType('window.ui')

class Main(QMainWindow, Ui_MainWindow):

def __init__(self, parent=None):

super(Main, self).__init__()

self.setupUi(self)

self.fig_dict = {}

self.ax_dict = {}

self.mplfigs.itemClicked.connect(self.changefig)

fig = Figure()

self.addmpl(fig)

def changefig(self,item):

text = item.text()

self.rmmpl()

self.addmpl(self.fig_dict[str(text)])

def addfig(self, name, fig, ax):

self.fig_dict[name] = fig

self.ax_dict[name] = ax

self.mplfigs.addItem(name)

def addmpl(self, fig):

self.canvas = FigureCanvas(fig)

self.mplvl.addWidget(self.canvas)

self.canvas.draw()

self.toolbar = NavigationToolbar(self.canvas,

self.mplwindow, coordinates=True)

self.mplvl.addWidget(self.toolbar)

self.canvas.mpl_connect('pick_event', self.on_pick)

def rmmpl(self):

self.mplvl.removeWidget(self.canvas)

self.canvas.close()

self.mplvl.removeWidget(self.toolbar)

self.toolbar.close()

def on_pick(self, event):

print('On Pick!')

thisline = event.artist

xdata = thisline.get_xdata()

ydata = thisline.get_ydata()

ind = event.ind

print(ind)

msg = str(xdata) + ' ' + str(ydata)

points = tuple(zip(xdata[ind], ydata[ind]))

myString = "got data %s, %s" % (str(xdata[ind]),str(ydata[ind]))

print('onpick points:', points)

QtGui.QMessageBox.information(self, "Click!", '' + myString)

ax1f1 = self.ax_dict['One plot']

highlighted_point = ax1f1.plot(xdata[ind],ydata[ind], marker='s', color='k',markersize=15)

self.canvas.draw()

open ('window.ui')

try:

_fromUtf8 = QtCore.QString.fromUtf8

except AttributeError:

_fromUtf8 = lambda s: s

class Ui_Dialog(object):

def setupUi(self, Dialog):

Dialog.setObjectName(_fromUtf8("Dialog"))

Dialog.resize(347, 249)

self.gridLayout = QtGui.QGridLayout(Dialog)

self.gridLayout.setObjectName(_fromUtf8("gridLayout"))

self.qwtPlot = Qwt5.QwtPlot(Dialog)

self.qwtPlot.setObjectName(_fromUtf8("qwtPlot"))

self.gridLayout.addWidget(self.qwtPlot, 0, 0, 1, 1)

self.retranslateUi(Dialog)

QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):

Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))

from PyQt4 import Qwt5

self.dialog = Ui_Dialog()

self.dialog.show()

if __name__ == '__main__':

import sys

from PyQt4 import QtGui

# If you want to display concrete POINTS:

x_series_1 = [4,1,2,3 ,4,5]

y_series_1 = [4,3,9,27,54,63]

x_series_2 = [4,2,4,6,8,10]

y_series_2 = [4,4,8,12,16,18]

# If you want to display a FUNCTION:

x_series_3 = [1,5,10,15,20,25]

y_series_3 = [x**2 for x in x_series_3]

x_series_4 = [1,2,3,4,5,6]

y_series_4 = [x**3 for x in x_series_4]

fig1 = Figure()

fig1=plt.figure()

ax1f1 = fig1.add_subplot(111)

ax1f1.plot(x_series_1, y_series_1, marker='s', linestyle='-', color='k',picker = 5)

ax1f1.plot(x_series_2, y_series_2, marker='s', linestyle='-', color='k',picker = 5)

ax1f1.plot(x_series_3, y_series_3, marker='s', linestyle='-', color='k',picker = 5)

ax1f1.plot(x_series_4, y_series_4, marker='s', linestyle='-', color='k',picker = 5)

#ax1f1.plot(x_series_1, y_series_1)

# If you want to display concrete POINTS:

ax1f1.plot(x_series_1, y_series_1, label="Graph 1")

ax1f1.plot(x_series_2, y_series_2, label="Graph 2")

# If you want to dispay a FUNCTION:

ax1f1.plot(x_series_3, y_series_3, label="Graph 3")

ax1f1.plot(x_series_4, y_series_4, label="Graph 4")

#create legend

ax1f1.legend(loc="upper left")

app = QtGui.QApplication(sys.argv)

main = Main()

main.addfig('One plot', fig1,ax1f1)

print main.fig_dict

main.show()

sys.exit(app.exec_())

分享给朋友:
您可能感兴趣的文章:
随机阅读: