Slider in PyQt4 - python-3.x

I am new to PyQt, I am trying to implement slider in PyQt4, but i don't know why code code is not generating any output.
what i want is, create 3 slider to change 3 values dynamically.
Here i am resizing font size of text "hue", "sat", "val".
Is there is any good source to learn slider in PyQt?
here is my code
PyQt4Slider.py
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTiltle("HSV")
self.home()
def home(self):
#hue = 100
#sat = 100
#val = 100
layout = QVBoxLayout()
self.l1 = QLabel("hue")
self.l2 = QLabel("sat")
self.l3 = QLabel("val")
self.l1.setAlignment(Qt.AlignCenter)
self.l2.setAlignment(Qt.AlignCenter)
self.l3.setAlignment(Qt.AlignCenter)
layout.addWidget(self.l1)
layout.addWidget(self.l1)
layout.addWidget(self.l1)
self.sl = QSlider(Qt.Horizontal)
self.s2 = QSlider(Qt.Horizontal)
self.s3 = QSlider(Qt.Horizontal)
self.sl.setMinimum(0)
self.sl.setMaximum(179)
self.sl.setValue(20)
self.sl.setTickPosition(QSlider.TicksBelow)
self.sl.setTickInterval(5)
self.s2.setMinimum(0)
self.s2.setMaximum(255)
self.s2.setValue(100)
self.s2.setTickPosition(QSlider.TicksBelow)
self.s2.setTickInterval(5)
self.s3.setMinimum(0)
self.s3.setMaximum(255)
self.s3.setValue(100)
self.s3.setTickPosition(QSlider.TicksBelow)
self.s3.setTickInterval(5)
layout.addWidget(self.s1)
self.sl.valueChanged.connect(self.valuechange)
layout.addWidget(self.s2)
self.s2.valueChanged.connect(self.valuechange)
layout.addWidget(self.s3)
self.s3.valueChanged.connect(self.valuechange)
self.setLayout(layout)
def valuechange(self):
sizel1 = self.sl.value()
self.l1.setFont("Arial",sizel1)
sizel2 = self.sl.value()
self.l2.setFont("Arial", sizel2)
sizel2 = self.sl.value()
self.l2.setFont("Arial", sizel2)
def main():
app = QApplication(sys.argv)
ex = Window()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
here is what i am getting after running code
Ran 0 tests in 0.000s
OK
Process finished with exit code 0
Empty test suite.

Most of the problems in your script are caused by typos. Here is a fixed version that should work okay:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("HSV")
self.home()
def home(self):
layout = QVBoxLayout()
self.l1 = QLabel("hue")
self.l2 = QLabel("sat")
self.l3 = QLabel("val")
self.l1.setAlignment(Qt.AlignCenter)
self.l2.setAlignment(Qt.AlignCenter)
self.l3.setAlignment(Qt.AlignCenter)
layout.addWidget(self.l1)
layout.addWidget(self.l2)
layout.addWidget(self.l3)
self.s1 = QSlider(Qt.Horizontal)
self.s2 = QSlider(Qt.Horizontal)
self.s3 = QSlider(Qt.Horizontal)
self.s1.setMinimum(0)
self.s1.setMaximum(179)
self.s1.setValue(20)
self.s1.setTickPosition(QSlider.TicksBelow)
self.s1.setTickInterval(5)
self.s2.setMinimum(0)
self.s2.setMaximum(255)
self.s2.setValue(100)
self.s2.setTickPosition(QSlider.TicksBelow)
self.s2.setTickInterval(5)
self.s3.setMinimum(0)
self.s3.setMaximum(255)
self.s3.setValue(100)
self.s3.setTickPosition(QSlider.TicksBelow)
self.s3.setTickInterval(5)
layout.addWidget(self.s1)
self.s1.valueChanged.connect(self.valuechange)
layout.addWidget(self.s2)
self.s2.valueChanged.connect(self.valuechange)
layout.addWidget(self.s3)
self.s3.valueChanged.connect(self.valuechange)
self.setLayout(layout)
# set the initial fonts
self.valuechange()
def valuechange(self):
sizel1 = self.s1.value()
self.l1.setFont(QFont("Arial", sizel1))
sizel2 = self.s2.value()
self.l2.setFont(QFont("Arial", sizel2))
sizel3 = self.s3.value()
self.l3.setFont(QFont("Arial", sizel3))
def main():
app = QApplication(sys.argv)
ex = Window()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Related

Get a selected font in a subclassed QFontDialog

I'm trying to subclass QFontDialog and would like to retrieve the characteristics of the selected font. If I use getFont() a QFontDialog window appears first, ... I'm certainly doing something wrong.
Here's my example code :
from PyQt5.QtWidgets import (QFontDialog, QPushButton,
QMainWindow, QApplication,
QTabWidget, QWidget, QVBoxLayout)
import sys
class FontSelection(QFontDialog) :
def __init__(self, parent=None):
super(FontSelection, self).__init__(parent)
self.setOption(self.DontUseNativeDialog, True)
self.bouton = self.findChildren(QPushButton)
self.intitule_bouton = self.bouton[0].text().lower()
self.ouvertureBouton = [x for x in self.bouton if self.intitule_bouton in str(x.text()).lower()][0]
self.ouvertureBouton.clicked.disconnect()
self.ouvertureBouton.clicked.connect(self.font_recup)
def font_recup(self) :
self.font_capture()
def font_capture(self) :
if self.intitule_bouton in ['ok', '&ok'] :
font, self.intitule_bouton = self.getFont()
print(font)
class MainQFontDialogTry(QMainWindow):
def __init__(self):
super(MainQFontDialogTry, self).__init__()
self.setWindowTitle('QFontDialog subclassed try')
self.setGeometry(0, 0, 1000, 760)
self.setMinimumSize(1000, 760)
self.tab_widget = QTabWidget()
self.win_widget_1 = FontSelection(self)
widget = QWidget()
layout = QVBoxLayout(widget)
self.tab_widget.addTab(self.win_widget_1, "QFontDialog Tab")
layout.addWidget(self.tab_widget)
self.setCentralWidget(widget)
self.qfont = FontSelection()
self.qfont.font_recup()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainQFontDialogTry()
ex.show()
sys.exit(app.exec_())

How to record the video from a webcam in a pyqt5 gui using OpenCV and QThread?

I'm trying to make pyqt5 Gui that shows a webcam live feed, records the feed at the same time, and saves it locally when closed. I managed to acheieve this using Timer(QTimer) in pyqt gui but When I try to implement it using Qthread (Which I really require) only the live feed is working.
Whenever I add Code required for recording video and run the program it says Python has Stopped Working and closes. Here is my Code:
import cv2
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QVBoxLayout
from PyQt5.QtCore import QThread, Qt, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QImage, QPixmap
class Thread(QThread):
changePixmap = pyqtSignal(QImage)
def run(self):
self.cap = cv2.VideoCapture(0)
self.width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self.height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.codec = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
self.writer = cv2.VideoWriter('output.avi', self.codec, 30.0, (self.width, self.height))
while self.cap.isOpened():
ret, self.frame = self.cap.read()
if ret:
self.frame = cv2.flip(self.frame, 1)
rgbimage = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgbimage.shape
bytesPerLine = ch * w
convertToQtFormat = QImage(rgbimage.data, w, h, bytesPerLine, QImage.Format_RGB888)
p = convertToQtFormat.scaled(640, 480, Qt.KeepAspectRatio)
self.changePixmap.emit(p)
class MyApp(QWidget):
def __init__(self):
super(MyApp, self).__init__()
self.title = 'Camera'
self.initUI()
def initUI(self):
self.label = QLabel(self)
lay = QVBoxLayout()
lay.addWidget(self.label)
self.setLayout(lay)
self.th = Thread()
self.th.changePixmap.connect(self.setImage)
self.th.start()
self.show()
#pyqtSlot(QImage)
def setImage(self, image):
self.label.setPixmap(QPixmap.fromImage(image))
self.th.writer.write(image)
def main():
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I tried placing the .write() inside the run() of Thread class as well which is showing the same error. Can you guys point out What I'm doing wrong and how to make it work. I'm new to python and pyqt.
Thanks in Advance.
You need to separate threads. First thread is for signal, second is for the record and the main thread is for GUI. Try the following code. There is a button to start/stop the record.
import sys
import cv2
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QVBoxLayout
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer, QThread, pyqtSignal, pyqtSlot
from PyQt5 import QtWidgets, QtCore, QtGui
#https://ru.stackoverflow.com/a/1150993/396441
class Thread1(QThread):
changePixmap = pyqtSignal(QImage)
def __init__(self, *args, **kwargs):
super().__init__()
def run(self):
self.cap1 = cv2.VideoCapture(0, cv2.CAP_DSHOW)
self.cap1.set(3,480)
self.cap1.set(4,640)
self.cap1.set(5,30)
while True:
ret1, image1 = self.cap1.read()
if ret1:
im1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
height1, width1, channel1 = im1.shape
step1 = channel1 * width1
qImg1 = QImage(im1.data, width1, height1, step1, QImage.Format_RGB888)
self.changePixmap.emit(qImg1)
class Thread2(QThread):
def __init__(self, *args, **kwargs):
super().__init__()
self.active = True
def run(self):
if self.active:
self.fourcc = cv2.VideoWriter_fourcc(*'XVID')
self.out1 = cv2.VideoWriter('output.avi', self.fourcc, 30, (640,480))
self.cap1 = cv2.VideoCapture(0, cv2.CAP_DSHOW)
self.cap1.set(3, 480)
self.cap1.set(4, 640)
self.cap1.set(5, 30)
while self.active:
ret1, image1 = self.cap1.read()
if ret1:
self.out1.write(image1)
self.msleep(10)
def stop(self):
self.out1.release()
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(660, 520)
self.control_bt = QPushButton('START')
self.control_bt.clicked.connect(self.controlTimer)
self.image_label = QLabel()
self.saveTimer = QTimer()
self.th1 = Thread1(self)
self.th1.changePixmap.connect(self.setImage)
self.th1.start()
vlayout = QVBoxLayout(self)
vlayout.addWidget(self.image_label)
vlayout.addWidget(self.control_bt)
#QtCore.pyqtSlot(QImage)
def setImage(self, qImg1):
self.image_label.setPixmap(QPixmap.fromImage(qImg1))
def controlTimer(self):
if not self.saveTimer.isActive():
# write video
self.saveTimer.start()
self.th2 = Thread2(self)
self.th2.active = True
self.th2.start()
# update control_bt text
self.control_bt.setText("STOP")
else:
# stop writing
self.saveTimer.stop()
self.th2.active = False
self.th2.stop()
self.th2.terminate()
# update control_bt text
self.control_bt.setText("START")
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
you placed the .write() inside the run() of Thread class is right way.
like:
...
while self.cap.isOpened():
ret, self.frame = self.cap.read()
if ret:
self.frame = cv2.flip(self.frame, 1)
rgbimage = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgbimage.shape
bytesPerLine = ch * w
convertToQtFormat = QImage(
rgbimage.data, w, h, bytesPerLine, QImage.Format_RGB888)
p = convertToQtFormat.scaled(640, 480, Qt.KeepAspectRatio)
# put your writer() here, make sure your param is frame
# not converted to QtImage format
self.writer.write(rgbimage)
self.changePixmap.emit(p)
...

pyqt4 Main window crashes after second window closes

I've got this simple program. Its has a button and when you press it a second window will come up.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import urllib.request
class second_window(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self)
super(second_window, self).__init__(parent)
layout = QVBoxLayout()
button = QPushButton("close")
button.clicked.connect(self.button_clicked)
layout.addWidget(button)
self.setLayout(layout)
def button_clicked(self):
self.close()
class Main_window(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
button = QPushButton("Second Window")
self.sec_window = second_window(self)
layout.addWidget(button)
button.clicked.connect(self.button_clicked)
self.setLayout(layout)
def button_clicked(self):
self.sec_window.exec_()
if __name__ == "__main__":
app = QApplication(sys.argv)
dl = window()
dl.show()
app.exec()
But sometimes if you close the Main_window just after you close the second_window it crashes and I get a message saying "Python.exe has stopped working".
can anyone help?
First Solution: Change self.sec_window = second_window(self) to self.sec_window = second_window() since it will not be necessary to clean because there is no relationship between second_window and Main_window. As the following code shows
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class second_window(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self)
super(second_window, self).__init__(parent)
layout = QVBoxLayout()
button = QPushButton("close")
button.clicked.connect(self.button_clicked)
layout.addWidget(button)
self.setLayout(layout)
def button_clicked(self):
self.close()
class Main_window(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
button = QPushButton("Second Window")
self.sec_window = second_window()
layout.addWidget(button)
button.clicked.connect(self.button_clicked)
self.setLayout(layout)
def button_clicked(self):
self.sec_window.exec_()
if __name__ == "__main__":
app = QApplication(sys.argv)
dl = Main_window()
dl.show()
app.exec()
Second Solution: Add self.sec_window.deleteLater() on closeEvent(self, event) to be able to delete second_window
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class second_window(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self)
super(second_window, self).__init__(parent)
layout = QVBoxLayout()
button = QPushButton("close")
button.clicked.connect(self.button_clicked)
layout.addWidget(button)
self.setLayout(layout)
def button_clicked(self):
self.close()
class Main_window(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
button = QPushButton("Second Window")
self.sec_window = second_window(self)
layout.addWidget(button)
button.clicked.connect(self.button_clicked)
self.setLayout(layout)
def button_clicked(self):
self.sec_window.exec_()
def closeEvent(self, event):
self.sec_window.deleteLater()
super().closeEvent(event)
if __name__ == "__main__":
app = QApplication(sys.argv)
dl = Main_window()
dl.show()
app.exec()

PyQt QMessageBox crush program

I have a problem with my code. How to correct the code below to QMessageBox not crash program? I don't know why this problem really exists. I tried run QMessageBox in new QThread but it changed nothing.
import sys
from time import sleep
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Signals(QObject):
update = pyqtSignal(int)
enable_button = pyqtSignal(bool)
class Window(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
self.button = QPushButton("Run", self)
self.button.clicked.connect(self.onButton)
self.progress = QProgressBar(self)
self.progress.setTextVisible(False)
self.layout = QVBoxLayout()
self.layout.setContentsMargins(5, 5, 5, 5)
self.layout.addWidget(self.button)
self.layout.addWidget(self.progress)
self.layout.addStretch()
self.worker_thread = QThread()
self.worker_thread.run = self.worker
self.worker_thread.should_close = False
self.signals = Signals()
self.signals.update.connect(self.progress.setValue)
self.signals.enable_button.connect(self.button.setEnabled)
self.setLayout(self.layout)
self.show()
self.resize(self.size().width(), 0)
def closeEvent(self, e):
self.worker_thread.should_close = True
self.worker_thread.wait()
#pyqtSlot()
def onButton(self):
self.button.setDisabled(True)
self.worker_thread.start()
def worker(self):
for i in range(101):
if self.worker_thread.should_close:
break
self.signals.update.emit(i)
sleep(0.1)
msgBox = QMessageBox(QMessageBox.Information, "Ok", "Done.", QMessageBox.NoButton)
icon = QIcon()
icon.addPixmap(QPixmap(":/favicon.ico"), QIcon.Normal, QIcon.Off)
msgBox.setWindowIcon(icon)
msgBox.addButton("Ok", QMessageBox.AcceptRole)
msgBox.exec_()
self.signals.enable_button.emit(True)
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

QWidget cannot display on QMainWindow instance PyQt5

I am learning PyQt5 now and tried to do something little on my own. I have made a very basic custom toolbox, which has just 6 QPushButtons buttons on it, which inherits from QWidget class.
My problem is that I can't display my toolbox on my QMainWidow instance. Let me show you what I did;
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class ToolBox(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn = [QPushButton('B', self) for i in range(6)]
for Btn in btn:
Btn.resize(30, 30)
self.resize(60, 90)
k = 0
for i in range(6):
btn[i].move((i%2)*30, k*30)
k += 1 if i % 2 == 1 else 0
self.show()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.resize(300, 200)
self.statusBar().showMessage('Ready!')
exitAction = QAction(QIcon('idea.png'), 'Exit', self)
exitAction.setStatusTip('Exit application')
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(qApp.quit)
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('File')
fileMenu.addAction(exitAction)
t = ToolBox()
t.move(150, 150)
t.show() #With and without this line, it doesn't work.
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
m = MainWindow()
sys.exit(app.exec_())
You just need to position your widget somewhere in the QMainWindow canvas. All you have to do is position it in the MainWindow. Just for an example, I use setCentralWidget() to position your QWidget.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class ToolBox(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn = [QPushButton('B', self) for i in range(6)]
for Btn in btn:
Btn.resize(30, 30)
self.resize(60, 90)
k = 0
for i in range(6):
btn[i].move((i%2)*30, k*30)
k += 1 if i % 2 == 1 else 0
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.resize(300, 200)
self.statusBar().showMessage('Ready!')
exitAction = QAction(QIcon('idea.png'), 'Exit', self)
exitAction.setStatusTip('Exit application')
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(qApp.quit)
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('File')
fileMenu.addAction(exitAction)
t = ToolBox()
self.setCentralWidget(t)
if __name__ == '__main__':
app = QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())

Resources