how to make multi window using pyqt5 - 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())

Related

How to connect using menubar in pyqt5?

I want to open a picture when I click the menu, but I don't know how
I want to be able to output the specified image file when clicking the submenu.
import sys
from PyQt5.QtWidgets import QMainWindow, QMenu, QApplication, QLabel
from PyQt5.QtGui import QPixmap, QIcon,QAction,*
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
menubar= self.menubar()
self.filemenu = menubar.addmenu('a')
x= QMenu('b',self)
y= QMenu('c',self)
self.filemenu.addmenu(y)
self.x1 =QAction('d',self)
x.addMenu(self.x1)
self.x1.triggered.connect(self.fileopen)
def fileopen(self):
qpixmapvar=QPixmap()
qpixmapvar.load("C:\\desktop\\A.png")
self.label.setPixmap(qPixmapvar)
self.label.resize(qPixmapvar.width(), qPixmapvar.height())
if__name__ =='__main__':
app= Qapplication(sys.argv)
ex = Example()
sys.exit(app.exec())

How do I prevent the QPushButton.clicked from the widget from executing when I'm in MainWindow 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.

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?

matplotlib and transparency figure

I am working with the matplotlib library and PyQt5 with Python 3.6. I add a figure in a window I create, and I wish to set transparent the background of this figure because I add an image to the background of the window. But, the figure is not really transparent, it duplicates the background image of the window.
For example, someone deals with the same problem two years ago :
matplotlib and pyqt4 transparent background
Here is a working example (with a background which is black but the figure is not black) :
import sys, os
from PyQt5.QtCore import Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import matplotlib
matplotlib.use('Qt5Agg') # Make sure that we are using QT5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
class SecondWindow(QWidget):
def __init__(self, parent=None):
super(SecondWindow, self).__init__(parent)
self.setupUi(self)
def setupUi(self, Form):
# WINDOW SETTINGS
Form.setWindowTitle('Hello')
self.p = QPalette()
self.pixmap = QPixmap(os.getcwd() + "/logo.png").scaled(self.size(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
self.p.setBrush(QPalette.Background, QBrush(self.pixmap))
self.setPalette(self.p)
# CREATE FIGURE AND SETTINGS
self.figure = plt.figure()
self.figure.patch.set_facecolor('None')
self.figure.patch.set_alpha(0)
self.canvas = FigureCanvas(self.figure)
self.axes = self.figure.add_subplot(111)
# WINDOW LAYOUT (with H1 and H2)
self.setLayout(QVBoxLayout())
self.layout().addWidget(self.canvas,1)
self.layout().setContentsMargins(50, 50, 50, 50)
if __name__ == '__main__':
app = QApplication(sys.argv)
form = SecondWindow()
form.show()
sys.exit(app.exec_())
I search for answer during long hours but didn't find a solution yet. Thanks for any help you can bring !
Operating System: Windows 7 Pro
Matplotlib Version: 2.0.2 (installed via Anaconda, conda install matplotlib --channel conda-forge)
Python Version: Python 3.6
Anaconda 3
The problem occurs because the background image is set as a palette to the widget. This causes the canvas to inherit the palette and hence the canvas will also have the image as background, somehow overlaying the widget's background.
A solution would be to set the background of the canvas transparent. An easy way to do so are style sheets.
self.canvas.setStyleSheet("background-color:transparent;")
Note that this is not the same as setting the patches' facecolor to none. The figure has a background, which is controlled inside matplotlib, but the canvas, being a PyQt object also has a background.
Complete example:
import sys, os
from PyQt4.QtCore import Qt
from PyQt4.QtGui import *
import matplotlib
matplotlib.use('Qt4Agg')
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
plt.rcParams['xtick.color'] ="w"
plt.rcParams['ytick.color'] ="w"
plt.rcParams['font.size'] = 14
class SecondWindow(QWidget):
def __init__(self, parent=None):
super(SecondWindow, self).__init__(parent)
# CREATE FIGURE AND SETTINGS
self.figure = plt.figure()
self.figure.patch.set_facecolor("None")
self.canvas = FigureCanvas(self.figure)
self.axes = self.figure.add_subplot(111)
self.axes.patch.set_alpha(0.5)
###### Make the background of the canvas transparent
self.canvas.setStyleSheet("background-color:transparent;")
self.p = QPalette()
self.p.setBrush(QPalette.Background, QBrush(QPixmap("house.png")))
self.setPalette(self.p)
self.setLayout(QVBoxLayout())
self.layout().addWidget(self.canvas,1)
self.layout().setContentsMargins(50, 50, 50, 50)
if __name__ == '__main__':
app = QApplication(sys.argv)
form = SecondWindow()
form.show()
sys.exit(app.exec_())
which might then look like

PyQt4 & BeautifulSoup setting the Browser Window Size

I'm attempting to use PyQt4 to do some web scraping, but the site I'm attempting to scrape keeps thinking I'm a mobile device, and is not presenting the dataset available to a desktop or laptop (even though I'm using a Mozilla/5.0 user agent).
To try to find out why I'm setting my URL to "whatsmyuseragent.com". And I notice that it's telling me that although my Screen Resolution is 1920px x 1080px my Browser Window Size is 0px x 0px, so could this be the problem?
Here is my code below. Any suggestions on what I need to change to convince the site I'm scraping to believe I'm a desktop or laptop (rather than a mobile) would be appreciated. Thanks.
import sys
from PyQt4 import QtGui
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebPage
from PyQt4.QtNetwork import QNetworkRequest
import bs4 as bs
import urllib.request
class Client(QWebPage):
def __init__ (self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self.on_page_load)
self.request = QNetworkRequest()
self.request.setUrl(QUrl(url))
self.request.setRawHeader("User-Agent",'Mozilla/5.0')
self.mainFrame().load(self.request)
self.app.exec_()
def on_page_load (self):
self.app.quit()
url = 'http://www.whatsmyuseragent.com'
client_response = Client(url)
source = client_response.mainFrame().toHtml()
soup = bs.BeautifulSoup(source, 'lxml')
print(soup.prettify())
Try to set the Viewport Size:
import sys
import re
from PyQt4 import QtGui
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl, QSize
from PyQt4.QtWebKit import QWebPage
from PyQt4.QtNetwork import QNetworkRequest
import bs4 as bs
class Client(QWebPage):
def __init__ (self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
# good ol'size
size = QSize(640, 480)
self.setViewportSize(size)
self.loadFinished.connect(self.on_page_load)
self.request = QNetworkRequest()
self.request.setUrl(QUrl(url))
self.request.setRawHeader("User-Agent",'Mozilla/5.0')
self.mainFrame().load(self.request)
self.app.exec_()
def on_page_load (self):
self.app.quit()
url = 'http://www.whatsmyuseragent.com'
client_response = Client(url)
source = client_response.mainFrame().toHtml()
soup = bs.BeautifulSoup(source, 'lxml')
# some meat from the soup
print(re.sub('\s+', ' ', soup.find(class_='browser-window').text))
This produce the following for me:
625 px x 465 px