How do I prevent the QPushButton.clicked from the widget from executing when I'm in MainWindow pyqt5? - pyqt5

I set the printInWidget clicked method for the btnprint button in widget1. But I want to replace that command in MainWindow with command printInMain by code: self.wid.btnprint.clicked.connect(self.printInMain). I don't know why it does both commands in MainWindow. Please help me make the command printInWidget not execute when running MainWidow.
Sorry everyone (English is not my native language and I only approached pyqt5 for a few months by teaching myself).
when I click button "printText"
my code:
mainwindow.py
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QTextEdit, QGridLayout, QVBoxLayout, QFileDialog,QHBoxLayout,QSpacerItem,QSizePolicy
from PyQt5 import uic, QtCore
import sys
from widget1 import *
class UI(QMainWindow):
def __init__(self):
super(UI, self).__init__()
uic.loadUi("MainWindow.ui", self)
self.wid = widget1()
self.verticalLayout.addWidget(self.wid)
self.wid.btnprint.clicked.connect(self.printInMain)
# I want to override method of button printText
def printInMain(self):
self.wid.labelB.setText('New text (method in Main)')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = UI()
window.show()
app.exec_()
widget1.py
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from PyQt5.QtGui import QIcon
from PyQt5 import uic
class widget1(QWidget):
def __init__(self):
super().__init__()
uic.loadUi('widget1.ui',self)
self.btnprint.clicked.connect(self.printInwidget)
def printInwidget(self):
self.labelA.setText('Hello World (Method in Widget)')
if __name__ == '__main__':
app = QApplication(sys.argv)
Main = QMainWindow()
widget = widget1()
widget.show()
sys.exit(app.exec_())

You need to disconnect. If you want to use the initial behaviour again you need to connect the signal to the slot again. I would suggest creating a reconnect() function that will do that for you if you decide to do the whole thing many times.
Here is an example.
More info can be found in the official QObject documentation (look for bool QObject::disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)).
Also try to provide full code or at least a minimal working example. What you have posted right now cannot work due to missing dependencies and a UI file.

Related

PyQt6 QWebEngineView can't load PPAPI flash player

I want to make a custom client for an online flash game
I am on Linux Manjaro KDE
I installed PPAPI using sudo pamac install pepper-flash
the result of this code
from PyQt6.QtCore import QUrl
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from PyQt6.QtWebEngineWidgets import *
from PyQt6.QtWebEngineCore import QWebEngineSettings
import sys
sys.argv += ['--webEngineArgs',
"--register-pepper-plugins=/usr/lib/PepperFlash/libpepflashplayer.so;application/x-shockwave-flash",
"--ppapi-flash-path=/usr/lib/PepperFlash/libpepflashplayer.so", '--ppapi-flash-version=32.21.0.153']
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow,self).__init__(*args, **kwargs)
self.browser = QWebEngineView()
self.setCentralWidget(self.browser)
self.browser.settings().setAttribute(QWebEngineSettings.WebAttribute.PluginsEnabled, True)
self.browser.settings().setAttribute(QWebEngineSettings.WebAttribute.JavascriptEnabled, True)
self.browser.setUrl(QUrl("https://webbrowsertools.com/test-flash-player/"))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
is "This plugin is not supported"
What am I missing ???
once, I managed to run flash on PyQt5 QWebEngineView
but now the web page on PyQt5 is not even rendering. it is blank. I don't know why, it is the same code that worked before.
EDIT:
I managed to get PyQt5 QWebEngineView working again.
see https://stackoverflow.com/a/74325318/10701585

How to track pyqt5 crash errors?

I use QWebEngineView to log in to the telegram web.
I would like to be able to save the session in the profile.
When I try to load a profile with an authorized session, my application abruptly terminates.
Is there anything I can do to track the errors that occurred during startup?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtCore import *
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.browser = QWebEngineView()
profile = QWebEngineProfile(f"profile", self.browser)
webpage = QWebEnginePage(profile, self.browser)
self.browser.setPage(webpage)
self.browser.load(QUrl('https://web.telegram.org/z'))
self.browser.show()
if __name__ == "__main__":
MyApp = QApplication(sys.argv)
window = Window()
MyApp.exec_()

pyqt5 - Problem switching back from QDialog window to QMainWindow

Hope indention is OK. I removed the reference to the current instance but the issue is still there. Closing the QDialog window with 'x' the MainWindow is displayed for a second and then it close out, while pressing the 'Cancel' button in the QDialog window it works fine.
Thanks for any suggestion
'''
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import QApplication,QWidget, QMainWindow, QDialog
from MainW import Ui_f_main
from ShowDialogUi import Ui_f_Dialog
class MainWindow(QWidget, Ui_f_main):
def __init__(self):
super().__init__()
self.setupUi(self)
self.wDiag = None
self.pbDialog.clicked.connect(self.dialog)
def dialog(self):
if self.wDiag is None:
window.close()
self.wDiag=ShowDialog()
self.wDiag.setAttribute(Qt.WA_DeleteOnClose)
self.wDiag.destroyed.connect(self.resetFlag_wDiag)
self.wDiag.show()
self.wDiag.pbOK.clicked.connect(self.go_on)
self.wDiag.pbCancel.clicked.connect(self.go_back)
else:
self.wDiag.close()
self.wDiag=None
def go_on(self):
print("OK")
def go_back(self):
window.show()
self.wDiag.close()
self.wDiag=None
def resetFlag_wDiag(self):
window.show()
self.wDiag=None
class ShowDialog(QDialog,Ui_f_Dialog):
def __init__(self):
super().__init__()
self.setupUi(self)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
'''

How to embed an URxvt terminal in a Pyqt5 GUI?

I have constructed a GUI in QtChooser and the code for it is written in PyQt5. I have a total of 7 tabs in the GUI, each of which are defined in the QMainWindow class of my code. These definitions contain the codes for each TextEdit, LineEdit, PushButtons, RadioButtons, etc.
However, in one the the tabs, I want to embed an external terminal which will open when a particular PushButton is clicked within that tab. I was able to open the Urxvt terminal when the RadioButton is toggled. The issue I'm facing now is to open the terminal specifically in the area of the TextEdit. This is how the original GUI (built in the QtDesigner looks like. I need the terminal to open in the TextEdit below the Output label. But, this is how the terminal opens in the GUI when the code is run
This is a part of the updated code:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QMessageBox, QAction
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt
import sys
import platform
import os
import subprocess
import time
import re
import textwrap
class EmbTerminal(QtWidgets.QWidget):
def __init__(self, parent=None):
super(EmbTerminal, self).__init__()
self.process = QtCore.QProcess(self)
self.terminal = QtWidgets.QWidget(self)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.terminal)
# Works also with urxvt:
self.process.start('urxvt',['-embed', str(int(self.winId())), '-bg', '#000000', '-fg', '#ffffff'])
self.setFixedSize(539, 308)
class Ui_Dialog(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_Dialog, self).__init__()
#Load GUI from QT5 Designer
uic.loadUi("S1_mainwindow.ui", self)
def openTerminalCheckBox (self):
if self.openTerminalRMachineRadioButton.isChecked():
status = True
self.commandLineRemoteCommandRMachineLineEdit.setDisabled(True)
self.commandlineRemoteCommandRMachineLabel.setDisabled(True)
self.executeRemoteCommandRMachinePushButton.setDisabled(True)
self.remoteMachineOutputLabel.setText("Terminal")
self.outputRMachineTextEdit = QtWidgets.QTabWidget()
self.gridLayout_6.addWidget(self.outputRMachineTextEdit)
self.outputRMachineTextEdit.addTab(EmbTerminal(), "EmbTerminal")
else:
status = False
app = QtWidgets.QApplication(sys.argv) # Create an instance of QtWidgets.QApplication
window = Ui_Dialog()
main = mainWindow()
main.show() # Create an instance of our class
app.exec_()
I need to open the terminal specifically in the QTextEdit which is already been defined in that tab. Do you guys have any suggestions/input?

how to make multi window using pyqt5

everybody!
Here's what I'm trying to do:
First, I created a file called basic.ui with a pushbutton.
Press the pushbutton to set the path and select the picture file in the set path to want the picture to appear in a new window(another window).
Let me tell you about the part where I am having difficulty.
The command to blow up the file is executed. Selecting the file causes an error.
I wonder what the cause is the problem.
How do I get the picture to appear in a new window(another window) after pressing the button?????
MainActivity.python
import sys
try:
from PyQt5.QtCore import Qt, QT_VERSION_STR
from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import QApplication, QFileDialog
except ImportError:
try:
from PyQt4.QtCore import Qt, QT_VERSION_STR
from PyQt4.QtGui import QImage, QApplication, QFileDialog
except ImportError:
raise ImportError("Requires PyQt5 or PyQt4.")
from QtImageViewer import QtImageViewer
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import uic
from PyQt5.QtCore import pyqtSlot
class From(QtWidgets.QDialog):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.ui=uic.loadUi("basic.ui",self)
self.ui.show()
#pyqtSlot()
def add_number(self):
app = QApplication(sys.argv)
viewer=QtImageViewer()
fileName, dummy = QFileDialog.getOpenFileName(None, "Open image file...")
image = QImage(fileName)
viewer.setImage(image)
viewer.show()
sys.exit(app.exec_())
if __name__=='__main__':
app=QtWidgets.QApplication(sys.argv)
w=From()
sys.exit(app.exec())
Signals & Slots
Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. Signals and slots are made possible by Qt's meta-object system.
http://doc.qt.io/qt-5/signalsandslots.html
Try it:
import sys
from os import getcwd
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import uic
from PyQt5.QtCore import Qt, pyqtSlot
class From(QtWidgets.QDialog):
def __init__(self, parent=None):
super(From, self).__init__(parent)
self.ui=uic.loadUi("basic.ui",self)
self.ui.pushButton.clicked.connect(self.add_number) # +++
# ^^^^^^^^^^ vvvvvvvvvv
# <widget class="QPushButton" name="pushButton"> <---> "basic.ui"
self.ui.labelImagen = QtWidgets.QLabel()
self.ui.show()
#pyqtSlot()
def add_number(self):
fileName, dummy = QtWidgets.QFileDialog.getOpenFileName(
self, "Open image file...", getcwd(),
"Image (*.png *.jpg)",
options=QtWidgets.QFileDialog.Options())
if fileName:
pix = QtGui.QPixmap(fileName)
self.ui.labelImagen.setPixmap(pix)
self.ui.labelImagen.show()
if __name__=='__main__':
app=QtWidgets.QApplication(sys.argv)
w=From()
sys.exit(app.exec())