PyQt5 display animated gif in label - windows

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Users\Χρήστος\Desktop\Papinhio player\project\main\ui files\Menu 1\Sound files\Import sound file\Loading image\loading.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QMovie
class Ui_dialog(object):
def setupUi(self, dialog):
dialog.setObjectName("dialog")
dialog.resize(430, 110)
self.horizontalLayout = QtWidgets.QHBoxLayout(dialog)
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(197, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label = QtWidgets.QLabel(dialog)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
spacerItem1 = QtWidgets.QSpacerItem(197, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.movie = QMovie("ajax-loader.gif")
self.label.setText("")
self.label.setMovie(self.movie)
#self.label.setScaledContents(True)
self.movie.start()
self.label.resize(220,19)
self.retranslateUi(dialog)
QtCore.QMetaObject.connectSlotsByName(dialog)
def retranslateUi(self, dialog):
_translate = QtCore.QCoreApplication.translate
dialog.setWindowTitle(_translate("dialog", "Εισαγωγή αρχείου ήχου"))
#self.label.setText(_translate("dialog", "123456789101112131415"))
#import main_icons_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
dialog = QtWidgets.QDialog()
ui = Ui_dialog()
ui.setupUi(dialog)
dialog.show()
sys.exit(app.exec_())
The above code works in ubuntu 20.04, but it doesn't appeared something (just only the window, with title and ?,X buttons) in Windows 10.
In Ubuntu i have python3.6
In Windows 10 i have python3.8
What's wrong?

if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.addLibraryPath(r"C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\Lib\site-packages\pyqt5_tools\Qt\plugins")
dialog = QtWidgets.QDialog()
ui = Ui_dialog()
ui.setupUi(dialog)
dialog.show()
sys.exit(app.exec_())
I had to import the image libraries to work!!!

Related

PySide, editing spinbox colors

How in PySide would you edit the background color of the field of a spinbox, using stylesheets, but not change the color of the up-button and the down-buttons? Is there a flag to only adjust the "field?"
self.intensity_multiplier_spinbox_list[iter].setStyleSheet("QDoubleSpinBox {background-color: orange;color: black};")
You can use QPalette:
import sys
from PySide2 import QtGui, QtWidgets
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QDoubleSpinBox()
pal = w.palette()
pal.setColor(QtGui.QPalette.Base, QtGui.QColor("orange"))
w.setPalette(pal)
w.show()
sys.exit(app.exec_())

Windows "Command Prompt" inside tkinter frame

How to run command prompt inside tkinter frame? I know how to do it with "pygame", but those methods doesn't work - command prompt just runs in another window.
I tried to do it like this:
import tkinter as tk
import os
import subprocess
root = tk.Tk()
root.geometry("640x480")
frame = tk.Frame(root)
frame.pack(fill="both", expand=True)
child_env = dict(os.environ)
child_env["SDL_WINDOWID"] = str(frame.winfo_id())
child_env["SDL_VIDEODRIVER"] = "windib"
p = subprocess.Popen(["cmd.exe"], env=child_env)
root.mainloop()
But, as i said, it doesn't work.
This does not use "Command Prompt" as in the program included with Windows, but ConEmu has an -insidewnd parameter which is meant for allowing it to be embedded in third-party applications.
import sys,pathlib,subprocess
import tkinter as tk
import tkinter.ttk as ttk
# assuming a common location for the portable conemu installation:
conemu_exe = pathlib.Path.home()/"Documents"/"ConEmu"/"ConEmu64.exe"
class app(tk.Tk):
def __init__(self):
super().__init__()
self.console_frame = ttk.Frame(self,height=480,width=640)
self.console_frame.pack(fill="both",expand=True)
hwnd = hex(self.console_frame.winfo_id())
p = subprocess.Popen(
[str(conemu_exe), "-insidewnd", hwnd],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
app().mainloop()
from tkinter import Tk, Text
import subprocess
def execute(code):
command = cmd.get('1.0', 'end').split('\n')[-2]
if command == 'exit':
exit()
cmd.insert('end', f'\n{subprocess.getoutput(command)}')
main = Tk()
cmd = Text(main)
cmd.pack()
cmd.bind('<Return>', execute)
main.mainloop()

Unable to implement mpl_connect

I'm working on a GUI that basically hold multiple widgets that each contain a figure as well as a few buttons/whatever. One of the figures is supposed to be interactive, calling a function whenever the user clicks on any part of the plot. Yet I can't get the function to fire using mpl_connect, even after playing with focus and whatnot. I'm somewhat new to PySide/Qt, so I don't exactly understand why my code is behaving like this (I've been searching for days for a solution, but haven't found anything about it).
I used Qt Designer to create the layout for the GUI. I'm using Spyder from Anaconda 2.2.0 (32-bit), Python 2.7, and PySide to develop the GUI. If it's any help, I come from more of a MATLAB background where I developed a full version of the GUI I'm trying to make in Python.
Below is the relevant code (scroll down a bit to see where the problem is):
from PySide import QtCore, QtGui
from PySide.QtCore import *
from PySide.QtGui import *
import numpy as np
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import matplotlib.pyplot as plt
from PySide.QtGui import QPalette, QCursor
import matplotlib.colors as colors
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1316, 765)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.widget = QtGui.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(75, 40, 375, 490))
self.widget.setObjectName("widget")
color = self.centralwidget.palette().color(QPalette.Window)
self.leftPlot = MatplotlibWidget(None,'','','',False,color)
self.setupPlot(self.widget,self.leftPlot)
self.leftPlot.figure.tight_layout()
self.leftImage = self.leftPlot.axes.imshow(self.defaultSlide, cmap = mymap)
Snippet of interest:
self.leftPlot.figure.canvas.setFocusPolicy(QtCore.Qt.StrongFocus)
self.leftPlot.figure.canvas.setFocus()
cid = self.leftPlot.figure.canvas.mpl_connect('button_release_event', self.getCoordinates) # doesn't get called
plt.show()
def getCoordinates(self, event):
print 'dasdsadadsa'
print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(event.button, event.x, event.y, event.xdata, event.ydata)
The rest:
class MatplotlibWidget(FigureCanvas):
def __init__(self, parent=None,xlabel='x',ylabel='y',title='Title',showTicks=False,color=None):
super(MatplotlibWidget, self).__init__(Figure())
self.setParent(parent)
if color != None:
self.figure = Figure(facecolor=(color.red()/256.0,color.green()/256.0,color.blue()/256.0),frameon=0)
else:
self.figure = Figure(frameon=0)
self.canvas = FigureCanvas(self.figure)
self.axes = self.figure.add_subplot(111)
self.axes.set_xlabel(xlabel)
self.axes.set_ylabel(ylabel)
self.axes.set_title(title)
self.axes.get_xaxis().set_visible(showTicks)
self.axes.get_yaxis().set_visible(showTicks)
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ControlMainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
plt.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mySW = ControlMainWindow()
mySW.show()
sys.exit(app.exec_())
I'm aware the code is messy, but any input is greatly appreciated.
Update (2015-09-04) : I've updated the MWE I provided as part of my original answer to use instead the approach that is suggested in the matplotlib documentation to embed a mpl figure in an application. This approach does not use the pyplot interface (as in my original answer) and use the Object Oriented API of mpl instead. Also, since all the mpl artists (figure, axes, etc.) know each other, there is no need to explicitly create new class variables. This allows a structure of code that is, IMO, easier to read and to maintain.
The problem comes from the fact that you are not connecting correctly your event to self.leftPlot (FigureCanvasQTAgg), but to self.leftPlot.figure.canvas (FigureCanvasQTAgg.figure.FigureCanvasQTAgg) instead. You are creating a canvas within a canvas in the MatplotlibWidget class (which is already a subclass of FigureCanvasQTAgg). You only need to create one mpl canvas, pass a figure to it, and then connect the event to it directly.
I've put together a MWE to demonstrate how this can be done using the Object Oriented API of Matplotlib as suggested in the documentation:
from PySide import QtGui
import numpy as np
import sys
import matplotlib as mpl
mpl.use('Qt4Agg')
mpl.rcParams['backend.qt4']='PySide'
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ControlMainWindow, self).__init__(parent)
self.setupUi()
def setupUi(self):
figure = mpl.figure.Figure(figsize=(5, 5))
leftPlot = MatplotlibWidget(figure)
self.setCentralWidget(leftPlot)
class MatplotlibWidget(FigureCanvasQTAgg):
def __init__(self, fig):
super(MatplotlibWidget, self).__init__(fig)
#-- set up an axe artist --
ax = fig.add_axes([0.1, 0.1, 0.85, 0.85])
ax.plot(np.arange(15), np.arange(15))
self.draw()
#---- setup event ----
self.mpl_connect('button_press_event', self.onclick)
def onclick(self, event):
x, y = event.x, event.y
print(x, y)
if x != None and y != None:
ax = self.figure.axes[0]
ax.plot(event.xdata, event.ydata, 'ro')
self.draw()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mySW = ControlMainWindow()
mySW.show()
sys.exit(app.exec_())
The code above results in:

cx_freeze issue with relative path on mac

I'm having issues with cx_freeze relative path logic on mac. I'm using python 3.3 and pyqt5 and building an app portable windows - mac.
The following simple script just loads an image into a QLabel as a QPixmap.
The code works fine both on windows and mac when launched from console, builds both on windows and mac.
The exe works on windows without any problem.
The app ( MacOSX ) does not load the image, same for the dmg.
I have a feeling it has something to do with relative path setting. The reason for this belief is the following:
- if I try to load the .app the image will not appear
- if I copy-paste the testimage.jpeg in the same folder where the .app is it will load the image.
I did try include_files in the setup with no results.
Can anyone help?
Here are the scripts:
First the file image_insert_test.py:
import sys
from PyQt5.QtWidgets import (QApplication, QDialog,
QErrorMessage, QLabel, QWidget, QVBoxLayout )
from PyQt5.QtCore import pyqtSlot, QDir, Qt
from PyQt5 import QtGui
class Window(QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.fill_box2 = QLabel()
pixmap = QtGui.QPixmap('testimage.jpeg')
self.fill_box2.setPixmap(pixmap.scaled(100,100,Qt.KeepAspectRatio))
# set the layout
layout = QVBoxLayout()
layout.addWidget(self.fill_box2)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Window()
main.setWindowTitle('test gui')
main.show()
sys.exit(app.exec_())
the file setup.py:
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
options = {
'build_exe': {
"includes": ["matplotlib.backends.backend_macosx"] ,
"include_files" : ["testimage.jpeg"]
}
}
executables = [
Executable('image_insert_test.py', base=base)
]
setup(name='image_insert_test',
version='0.1',
description='Sample image_insert_test script',
options=options,
executables=executables
)
and this is the log of the cx_freeze bdist_mac http://pastebin.com/77uU4Exr
a workaround is the following:
instead of
pixmap = QtGui.QPixmap('testimage.jpeg')
place:
pixmap = QtGui.QPixmap(os.path.join(os.getcwd(),'image_insert_test-0.1.app/Contents/MacOS','testimage.jpeg')
this fishes the image path from the .app package but there has to be a more elegant way to do it.

Popup window on button click

I want to create a code which will display a window with a button, which clicked will create another window with some fields (like QLabel, QLineEdit, QSpinBox, etc.). However, I don't know how to create that popup window...
Here is my code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys # Needed for PySide
from PySide.QtCore import *
from PySide.QtGui import *
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
# Create widgets
self.label1 = QLabel("Label1")
self.button_open = QPushButton("Open popup")
self.button = QPushButton("Go!")
self.qbtn = QPushButton('Quit')
# Create layout and add widgets
layout = QVBoxLayout()
layout.addWidget(self.label1)
layout.addWidget(self.button_open)
# Buttons layout
hbox_buttons = QHBoxLayout()
hbox_buttons.addStretch(1)
hbox_buttons.addWidget(self.button)
hbox_buttons.addWidget(self.qbtn)
# Main layout
layout.addStretch(1)
layout.addWidget(self.button_open)
layout.addLayout(hbox_buttons)
self.setLayout(layout)
# Add buttons slots
self.button_open.clicked.connect(self.popup)
self.button.clicked.connect(self.function_runner)
self.qbtn.clicked.connect(QCoreApplication.instance().quit)
def popup (self, parent=__init__):
new_win = # I wonder what should be here
if __name__ == '__main__':
# Create the Qt Application
app = QApplication(sys.argv)
# Create and show the form
form = Form()
form.show()
# Run the main Qt loop
sys.exit(app.exec_())
I don't know if this is the best way, but the one I could figure out over the night... I hope it will help someone who got stuck with a similar problem.
So, I (simply) created a separate code for that second window and called it with
from subprocess import call
call("./my_2nd_window_code.py")

Resources