Controling a matplotlib plot from a separate window - plot

I'm trying to create the simplest example of controlling a plot in one window from buttons and functions in another window. I know I'm going to need more things in the window with the plot, so, I need to create my plot figure withing a QT window.
This is what I have so far:
My control window:
import sys
from PyQt4.QtCore import (QSize, SIGNAL)
from PyQt4.QtGui import (QApplication, QDialog, QPushButton, QVBoxLayout)
import PlotWindow
class Window(QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.pushButton1 = QPushButton()
self.pushButton1.setMaximumSize(QSize(110, 24))
self.pushButton1.setObjectName("pushButton1")
self.pushButton1.setText("Open Plot")
self.pushButton2 = QPushButton()
self.pushButton2.setMaximumSize(QSize(110, 24))
self.pushButton2.setObjectName("pushButton2")
self.pushButton2.setText("Update Plot")
layout = QVBoxLayout()
layout.addWidget(self.pushButton1)
layout.addWidget(self.pushButton2)
self.setLayout(layout)
self.setWindowTitle("Plot Control")
self.connect(self.pushButton1,SIGNAL("clicked()"),self.plotNew)
def plotNew(self):
print "new plot window"
self.dialog = PlotWindow.PlotWindow()
self.dialog.show()
app = QApplication(sys.argv)
form = Window()
form.open()
app.exec_()
My plot window :
import matplotlib
import numpy as np
from PyQt4 import QtGui
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import matplotlib.animation as animation
class PlotWindow(QtGui.QDialog):
def __init__(self, parent=None):
super(PlotWindow, self).__init__(parent)
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111)
plt.draw()
Obviously my plot window is where I'm having the biggest problems. I've gone through lots of examples, but none of them seem to show how to place a plot within another window.
Thanks

Well, I can answer part of my own question, but I would like comments on whether or not this is the best way to accomplish. The following code shows the changes I've made to bring up a second window with a plot inside.
import sys
from PyQt4 import QtGui
import numpy as np
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
class PlotCanvas(FigureCanvas):
def __init__(self, parent):
self.fig = Figure()
self.axes = self.fig.add_subplot(111)
# self.x = np.arange(0.0,0.3,0.01)
# self.y = np.cos(2*np.pi*self.x)
# self.axes.plot(self.x,self.y)
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
class PlotWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle("Plot Figure")
self.main_widget = QtGui.QWidget(self)
vbl = QtGui.QVBoxLayout(self.main_widget)
qmc = PlotCanvas(self.main_widget)
ntb = NavigationToolbar(qmc, self.main_widget)
vbl.addWidget(qmc)
vbl.addWidget(ntb)
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
I still need help with setting the data for the plot from the other window.
Thanks

Related

qt: qlayout do not correctly add widget

I want do dynamically change the layout in Qt. For example, I want to change the QHBoxLayout to QVBoxLayout through a button. My test code is:
from PyQt5.QtWidgets import *
import sys
class SubWidget(QWidget):
def __init__(self):
super().__init__()
self.lay = QHBoxLayout()
self.label1 = QLabel('left')
self.label2 = QLabel('right')
self.lay.addWidget(self.label1)
self.lay.addWidget(self.label2)
self.setLayout(self.lay)
def change(self):
self.lay.removeWidget(self.label1)
self.lay.removeWidget(self.label2)
self.lay = QVBoxLayout()
self.setLayout(self.lay)
self.lay.addWidget(self.label2)
self.lay.addWidget(self.label1)
class Widget(QWidget):
def __init__(self):
super().__init__()
lay = QVBoxLayout()
self.btn = QPushButton('change layout')
self.btn.clicked.connect(self.btnClick)
self.subWidget = SubWidget()
lay.addWidget(self.btn)
lay.addWidget(self.subWidget)
self.setLayout(lay)
def btnClick(self, check=False):
self.subWidget.change()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Widget()
win.show()
app.exec_()
The code output GUI is:
And I hope it change to the following picture after click change layout button:
Any suggestion is appreciated~~~
The point of the solution is
Make sure old layout is deleted, included both python wrapping reference and core Qt object, that's for deleteLater() is used.
Make sure new layout is assigned strictly after old was deleted, that's for need to use destroyed()-switchlayout() signal-slot chain.
I reproduced example with PySide6 (don't forget to switch on your version of PyQt or PySide package):
# from PyQt5.QtWidgets import *
from PySide6.QtWidgets import *
import sys
class SubWidget(QWidget):
def __init__(self):
super().__init__()
self.lay = QVBoxLayout()
self.label1 = QLabel('left')
self.label2 = QLabel('right')
self.lay.addWidget(self.label1)
self.lay.addWidget(self.label2)
self.setLayout(self.lay)
def change(self):
self.lay.removeWidget(self.label1)
self.lay.removeWidget(self.label2)
self.lay.deleteLater()
self.lay.destroyed.connect(self.switchlayout)
def switchlayout(self):
# print("***destroyed")
self.lay = QHBoxLayout()
self.lay.addWidget(self.label2)
self.lay.addWidget(self.label1)
self.setLayout(self.lay)
self.adjustSize()
class Widget(QWidget):
def __init__(self):
super().__init__()
lay = QVBoxLayout()
self.btn = QPushButton('change layout')
self.btn.clicked.connect(self.btnClick)
self.subWidget = SubWidget()
lay.addWidget(self.btn)
lay.addWidget(self.subWidget)
self.setLayout(lay)
def btnClick(self, check=False):
self.subWidget.change()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Widget()
win.show()
app.exec_()

Python listwidget click item button

First, you should know I'm very new to Python. I'm currently working on an app to increase my knowledge of this language.
I'm having a QListWidget which contains items based on a widget I created. This widget contains buttons like one to update or another to delete the item.
So I'm having troubles to do this action. For example, If I click on the update button, I want to open a window and load content by getting the item values.
How could I do that ?
This is my actual code
MainWindow
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtCore import QSize
from PyQt5.QtWidgets import QMainWindow, QHBoxLayout
from CustomWidgets.fb_line import fb_line
from Data.data_saver import data_saver
from MainWindows import Ui_MainWindow
from Windows.Ajout import Ajout
from Windows.Parametres import Parametres
from Windows.custom_fb_line import custom_fb_line
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.c_fb_l = custom_fb_line()
self.parametres = Parametres()
self.ajout = Ajout()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btn_parametre.clicked.connect(self.button_open_parametres)
self.ui.btn_ajouter.clicked.connect(self.button_open_ajout)
self.ui.btn_annuler.clicked.connect(self.button_annuler)
self.ui.btn_fermer.clicked.connect(self.close)
self.addLines()
def button_open_parametres(self):
self.parametres.show()
def button_open_ajout(self):
self.ajout.show()
def button_annuler(self):
self.ui.lw_dossier.clear()
self.addLines()
def addLines(self):
fbs = ds.read_data(data)
listWidget = self.ui.lw_dossier
listWidget.reset()
for fb in fbs:
item_widget = fb_line(fb)
item = QtWidgets.QListWidgetItem(listWidget)
item.setSizeHint(QSize(0, 50))
listWidget.addItem(item)
listWidget.setItemWidget(item, item_widget)
listWidget.show()
data = "./Data/data.pkl"
ds = data_saver()
Widget :
from PyQt5 import QtWidgets
from Objects.enum import enum
from custom_fb_line import Ui_custom_fb_line
class fb_line(QtWidgets.QWidget):
def __init__(self, fb, *args, **kwargs):
QtWidgets.QWidget.__init__(self, *args, **kwargs)
self.ui = Ui_custom_fb_line()
self.ui.setupUi(self)
self.ui.le_source.setText(fb.dossier_source)
self.ui.le_cible.setText(fb.dossier_cible)
self.ui.le_cron.setText("tous les " + str(fb.cron.number) + " " + enum().get_types()[fb.cron.type])
self.ui.btn_delete.clicked.connect(self.delete)
self.ui.btn_modifier.clicked.connect(self.modifier)
self.ui.btn_demarrer.clicked.connect(self.demarrer)
def delete(self):
print('delete ')
def modifier(self):
print('modifier')
def demarrer(self):
print('démarrer')
So print does actually show in console but I can't figure out how to achieve button click to update or delete items.
Never mind, I found a way, which is really simple in fact. Being new to python, I didn't know how to pass arguments to methods plugged to buttons. Here is how to do that for anyone who could have same issues
self.ui.btn_delete.clicked.connect(lambda: self.delete(fb))
def delete(self, fb):
print('delete ')
print(fb.dossier_source)

pyqtgraph custom tick labels potential bug

I'm using PyQtGraph and embedding a graph into a Qt program. I've added axisItems to the PlotWidget's constructor. It seems to do something a bit weird; I get some off line with 0___1 in the top right-hand corner. Does anyone know what is causing this and/or how to get rid of it?
I've attached a picture showing this and my code to generate what's in the picture.
If I don't add the AxisItem to the constructor (hence not getting my custom tick labels), the issue doesn't occur.
import sys
import numpy as np
from PySide import QtCore, QtGui, QtUiTools, QtXml
from ui import *
import pyqtgraph as pg
class PlotWidgetBarGraph(pg.PlotWidget):
def __init__(self, **opts):
# Add custom tick strings.
xDict = {1.:'1', 2.:'2', 3.:'3', 4.:'4',
5.:'5', 6.:'6', 7.:'7', 8.:'8',
9.:'1/2', 10.:'1/3', 11.:'1/4',
12.:'2/3', 13.:'2/4', 14.:'3/4',
15.:'1/2/3', 16.:'2/3/4',
17:'1/2/3/4'}
xAxis = pg.AxisItem(orientation='bottom')
xAxis.setTicks([xDict.items(), []])
super().__init__(axisItems={'bottom': xAxis})
# Create and add bar graph.
bg = pg.BarGraphItem(**opts)
self.addItem(bg)
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
splitter = QtGui.QSplitter(QtCore.Qt.Vertical)
x = np.arange(20)
y = np.abs(np.sin(x))+0.1
plot1 = PlotWidgetBarGraph(x=x, height=y, width=0.8, brush='b')
plot2 = PlotWidgetBarGraph(x=x, height=y, width=0.8, brush='b')
splitter.addWidget(plot1)
splitter.addWidget(plot2)
splitter.setStretchFactor(0, 1)
splitter.setStretchFactor(1, 2)
self.verticalLayout.addWidget(splitter)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

QMovie setting filename second time wont work

I am setting the Initial Movie File like this:
self.movie = QMovie("dotGreenBlack.gif", QByteArray(), self)
At runtime, i want to change it by setting:
self.movie.setFileName("dotGreyStatic.gif")
But this leads to frame-freeze of the primarly set file "dotGreenBlack.gif"
Anything else i have to do to change the Gif-Animation on runtime?
Here is the fullcode:
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class ImagePlayer(QWidget):
def __init__(self, filename, title, parent=None):
QWidget.__init__(self, parent)
# Load the file into a QMovie
self.movie = QMovie(filename, QByteArray(), self)
size = self.movie.scaledSize()
self.setGeometry(200, 200, size.width(), size.height())
self.setWindowTitle(title)
self.movie_screen = QLabel()
# Make label fit the gif
self.movie_screen.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.movie_screen.setAlignment(Qt.AlignCenter)
# Create the layout
main_layout = QVBoxLayout()
main_layout.addWidget(self.movie_screen)
self.setLayout(main_layout)
# Add the QMovie object to the label
self.movie.setCacheMode(QMovie.CacheAll)
self.movie.setSpeed(100)
self.movie_screen.setMovie(self.movie)
self.movie.start()
self.movie.loopCount()
self.movie.setFileName("dotGreyStatic.gif")
self.movie_screen.setMovie(self.movie)
if __name__ == "__main__":
gif = "dotGreenBlack.gif"
app = QApplication(sys.argv)
app.setStyleSheet("QWidget { background-color: black }")
player = ImagePlayer(gif, "was")
player.show()
sys.exit(app.exec_())
Gif Icons used in this Example:
You just need to stop and restart the movie:
self.movie.stop()
self.movie.setFileName("dotGreyStatic.gif")
self.movie.start()

Python code to change displayed image with pyqt4 on request

I have the following code to display am image using pyQt:
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
window.setGeometry(opts.posx, opts.posy, opts.width, opts.height)
pic = QtGui.QLabel(window)
pic.setGeometry(5, 5, opts.width-10, opts.height-10)
pixmap = QtGui.QPixmap(opts.filename)
pixmap = pixmap.scaledToHeight(opts.height)
pic.setPixmap(pixmap)
window.show()
sys.exit(app.exec_())
I would like to wrap up this code possibly in the form of a class, and be able to set a different image during runtime, using signals, socket, threads I really do not know. I would imagine something like:
class MyImage(object):
def __init(self, args):
some setup code
self.pic = whatever
def set_image(self, filename):
pixmap = QtGui.QPixmap(opts.filename)
pixmap = pixmap.scaledToHeight(opts.height)
pic.setPixmap(pixmap)
With the original code I just call sys.exit(app.exec_()) which makes the code 'freeze'. But I want to send a signal (and a filename) from a different running python code. Any suggestion how this can be handled easily and straightforward? Maybe overwriting the app.exec_ method?
Something like this should work for you:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4 import QtGui, QtCore
class ImageChanger(QtGui.QWidget):
def __init__(self, images, parent=None):
super(ImageChanger, self).__init__(parent)
self.comboBox = QtGui.QComboBox(self)
self.comboBox.addItems(images)
self.layout = QtGui.QVBoxLayout(self)
self.layout.addWidget(self.comboBox)
class MyWindow(QtGui.QWidget):
def __init__(self, images, parent=None):
super(MyWindow, self).__init__(parent)
self.label = QtGui.QLabel(self)
self.imageChanger = ImageChanger(images)
self.imageChanger.move(self.imageChanger.pos().y(), self.imageChanger.pos().x() + 100)
self.imageChanger.show()
self.imageChanger.comboBox.currentIndexChanged[str].connect(self.changeImage)
self.layout = QtGui.QVBoxLayout(self)
self.layout.addWidget(self.label)
#QtCore.pyqtSlot(str)
def changeImage(self, pathToImage):
pixmap = QtGui.QPixmap(pathToImage)
self.label.setPixmap(pixmap)
if __name__ == "__main__":
import sys
images = [ "/path/to/image/1",
"/path/to/image/2",
"/path/to/image/3",
]
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow(images)
main.show()
sys.exit(app.exec_())

Resources