Align qtexttable and qimage side by side in pyqt4 - python

I am currently building a GUI using pyqt, does anyone know how I can align a qtexttable and a qimage next to each other in a qtextdocument. They are presently stacked but I would prefer if they can be inserted side by side. Please find below my code. The table and image are both embedded in the qtextdocument.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib qt5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QWidget):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 780, 533))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.gridLayout_2 = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setObjectName("gridLayout_2")
self.textEdit = QtWidgets.QTextEdit(self.scrollAreaWidgetContents)
self.textEdit.setObjectName("textEdit")
self.textEdit.setReadOnly(True)
self.textEdit.setAcceptRichText(True)
document = QtGui.QTextDocument()
self.textEdit.setDocument(document)
self.gridLayout_2.addWidget(self.textEdit, 0, 0, 1, 1)
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.pushButton = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.vis_plot)
self.gridLayout_2.addWidget(self.pushButton, 1, 0, 1, 1)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
def vis_plot(self):
df = pd.DataFrame(np.random.randn(100,4),columns = list('ABCD'))
ax1 = self.figure.add_subplot(111)
for column in df:
col = pd.DataFrame(df[column])
col_stat = col.apply([np.min, np.max, np.mean, np.median])
col_table = col_stat.rename(index={'amin':'Minimum', 'amax':'Maximum','mean':'Mean', 'median':'Median'})
col_table.reset_index(level=0, inplace=True)
col_tablerender = col_table.rename(columns={col_table.columns[0]:'Metric', col_table.columns[1]:'Value'})
cursor = self.textEdit.textCursor()
cursor.insertTable(len(col_tablerender), 2)
for id, value in col_tablerender.iterrows():
for row in value:
cursor.insertText(str(row))
cursor.movePosition(QtGui.QTextCursor.NextCell)
col.plot.hist(ax=ax1, title=str(column))
self.canvas.draw()
size = self.canvas.size()
width, height = size.width(), size.height()
im = QtGui.QImage(self.canvas.buffer_rgba(), width, height, QtGui.QImage.Format_ARGB32)
cursor = self.textEdit.textCursor()
cursor.insertImage(im)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I would prefer something that looks like this

Related

how would I make the openFileDialog open txt file in this case

I am trying to have
fname = QFileDialog(self,'Source Text','','txt(*.txt)')
run when you call the function openFileDialog
I have tried using a lambda instead of a normal function but that didn't work ethier
I am trying to make a cryptography program and am setting up the basic text upload system
I am currently trying to set fname to the file name
import sys
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QDialog, QApplication, QFileDialog
from tkinter import filedialog
def Encrypt(SourceText):
print (SourceText)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(886, 487)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.Enter = QtWidgets.QPushButton(self.centralwidget)
self.Enter.clicked.connect(self.clicked)
self.Enter.setObjectName("Enter")
self.gridLayout.addWidget(self.Enter, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setLayoutDirection(QtCore.Qt.LayoutDirection.RightToLeft)
self.label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setObjectName("plainTextEdit")
self.gridLayout.addWidget(self.plainTextEdit, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 886, 20))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionUpload = QtGui.QAction(MainWindow)
self.actionUpload.setShortcutContext(
QtCore.Qt.ShortcutContext.WindowShortcut)
self.actionUpload.setMenuRole(QtGui.QAction.MenuRole.AboutQtRole)
self.actionUpload.setObjectName("actionUpload")
self.menuFile.addAction(self.actionUpload)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.actionUpload.triggered.connect(lambda: self.Open("File Uploaded"))
def clicked(self):
Encrypt(self.plainTextEdit.toPlainText())
def openFileDialog(self):
fname = QFileDialog(self,'Source Text','','txt(*.txt)')
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.Enter.setText(_translate("MainWindow", "Enter"))
self.label.setText(_translate(
"MainWindow", "Enter Text Below Or Upload File"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionUpload.setText(_translate("MainWindow", "Upload"))
self.actionUpload.setShortcut(_translate("MainWindow","Ctrl+O"))
def Open(self, text):
openFileDialog()
print (text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec())
You must use QFileDialog().getOpenFileName(parent, caption, path, filter) instead of QFileDialog(parent, caption, path, filter):
import sys
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QDialog, QApplication, QFileDialog
from tkinter import filedialog
def Encrypt(SourceText):
print(SourceText)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(886, 487)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.Enter = QtWidgets.QPushButton(self.centralwidget)
self.Enter.clicked.connect(self.clicked)
self.Enter.setObjectName("Enter")
self.gridLayout.addWidget(self.Enter, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setLayoutDirection(QtCore.Qt.LayoutDirection.RightToLeft)
self.label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setObjectName("plainTextEdit")
self.gridLayout.addWidget(self.plainTextEdit, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 886, 20))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionUpload = QtGui.QAction(MainWindow)
self.actionUpload.setShortcutContext(
QtCore.Qt.ShortcutContext.WindowShortcut)
self.actionUpload.setMenuRole(QtGui.QAction.MenuRole.AboutQtRole)
self.actionUpload.setObjectName("actionUpload")
self.menuFile.addAction(self.actionUpload)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.actionUpload.triggered.connect(lambda: self.Open("File Uploaded"))
def clicked(self):
Encrypt(self.plainTextEdit.toPlainText())
def openFileDialog(self):
fname, _ = QFileDialog().getOpenFileName(self, 'Source Text', '', 'Text files (*.txt)')
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.Enter.setText(_translate("MainWindow", "Enter"))
self.label.setText(_translate(
"MainWindow", "Enter Text Below Or Upload File"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionUpload.setText(_translate("MainWindow", "Upload"))
self.actionUpload.setShortcut(_translate("MainWindow", "Ctrl+O"))
def Open(self, text):
self.openFileDialog()
print(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec())

"Command terminated" when using PyQt5 QtWidgets QGraphicsScene()

I am trying to open an image with PyQt5 QtWidgets QGraphicsView and using QGraphicsScene from the same module. When I am calling QtWidgets.QGraphicsScene.addPixmap(pix) the app closes with the output Command terminated. Below is the code:
import os, sys
from PyQt5 import QtGui, QtCore, QtWidgets
sys.path.insert(0,os.getcwd()+'/'+'gui')
sys.path.insert(0,os.getcwd()+'/'+'src')
from Widget import Ui_MainWindow
from PIL import Image, ImageQt
class Window(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self):
super(Window,self).__init__()
self.setupUi(self)
self.actionOpen.setShortcut("Ctrl+O")
self.actionOpen.setStatusTip("open an image")
self.actionOpen.triggered.connect(self.file_open)
self.show()
def file_open(self):
filepath,_ = QtWidgets.QFileDialog.getOpenFileName(self,'Open File')
if filepath:
pim = Image.open(str(filepath))
qim = ImageQt.ImageQt(pim)
pix = QtGui.QPixmap.fromImage(qim)
self.scene = QtWidgets.QGraphicsScene()
self.scene.addPixmap(pix)
self.view = self.graphicsView.setScene(self.scene)
else:
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())
Additional Info: I used Qt Designer to create Widget.ui file and converted to Widget.py. My machine host is windows and I am running Ubuntu 18.04 in virtualbox. My python version is 2.7. I would really appreciate any help (I spent more than a day on this problem) and can provide any missing info. Thanks
and below is my ui converted to py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Widget.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1115, 689)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(6, 10, 1091, 601))
self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.treeView = QtWidgets.QTreeView(self.horizontalLayoutWidget_2)
self.treeView.setObjectName("treeView")
self.verticalLayout.addWidget(self.treeView)
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.pushButton_4 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_4.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout_2.addWidget(self.pushButton_4, 0, 1, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_2.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout_2.addWidget(self.pushButton_2, 1, 0, 1, 1)
self.pushButton_5 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_5.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_5.setObjectName("pushButton_5")
self.gridLayout_2.addWidget(self.pushButton_5, 1, 1, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton.setObjectName("pushButton")
self.gridLayout_2.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_3 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_3.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout_2.addWidget(self.pushButton_3, 0, 2, 1, 1)
self.pushButton_6 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_6.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_6.setObjectName("pushButton_6")
self.gridLayout_2.addWidget(self.pushButton_6, 1, 2, 1, 1)
self.verticalLayout_3.addLayout(self.gridLayout_2)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.rotation = QtWidgets.QDoubleSpinBox(self.horizontalLayoutWidget_2)
self.rotation.setMinimumSize(QtCore.QSize(100, 20))
self.rotation.setObjectName("rotation")
self.horizontalLayout.addWidget(self.rotation)
self.verticalLayout_3.addLayout(self.horizontalLayout)
self.verticalLayout.addLayout(self.verticalLayout_3)
self.logRun = QtWidgets.QPlainTextEdit(self.horizontalLayoutWidget_2)
self.logRun.setObjectName("logRun")
self.verticalLayout.addWidget(self.logRun)
self.horizontalLayout_2.addLayout(self.verticalLayout)
self.graphicsView = QtWidgets.QGraphicsView(self.horizontalLayoutWidget_2)
self.graphicsView.setMinimumSize(QtCore.QSize(500, 0))
self.graphicsView.setObjectName("graphicsView")
self.horizontalLayout_2.addWidget(self.graphicsView)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.tabWidget = QtWidgets.QTabWidget(self.horizontalLayoutWidget_2)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.verticalLayout_2.addWidget(self.tabWidget)
self.extractedText = QtWidgets.QPlainTextEdit(self.horizontalLayoutWidget_2)
self.extractedText.setObjectName("extractedText")
self.verticalLayout_2.addWidget(self.extractedText)
self.horizontalLayout_2.addLayout(self.verticalLayout_2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1115, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionCtrl_S = QtWidgets.QAction(MainWindow)
self.actionCtrl_S.setObjectName("actionCtrl_S")
self.actionSave_3 = QtWidgets.QAction(MainWindow)
self.actionSave_3.setObjectName("actionSave_3")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionSave_3)
self.menuFile.addSeparator()
self.menuFile.addAction(self.actionExit)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.logRun, self.extractedText)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_4.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "remove black"))
self.pushButton_5.setText(_translate("MainWindow", "PushButton"))
self.pushButton.setText(_translate("MainWindow", "whiten"))
self.pushButton_3.setText(_translate("MainWindow", "PushButton"))
self.pushButton_6.setText(_translate("MainWindow", "PushButton"))
self.label.setText(_translate("MainWindow", "Rotation"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionCtrl_S.setText(_translate("MainWindow", "Ctrl+S"))
self.actionSave_3.setText(_translate("MainWindow", "Save"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Integration of a Radar Graphic into a widget

I tried to integrate the code of this Radar graphic (see link) into a GUI containing a widget.
Instead of plotting x and y into the widget, I would like to plot a radar graphic but I can't solve this problem.
The idea is to replace this piece of code:
x = [2000,2001,2002,2003,2004]
y = [10,20,30,40,50]
self.axes.plot(x, y
With the code from the link above.
Below the code of the back end of my GUI:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QSizePolicy
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from front_end import Ui_MainWindow
class Graph_init(FigureCanvas):
def __init__(self, parent=None):
fig = Figure()
self.axes = fig.add_subplot(111)
self.compute_initial_figure()
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
class Graph_populate(Graph_init):
def compute_initial_figure(self):
x = [2000,2001,2002,2003,2004]
y = [10,20,30,40,50]
self.axes.plot(x, y)
class GUI(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(GUI, self).__init__(parent)
self.setupUi(self)
self.sc = Graph_populate(self.widget)
self.gridLayout.addWidget(self.sc, 0, 1, 1, 1)
if __name__ == '__main__':
app = QApplication(sys.argv)
prog = GUI()
prog.showMaximized()
sys.exit(app.exec_())
and the front end:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(595, 393)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setMaximumSize(QtCore.QSize(100, 16777215))
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setMinimumSize(QtCore.QSize(0, 200))
self.widget.setStyleSheet("background-color: rgb(255, 255, 255);")
self.widget.setObjectName("widget")
self.gridLayout.addWidget(self.widget, 0, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 595, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Thank you
the radar type graph is a graph with polar projection, so when creating the axes you must activate that property.
self.axes = fig.add_subplot(111, projection="polar")
When using the plot function, the first parameter is the angle in radians, and the second the length of the radius.
class Graph_init(FigureCanvas):
def __init__(self, parent=None):
fig = Figure()
self.axes = fig.add_subplot(111, projection="polar")
self.compute_initial_figure()
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
class Graph_populate(Graph_init):
def compute_initial_figure(self):
import math
r =[10, 20, 30, 40, 50]
theta = [0, 50, 90, 120, 180]
angles = [angle*math.pi/180 for angle in theta]
self.axes.plot(angles, r)

Embeding plot into graphicsView in PyQt5

I use pyqtgraph to plot in PyQt5 GUI. I can plot in a pop-up window, but I am trying embed the graph in the graphicsView area.
Here is how I can do a simple plot in a new window:
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
pg.plot(L)
I tried to use this line to embed the plot, but it doesn't work:
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
self.graphicsView.plot(L)
Here is the full code:
import pyqtgraph as pg
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(662, 512)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView.setObjectName("graphicsView")
self.verticalLayout.addWidget(self.graphicsView)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.horizontalLayout.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 662, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
pg.plot(L)#this line plots in a new window
self.graphicsView.plot(L)#this line doesn't work
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Pg.plot returns an instance of the pg.graphicsWindows.PlotWindow class, and this class inherits from QGraphicsView, ie pg.graphicsWindows.PlotWindow is used to plot but QGraphicsView is not. If you want to embed a plot within the Widget you must use the class pg.PlotWidget(), you must change:
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
to:
self.graphicsView = pg.PlotWidget(self.centralwidget)
and:
def btn_clk(self):
L = [1,2,3,4,5]
self.graphicsView.plot(L)#this line doesn't work
Output:

Menubar created by PyQt Designer

i designed in PyQt-Designer a surface and converted it to a .py
No I´m trying to link a function to a menubar-(button). There is something like open, save, close....
I have tried a lot, but unsuccessfully, i hope you can help me to connect a simple function to the open-button in the menu bar.
for example a function that is linked to the menubar
menubar->open->function(open_path):
def open_path():
root= Tk()
Pfad=askdirectory()
root.destroy
Thank you for your help!
Here is the Code:
from PyQt5 import QtCore, QtGui, QtWidgets
import os
from tkinter import *
from tkinter.filedialog import askdirectory
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Auswertung Brechzahlbestimmung")
MainWindow.resize(1205, 641)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_5 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_5.setObjectName("gridLayout_5")
self.gridLayout_3 = QtWidgets.QGridLayout()
self.gridLayout_3.setObjectName("gridLayout_3")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.histogram = QtWidgets.QGraphicsView(self.centralwidget)
self.histogram.setMinimumSize(QtCore.QSize(182, 126))
self.histogram.setMaximumSize(QtCore.QSize(16777215, 126))
self.histogram.setObjectName("histogram")
self.verticalLayout_2.addWidget(self.histogram)
self.gridLayout_3.addLayout(self.verticalLayout_2, 2, 2, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout_3.addWidget(self.label, 0, 2, 1, 1)
self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.gridLayout_3.addWidget(self.horizontalSlider, 2, 0, 1, 1)
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setObjectName("label_4")
self.gridLayout_3.addWidget(self.label_4, 0, 0, 1, 1)
self.gridLayout_4 = QtWidgets.QGridLayout()
self.gridLayout_4.setObjectName("gridLayout_4")
self.gridLayout_3.addLayout(self.gridLayout_4, 1, 4, 1, 1)
self.seitenansicht = QtWidgets.QGraphicsView(self.centralwidget)
self.seitenansicht.setMinimumSize(QtCore.QSize(537, 407))
self.seitenansicht.setObjectName("seitenansicht")
self.gridLayout_3.addWidget(self.seitenansicht, 1, 2, 1, 1)
self.verticalSlider = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider.setObjectName("verticalSlider")
self.gridLayout_3.addWidget(self.verticalSlider, 1, 1, 1, 1)
self.draufsicht = QtWidgets.QGraphicsView(self.centralwidget)
self.draufsicht.setMinimumSize(QtCore.QSize(537, 407))
self.draufsicht.setObjectName("draufsicht")
self.gridLayout_3.addWidget(self.draufsicht, 1, 0, 1, 1)
self.referenz = QtWidgets.QGraphicsView(self.centralwidget)
self.referenz.setMinimumSize(QtCore.QSize(70, 0))
self.referenz.setMaximumSize(QtCore.QSize(70, 16777215))
self.referenz.setObjectName("referenz")
self.gridLayout_3.addWidget(self.referenz, 1, 3, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout_3.addWidget(self.label_2, 0, 3, 1, 1)
self.gridLayout_5.addLayout(self.gridLayout_3, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
#Here begins the Menubar
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1205, 20))
self.menubar.setObjectName("menubar")
self.menuDatei = QtWidgets.QMenu(self.menubar)
self.menuDatei.setObjectName("menuDatei")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionSave = QtWidgets.QAction(MainWindow)
self.actionSave.setObjectName("actionSave")
self.actionExport_Picture = QtWidgets.QAction(MainWindow)
self.actionExport_Picture.setObjectName("actionExport_Picture")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.menuDatei.addAction(self.actionSave)
self.menuDatei.addAction(self.actionOpen)
self.menuDatei.addSeparator()
self.menuDatei.addAction(self.actionExport_Picture)
self.menuDatei.addSeparator()
self.menuDatei.addAction(self.actionExit)
self.menubar.addAction(self.menuDatei.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Auswertung Brechzahlbestimmung"))
self.label.setText(_translate("MainWindow", "Schnitt"))
self.label_4.setText(_translate("MainWindow", "Draufsicht"))
self.label_2.setText(_translate("MainWindow", "n"))
self.menuDatei.setTitle(_translate("MainWindow", "Datei"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionSave.setText(_translate("MainWindow", "Save"))
self.actionExport_Picture.setText(_translate("MainWindow", "Export Picture"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
First of all I recommend you read the docs, it recommends not to modify the class generated by Qt Designer, but you must create another class that inherited from a widget and use the class provided by Qt Designer as an interface.
On the other hand it is not necessary to use tkinter, Qt provides widgets to obtain directory paths like QFileDialog.
And finally you have to use the triggered signal of self.actionOpen.
Considering the above the solution is:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
# ...
def retranslateUi(self, MainWindow):
# ...
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.actionOpen.triggered.connect(self.open_file)
#QtCore.pyqtSlot()
def open_file(self):
fdirectory = QtWidgets.QFileDialog.getExistingDirectory(self, "Open Directory")
if fdirectory:
print(fdirectory)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Categories

Resources