How to Mute Sound in QWebEngineView PyQt5 Python - python

Problem
I Want to Mute all sound QWebEngineView on Button click. And Also UnMute all Sound on another Button Click. I searched a lot on the internet but no one asked this type of Questions.
Code
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
import sys
class Main(QWidget):
def __init__(self):
super(Main, self).__init__()
self.vbox=QVBoxLayout()
self.mutebtn=QPushButton('Mute this WebPage')
self.mutebtn.clicked.connect(self.mutebrowser)
self.vbox.addWidget(self.mutebtn)
self.unmutebtn=QPushButton('UnMute this WebPage')
self.unmutebtn.clicked.connect(self.unmutebrowser)
self.vbox.addWidget(self.unmutebtn)
self.browser=QWebEngineView()
self.browser.setUrl(QUrl('https://youtu.be/B-wmrlzdK3k'))
self.vbox.addWidget(self.browser)
self.setMinimumSize(400,500)
self.setLayout(self.vbox)
def mutebrowser(self):
# Code to Mute Sound in "self.browser"
pass
def unmutebrowser(self):
# Code to UnMute Sound in "self.browser"
pass
if __name__ == '__main__':
app=QApplication(sys.argv)
win=Main()
win.show()
sys.exit(app.exec_())

Related

How to add video to Pyqt5 background?

Hello I'm trying to add galaxy.mp4 file to my pqt5 window background with below source codes, when I try with this, any video showing and starting application closed and didnt give any error. How can I solve this ? Im using Windows10
from PyQt5 import QtWidgets, QtMultimediaWidgets, QtMultimedia, QtCore, QtGui, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QLineEdit, QComboBox
from PyQt5.QtGui import QTransform
import sys
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(0, 0, 1920, 1080)
self.setWindowTitle("Welcome Galaxy")
self.Welcome()
self.show()
def Welcome(self):
# create link to movie file
movie_file = QtCore.QUrl.fromLocalFile('./galaxy.mp4')
vid_media = QtMultimedia.QMediaContent(movie_file)
# create video widget
self.videoWidget = QtMultimediaWidgets.QVideoWidget()
self.videoWidget.setGeometry(0,0,1920,1080)
# create media player object (video widget goes in media player)
self.mediaPlayer = QtMultimedia.QMediaPlayer(None,
QtMultimedia.QMediaPlayer.VideoSurface)
self.mediaPlayer.setVideoOutput(self.videoWidget)
# playlist
self.playlist = QtMultimedia.QMediaPlaylist()
self.playlist.setCurrentIndex(0)
self.playlist.setPlaybackMode(QtMultimedia.QMediaPlaylist.Loop)
self.playlist.addMedia(vid_media)
# add content to media player
self.mediaPlayer.setPlaylist(self.playlist)
self.mediaPlayer.play()
self.setCentralWidget(self.videoWidget)
app = QApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec_())

Edit text when I open a window

I'm working with PYQt5 and Python. Whenever I open a new window and that window has a lineEdit, I have to clic in the lineEdit in order to write any text. Is there a way in which I can start writing the text in the lineEdit just the moment I open the window without, first, having to clic on it?
Thanks!
Edit. Here is the code I've been using
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QVBoxLayout, QMainWindow
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer, QThread, pyqtSignal, pyqtSlot
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.uic import loadUi
class VentanaInicio(QMainWindow):
path = "null"
def __init__(self):
super(VentanaInicio,self).__init__()
loadUi('D:\Matt\Combi\Archivo.ui',self)
self.pushButton.clicked.connect(self.conectar)
# Abrir Ventana Video
def conectar(self):
self.crear_carpeta()
iniciar = VentanaVideo()
iniciar.exec_()
# Salir
def conectar1(self):
self.close()
def crear_carpeta(self):
VentanaInicio.path = str(self.lineEdit.text())
if not self.lineEdit.text():
VentanaInicio.path = "null"
try:
os.mkdir(VentanaInicio.path)
print("Carpeta Creada")
except FileExistsError:
print("Carpeta Existe")
return VentanaInicio.path
Thanks to musicamante the question was solved. I added the setFocus() like this.
def __init__(self):
super(VentanaInicio,self).__init__()
loadUi('D:\Matt\Combi\Archivo.ui',self)
self.lineEdit.setFocus(True)
self.pushButton.clicked.connect(self.conectar)

How to use QTest.keySequence?

I'm trying to test if a shortcut is working using PyQt5 and QTest. Here is my code:
Main.py
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, QPushButton
class Window(QWidget):
def __init__(self):
super().__init__()
self.more_btn = QPushButton("More")
self.more_btn.clicked.connect(self.on_clicked)
self.more_btn.setShortcut(QKeySequence.Undo)
vbox = QVBoxLayout()
vbox.addWidget(self.more_btn)
self.setLayout(vbox)
def on_clicked(self):
print("Item clicked")
test_main.py
import sys
import unittest
from PyQt5.QtGui import QKeySequence
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QApplication
from Main import Window
class MainTestCase(unittest.TestCase):
def test_main(self):
app = QApplication(sys.argv)
form = Window()
QTest.keySequence(form, QKeySequence.Undo)
When I run test_main, item_clicked should be printed, but it doesn't. I tried to debug the program and set the breatpoint at the print("Item clicked") line, it didn't stop. It seems that QTest.keySequence didn't work. Why? How should I make it work?

To turn back from 2nd window to 1st window (different files)

An application is being created with a large number of different windows.
The bottom line is that I am trying to get from the main window (by clicking button) to the client window. There is "Back" button in the clients window, which should return the user to the main window.
Both codes are in different files. The problem occurs at the stage of pressing the "Customers" button (Process finished with exit code -1073740791 (0xC0000409)).
Moreover, if everything is in one file, then everything works fine. But only here the program is not planned to be small, so I don't want to put a huge code in one file.
Welcome_Screen.py
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QDialog, QApplication
import Customers
class WelcomeScreen(QDialog):
def __init__(self):
super(WelcomeScreen, self).__init__()
loadUi('screens/welcomescreen.ui', self)
self.CustomerData.clicked.connect(self.go_to_CustomerData)
def go_to_CustomerData(self):
client = Customers.CustomerScreen()
widget.addWidget(client)
widget.setCurrentIndex(widget.currentIndex() + 1)
# main
app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('Icons/MainIcon.png'))
app.setApplicationDisplayName('Bonus Program')
welcome = WelcomeScreen()
widget = QtWidgets.QStackedWidget()
widget.addWidget(welcome)
widget.setFixedWidth(600)
widget.setFixedHeight(475)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
Customers.py
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QDialog, QApplication, QWidget, QMessageBox, QTableWidget, QTableWidgetItem, QTableView
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlQuery
from PyQt5.QtGui import QIcon
import Welcome_Screen as ws
import sqlite3
db_path = 'Bonus_db.sqlite'
class CustomerScreen(QDialog):
def __init__(self):
super(CustomerScreen, self).__init__()
loadUi("screens/customerscreen.ui", self)
self.back_btn.setIcon(QIcon('Icons/Back.png'))
self.back_btn.setIconSize(QtCore.QSize(45, 60))
self.back_btn.clicked.connect(self.go_Back_to_WelcomeScreen)
# self.SoldTos.clicked.connect(self.gotoSoldTos)
def go_Back_to_WelcomeScreen(self):
welcome = ws.WelcomeScreen()
ws.widget.addWidget(welcome)
ws.widget.setCurrentIndex(ws.widget.currentIndex() + 1)
Customer_app = QApplication(sys.argv)
cust_window = CustomerScreen()
cust_window.show()
sys.exit(Customer_app.exec_())

How can I pass my data between my classes on PyQt5?

I made an joystick with qframe class and also i converted my mouse location data to -1,1 but when i try to send this location data to my mainwindow class with pyqt signal it gives an error 'pyqt signal has no attiribute emit' like this, i go to pyqtSignal class
here it is it really dont have emit function but there are many example like this on the net,does my pyqt files broken or something or is it because of absance of QObject class? How can i pass my data to other class?
Here it is my joystick class;
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5.QtCore import QPointF, QTimer, Qt ,pyqtSignal
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtWidgets import QFrame, QApplication, QLabel
import sys
import rospy
from sensor_msgs.msg import Joy
class clock(QFrame):
def __init__(self):
super(clock,self).__init__()
self.setMinimumSize(300,300)
self.setMaximumSize(300,300)
self.x=self.width()/2
self.y=self.height()/2
self.relative_x=0
self.relative_y=0
self.signal=pyqtSignal(int)
def sub(self):
self.node=rospy.init_node("hanınamk",anonymous=True)
rospy.Subscriber('/joy',Joy,self.joyControl)
def joyControl(self,data):
self.data=data.axes
self.x=(-(self.data[0]*150)+150)
self.y=(-(self.data[1]*150)+150)
print(self.data[0],self.data[1])
self.repaint()
def paintEvent(self,event):
pen=QPen()
pen.setWidth(5)
pen.setBrush(Qt.black)
painter=QPainter(self)
painter.setPen(pen)
painter.setRenderHint(QPainter.Antialiasing)
painter.drawEllipse(self.rect())
painter_2=QPainter(self)
painter_2.setBrush(Qt.black)
painter_2.setRenderHint(QPainter.Antialiasing)
painter_2.drawEllipse(QPointF(self.x,self.y),50,50)
def mouseMoveEvent(self,e):
self.relative_x=(e.x()-150)/150.0
self.relative_y=(e.y()-150)/150.0
# print(round(self.relative_x,1),round(self.relative_y,1))
self.signal.emit(self.relative_x)
if self.relative_x**2+self.relative_y**2<1:
self.x=e.x()
self.y=e.y()
self.repaint()
def mousePressEvent(self,e):
if self.relative_x**2+self.relative_y**2<1:
self.x=e.x()
self.y=e.y()
self.repaint()
def mousePressEvent(self,e):
if self.relative_x**2+self.relative_y**2<1:
self.x=e.x()
self.y=e.y()
self.repaint()
def mouseReleaseEvent(self,e):
self.x=self.width()/2
self.y=self.height()/2
self.repaint()
if __name__=="__main__":
app=QApplication(sys.argv)
win=clock()
win.show()
sys.exit(app.exec_())
and my main class;
from PyQt5.QtCore import QRect, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QMouseEvent
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
import sys
from joystick import clock
class Main(QWidget):
def __init__(self):
super(Main,self).__init__()
self.setWindowTitle('Main')
self.setMinimumSize(900,900)
self.setMaximumSize(900,900)
layout=QHBoxLayout()
layout.setGeometry(QRect(200,200,300,300))
self.joy=clock()
self.joy_2=clock()
self.setLayout(layout)
layout.addWidget(self.joy)
layout.addWidget(self.joy_2)
self.label=QLabel(self)
#pyqtSlot(int)
def on_change(self,data):
print(data)
if __name__=="__main__":
app=QApplication(sys.argv)
win=Main()
win.show()
sys.exit(app.exec_())
Make the signal a class attribute not an instance attribute:
class clock(QFrame):
signal = pyqtSignal(int)
def __init__(self):
# ...
and connect it in Main.__init__:
self.joy.signal.connect(self.on_change)
self.joy2.signal.connect(self.on_change)

Categories

Resources