I am facing a problem interacting with the MatplotlibWidget that I creater via Qt Designer. I am unable to change the axes labels, scale, provide titles or anything. Am i doing anything wrong?
This is a sample UI code generated using Qt Designer:
from PyQt4 import QtCore, QtGui
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.mplwidget = MatplotlibWidget(self.centralwidget)
self.mplwidget.setGeometry(QtCore.QRect(70, 50, 400, 300))
self.mplwidget.setObjectName("mplwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
from matplotlibwidget import MatplotlibWidget
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None, f=QtCore.Qt.WindowFlags()):
QtGui.QMainWindow.__init__(self, parent, f)
self.setupUi(self)
This is the python code i wrote to interact with the UI python Code:
from PyQt4 import QtGui, QtCore
from TestUI2 import MainWindow
class Window(MainWindow):
def __init__(self):
MainWindow.__init__(self)
x=[0,10,100]
y=[3,4,5]
self.mplwidget.axes.set_xscale('log') # Nothing Happens
self.mplwidget.axes.set_title('GRAPH') # Nothing Happens
self.mplwidget.axes.plot(x,y)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
You need to define your own class for MatplotlibWidget first (in your main script, not the UI code) and then instantialize it in Window() definintion. To do that you also need to import FigureCanvas from _qt4agg matplotlib backend module and Figure class:
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as Canvas
from matplotlib.figure import Figure
Then in the Matplotlib class definition you create instances of the Figure subplots and Figure canvas, that can then be used to interact with your plot from the main window.
So using your code this would look like this:
from PyQt4 import QtGui, QtCore
from TestUI2 import MainWindow
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as Canvas
from matplotlib.figure import Figure
class Window(MainWindow):
def __init__(self):
MainWindow.__init__(self)
x=[0,10,100]
y=[3,4,5]
self.mplwidget = MatplotlibWidget(self.centralwidget)
self.mplwidget.setGeometry(QtCore.QRect(70, 50, 600, 500))
self.mplwidget.setObjectName("mplwidget")
self.mplwidget.plotDataPoints(x,y)
class MatplotlibWidget(Canvas):
def __init__(self, parent=None, title='Title', xlabel='x label', ylabel='y label', dpi=100, hold=False):
super(MatplotlibWidget, self).__init__(Figure())
self.setParent(parent)
self.figure = Figure(dpi=dpi)
self.canvas = Canvas(self.figure)
self.theplot = self.figure.add_subplot(111)
self.theplot.set_title(title)
self.theplot.set_xlabel(xlabel)
self.theplot.set_ylabel(ylabel)
def plotDataPoints(self, x, y):
self.theplot.plot(x,y)
self.draw()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
And the Ui script:
from PyQt4 import QtCore, QtGui
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None, f=QtCore.Qt.WindowFlags()):
QtGui.QMainWindow.__init__(self, parent, f)
self.setupUi(self)
And then if you want to interact with the mpl plot, for example to change a title you do it from within the Window class, say:
self.mplwidget.theplot.set_title("New Title")
Hope that helps.
You only need to add the line:
self.mplwidget.draw()
in your:
class Window(MainWindow):
def __init__(self):
There is no need to manually define the class, you can just use the MatplotlibWidget you created with the Qt Designer.
Based on embert's answer, regarding to the code in the original question, the following is tested and worked:
Either
x=[0,10,100]
y=[3,4,5]
self.matplotlibwidget.axes.plot(x,y)
self.matplotlibwidget.axes.set_xscale('log')
self.matplotlibwidget.axes.set_title('GRAPH')
Or
x=[0,10,100]
y=[3,4,5]
self.matplotlibwidget.axes.set_xscale('log')
self.matplotlibwidget.axes.set_title('GRAPH')
self.matplotlibwidget.axes.hold(True)
self.matplotlibwidget.axes.plot(x,y)
You call self.mplwidget.axes.plot(x,y) after you set the properties.
hold (False): if False, figure will be cleared each time plot is called
Therefore change
self.mplwidget = MatplotlibWidget(self.centralwidget)
to
self.mplwidget = MatplotlibWidget(self.centralwidget, hold=True)
or set the properties after calling plot
Related
I have recreated a problem I am encountering as a minimal example below.
The situation: I have two Qt Designer generated GUI, each being called by their own separated scripts. A third script is meant to collect information from the first script upon the click of a button on the second script. I does not, yet there is no errors.
I have also attempted to solve this by using signals, but these does not seem to communicate between scripts. I provided a simpler version here that doesn't use signals per se.
My question is: How do You get a third script to handle information of two other scripts related to GUIs in pyqt5 ?
Here is the minimal example:
The first GUI script:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(504, 223)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.TypeHere = QtWidgets.QTextEdit(self.centralwidget)
self.TypeHere.setObjectName("TypeHere")
self.verticalLayout.addWidget(self.TypeHere)
self.HelloButton = QtWidgets.QPushButton(self.centralwidget)
self.HelloButton.setObjectName("HelloButton")
self.verticalLayout.addWidget(self.HelloButton)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 504, 22))
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.HelloButton.setText(_translate("MainWindow", "Say hello"))
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_())
The second GUI script:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(282, 392)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton01 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton01.setObjectName("pushButton01")
self.verticalLayout.addWidget(self.pushButton01)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 282, 22))
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.pushButton01.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_())
JustSomeTextv01, the script of the first GUI:
from PyQt5 import QtWidgets
from PyQt5.QtCore import QProcess, QThreadPool
from TypingUIv01 import Ui_MainWindow
import JustSomeButtonsv01 as JSB
import sys
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.HelloButton.pressed.connect(self.openButtons)
self.Display = JSB.Window()
self.ButtonsThread()
def openButtons(self):
self.Display.show()
def ButtonsThread(self):
self.threadpoolbutt = QThreadPool()
self.threadpoolbutt.start(self.runButtons)
def runButtons(self):
self.butt = QProcess()
print("Buttons Running")
self.butt.execute('python',['JustSomeButtonsv01.py'])
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
JustSomeButtonsv01, the script of the second GUI:
from PyQt5 import QtWidgets
from PyQt5.QtCore import QProcess, QThreadPool
from ButtonsUIv01 import Ui_MainWindow
# import JustSomeRecordv01 as JSR
import sys
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.RecordThread()
def RecordThread(self):
self.threadpoolrec = QThreadPool()
self.threadpoolrec.start(self.runRecord)
def runRecord(self):
self.rec = QProcess()
print("Record Running")
self.rec.execute('python',['JustSomeRecordv01.py'])
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
# window.show()
sys.exit(app.exec())
And finally, JustSomeRecordv01, the third script trying to interact with the other two:
from PyQt5 import QtWidgets
import sys
from TypingUIv01 import Ui_MainWindow as JSTWin
from ButtonsUIv01 import Ui_MainWindow as ButtWin
class Record(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# self.setupUi(self)
app2 = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
self.Win = JSTWin()
self.Win.setupUi(MainWindow)
self.Text = self.Win.TypeHere.toPlainText()
print("Running")
self.Butt = ButtWin()
self.Butt.setupUi(MainWindow)
self.Butt.pushButton01.pressed.connect(self.PrintIT)
def PrintIT(self):
print("Texting")
print(self.Text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Record()
# window.show()
sys.exit(app.exec())
How to reproduce the problem:
You execute the JustSomeTextv01 script. Press the "Hello Button" and a second window will show up. You type anything in the QTextEdit of the first window and then click the button of the second window. The intent is that this second button would print what You wrote, but it doesn't work.
Thank You for your time,
I managed to do it within the example scripts, but the only solution was to put everything not-GUI into the same script as so:
# To test textbox related function
from PyQt5 import QtWidgets, QtCore
from TypingUIv01 import Ui_MainWindow
from ButtonsUIv01 import Ui_MainWindow as Ui2
import sys
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
PatchSignal = QtCore.pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.HelloButton.pressed.connect(self.openButtons)
self.PatchSignal.connect(self.printIT)
def openButtons(self):
self.w2 = Wintwo()
self.w2.show()
def printIT(self):
self.Text = self.TypeHere.toPlainText()
# print("PRINTING")
print(self.Text)
class Wintwo(QtWidgets.QMainWindow, Ui2):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.pushButton01.pressed.connect(self.Emit)
self.emitter = window
def Emit(self):
# print("EMITTING")
self.emitter.PatchSignal.emit()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
It seems sending Signals between scripts is simply not possible in pyqt5!
Somehow this doesn't work in my actual script since "window" isn't recognized, but that's another question for another day.
I'm new at python and i need some help ;-)
I created a window with a label with the QT designer en generated the py file (window.py):
'''
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(847, 283)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lblMain = QtWidgets.QLabel(self.centralwidget)
self.lblMain.setGeometry(QtCore.QRect(160, 60, 311, 51))
self.lblMain.setObjectName("lblMain")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 847, 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.lblMain.setText(_translate("MainWindow", "label main window"))
'''
I created main.py which calls the window:
from PyQt5 import QtWidgets,QtGui
from window import Ui_MainWindow
class window(QtWidgets.QMainWindow):
def __init__(self):
super(window, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = window()
win.show()
sys.exit(app.exec_())
So far so good.
From within main.py i can set the text of the label using:
self.ui.lblMain.setText('some text')
This works also.
Now i would like to create another file with another class which can update the label.
class update.py:
from PyQt5 import QtWidgets,QtGui
from window import Ui_MainWindow
class window(QtWidgets.QMainWindow):
def __init__(self):
super(window, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def settext(self):
self.ui.lblMain.setText('updated')
And here i'm stuck.
Can anyone give a hand ?
Cheers John
A function to update a label no matter where it came from:
def update_label(label, new_text):
label.setText(new_text)
You can save this function anywhere you like including inside a new class. The function or the new class don't need to know anything about the ui to do this.
If it's in a class you'll have to create an instance of that class before using it.
The class you show should work perfectly, simply call the function settext somewhere.
Under update.py:
class Updater:
def __init__(self, label):
self.label = label
def settext(self):
self.label.setText('updated')
Notice that this class is not another window, it accepts the label object from the Ui_MainWindow class as an argument and saves it as a property. To use it add under main, after you create the app:
my_instance = Updater(win.ui.lblMain)
my_instance.settext()
You could even pass the whole win.ui as an argument. As long as all the classes share the same instance of ui then they will change the same widgets
For completeness, a full program that uses the updater class to change the text in the GUI:
from PyQt5 import QtWidgets,QtGui
from window import Ui_MainWindow
class window(QtWidgets.QMainWindow):
def __init__(self):
super(window, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Updater:
def __init__(self, label):
self.label = label
def settext(self):
self.label.setText('updated')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = window()
win.show()
my_instance = Updater(win.ui.lblMain)
my_instance.settext()
sys.exit(app.exec_())
I have a set of routines that essentially mirror the solution found here, except that I have my plot function in its own class that I import. I do this instead of leaving the code as a method within the MyApp class as the solution has in the given link. I make the code to plot the data in its own file because the actual code is very verbose.
My goal: I am trying to make it such that clicking on the graph will change a label in the GUI.
I've tried researching signals/slots and inheritance, but can't grasp how they can connect or apply when a module load is involved. Keep in mind I'm creating the GUI in PyQtDesigner.
Here is my code
main.py
import sys
from PyQt5.QtWidgets import QMainWindow
from PyQt5 import QtWidgets
from timeseries import plotData
import design
class MyApp(QMainWindow, design.Ui_MainWindow, plotData):
def __init__(self, parent=None):
super(self.__class__, self).__init__()
self.setupUi(self)
self.x = [1,2,3,4,5]
self.y = [1,2,3,4,5]
# Button click plots the timeseries in the first timeseries plot widget
self.pushButton_plotData.clicked.connect(lambda: self.plot())
if __name__ == '__main__':
# Create GUI application
app = QtWidgets.QApplication(sys.argv)
form = MyApp()
form.show()
app.exec_()
mplwidget.py
from PyQt5 import QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
import matplotlib
# Ensure using PyQt5 backend
matplotlib.use('QT5Agg')
# Matplotlib canvas class to create figure
class MplCanvas(Canvas):
def __init__(self):
self.fig = Figure(dpi=100)
self.ax = self.fig.add_subplot(111)
Canvas.__init__(self, self.fig)
Canvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
Canvas.updateGeometry(self)
# Call when user clicks in the plot window
Canvas.mpl_connect(self, s='button_press_event', func=self.get_coord)
def get_coord(self, event):
if event.button == 1:
# Get the x coordinate of the mouse cursor in data coordinates
ix = event.xdata
print(ix)
# GOAL: Want to be able to execute:
# self.textBrowser_X1.setText(str(ix[0]))
# where textBrowser_X1 is the name of a line edit widget
# Matplotlib widget
class MplWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent) # Inherit from QWidget
self.canvas = MplCanvas() # Create canvas object
self.vbl = QtWidgets.QVBoxLayout() # Set box for plotting
self.vbl.addWidget(self.canvas)
self.setLayout(self.vbl)
timeseries.py
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
class plotData(FigureCanvas):
def __init__(self, parent=None):
super().__init__()
def plot(self):
self.plotWidget.canvas.ax.plot(self.x, self.y)
self.plotWidget.canvas.draw()
design.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1216, 379)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_plotData = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_plotData.setGeometry(QtCore.QRect(216, 20, 96, 33))
font = QtGui.QFont()
font.setPointSize(14)
self.pushButton_plotData.setFont(font)
self.pushButton_plotData.setObjectName("pushButton_plotData")
self.plotWidget = MplWidget(self.centralwidget)
self.plotWidget.setGeometry(QtCore.QRect(20, 75, 1177, 234))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.plotWidget.sizePolicy().hasHeightForWidth())
self.plotWidget.setSizePolicy(sizePolicy)
self.plotWidget.setObjectName("plotWidget")
self.textBrowser_X1 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_X1.setGeometry(QtCore.QRect(132, 21, 75, 30))
self.textBrowser_X1.setObjectName("textBrowser_X1")
self.label_X1 = QtWidgets.QLabel(self.centralwidget)
self.label_X1.setGeometry(QtCore.QRect(25, 22, 170, 25))
font = QtGui.QFont()
font.setPointSize(14)
self.label_X1.setFont(font)
self.label_X1.setObjectName("label_X1")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1216, 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_plotData.setText(_translate("MainWindow", "Plot Data"))
self.label_X1.setText(_translate("MainWindow", "X position 1"))
from mplwidget import MplWidget
I see that there are many redundant things. For example because it is necessary that PlotWidget is a QWidget that only has the canvas if the canvas is a QWidget, another example is the class plotData that inherits unnecessarily from FigureCanvas in addition to doing a redundant task.
Considering the above, the solution is:
mplwidget.py
from PyQt5 import QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
import matplotlib
# Ensure using PyQt5 backend
matplotlib.use("QT5Agg")
class MplWidget(Canvas):
def __init__(self, parent=None):
Canvas.__init__(self, Figure(dpi=100))
self.setParent(parent)
self.setSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
)
self.updateGeometry()
self.ax = self.figure.add_subplot(111)
main.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
import design
class MyApp(QMainWindow, design.Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.x = [1, 2, 3, 4, 5]
self.y = [1, 2, 3, 4, 5]
self.pushButton_plotData.clicked.connect(self.plot)
self.plotWidget.mpl_connect(s="button_press_event", func=self.get_coord)
def plot(self):
self.plotWidget.ax.plot(self.x, self.y)
self.plotWidget.draw()
def get_coord(self, event):
if event.button == 1:
ix = event.xdata
self.textBrowser_X1.setText(str(ix))
if __name__ == "__main__":
# Create GUI application
app = QApplication(sys.argv)
form = MyApp()
form.show()
app.exec_()
I'm trying to implement a tab with the name "+" in which if I click on it opens a ApplicationModal window for configurate the content in that tab. I want to avoid the change of tab when i click in it until press "Accept" in the ApplicationModal window. How can I block this change? I don't know if I explain me.
This it's de code
tabs.py for the MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(628, 504)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(36, 34, 541, 396))
self.tabWidget.setObjectName("tabWidget")
self.tab1 = QtWidgets.QWidget()
self.tab1.setObjectName("tab1")
self.tabWidget.addTab(self.tab1, "")
self.tab2 = QtWidgets.QWidget()
self.tab2.setObjectName("tab2")
self.tabWidget.addTab(self.tab2, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 628, 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)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab1), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab2), _translate("MainWindow", "+"))
win0.py for the ApplicationModal window
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Win0(object):
def setupUi(self, Win0):
Win0.setObjectName("Win0")
Win0.setWindowModality(QtCore.Qt.ApplicationModal)
Win0.resize(255, 203)
self.centralwidget = QtWidgets.QWidget(Win0)
self.centralwidget.setObjectName("centralwidget")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(17, 9, 154, 22))
self.comboBox.setObjectName("comboBox")
self.accpet_button = QtWidgets.QPushButton(self.centralwidget)
self.accpet_button.setGeometry(QtCore.QRect(43, 130, 75, 23))
self.accpet_button.setObjectName("accpet_button")
self.cancel_button = QtWidgets.QPushButton(self.centralwidget)
self.cancel_button.setGeometry(QtCore.QRect(135, 131, 75, 23))
self.cancel_button.setObjectName("cancel_button")
Win0.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Win0)
self.menubar.setGeometry(QtCore.QRect(0, 0, 255, 21))
self.menubar.setObjectName("menubar")
Win0.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Win0)
self.statusbar.setObjectName("statusbar")
Win0.setStatusBar(self.statusbar)
self.retranslateUi(Win0)
QtCore.QMetaObject.connectSlotsByName(Win0)
def retranslateUi(self, Win0):
_translate = QtCore.QCoreApplication.translate
Win0.setWindowTitle(_translate("Win0", "New Window"))
self.accpet_button.setText(_translate("Win0", "Accept"))
self.cancel_button.setText(_translate("Win0", "Cancel"))
And the main.py for develop the functionality
from tabs import Ui_MainWindow
from win0 import Ui_Win0
from PyQt5 import QtCore, QtGui, QtWidgets
class Win0(Ui_Win0):
win0 = None
def __init__(self, win):
self.win0 = win
super().__init__()
self.setupUi(self.win0)
class Main(Ui_MainWindow):
win0 = None
win1 = None
def __init__(self, win):
self.win = win
super().__init__()
self.setupUi(self.win)
self.tabWidget.tabBarClicked.connect(self.tabClick)
def tabClick(self, event):
if event == 1:
print("New tab")
self.win1 = QtWidgets.QMainWindow()
new_window = Win0(self.win1)
self.win1.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Main(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
If you want a window that blocks the others until it's closed, you're looking for a modal dialog.
A modal window creates a mode that disables the main window but keeps it visible, with the modal window as a child window in front of it. Users must interact with the modal window before they can return to the parent application. This avoids interrupting the workflow on the main window.
[From the wikipedia article about modal windows]
It is relatively easy to achieve that in Qt, by using a QDialog.
Designer already provides two basic templates with basic Ok/Cancel buttons when you create a new form, so create a new dialog with buttons on right or bottom (this will automatically connect the button box with the dialog's accept/reject slots), add the combobox, save and convert the file with pyuic (in the following example, I exported the file as dialog.py and used the default Ui_Dialog).
Then the implementation is very easy. The important thing is to add the parent argument to the QDialog instance: this ensures that the dialog uses the parent as a reference for the "window modality", so that that parent is blocked until the dialog is closed by accepting or rejecting it (rejection is usually done by clicking on a RejectRole button like Cancel or by closing the dialog in any way, including pressing Esc).
Do note that I changed your approach by using multiple inheritance in order to make things easier (see the guidelines on using Designer about this approach, which is usually the better and most suggested method when using pyuic generated files).
from tabs import Ui_MainWindow
from dialog import Ui_Dialog
from PyQt5 import QtCore, QtGui, QtWidgets
class SelectDialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
class Main(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.tabWidget.tabBarClicked.connect(self.tabClick)
def tabClick(self, tab):
if tab == 1:
dialog = SelectDialog(self)
if dialog.exec_():
print(dialog.comboBox.currentIndex())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = Main()
mainWindow.show()
sys.exit(app.exec_())
In my PyQt5 Application I want to add an Import data window in order to make sure delimiter, skiprows, etc.. is correctly set before trying to import data from a file. Obviously I want this to happen in a separate window. I'm not going to post the full code there, just a quick example which is similar.
In this example there are two windows: the main and the import window. I'd like to send simple text from import window to mainwindow. My problem is I can't really access the created instance of MainWindow, because that happens in main.py.
Here is the code:
main.py
import sys
from logic import MainProgram
from PyQt5 import QtWidgets
def main():
app = QtWidgets.QApplication(sys.argv)
my_interface = MainProgram()
my_interface.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
logic.py
from PyQt5.QtWidgets import QFileDialog
from PyQt5 import QtWidgets, QtCore
from mainwindow_ui import Ui_MainWindow
from import_ui import Ui_ImportPage
class MainProgram(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainProgram, self).__init__(parent)
self.setupUi(self)
self.open.clicked.connect(self.open_import_page)
def open_import_page(self):
self.window1 = ImportPage(self)
self.window1.show()
class ImportPage(QtWidgets.QMainWindow, Ui_ImportPage):
def __init__(self, parent=None):
super(ImportPage, self).__init__(parent)
self.setupUi(self)
self.get.clicked.connect(self.get_text)
def get_text(self):
print(self.line.text()) # <--- this is want I want to send back to MainProgram class
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
my_interface = MainProgram()
my_interface.show()
sys.exit(app.exec_())
mainwindow_ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.open = QtWidgets.QPushButton(self.centralwidget)
self.open.setObjectName("open")
self.verticalLayout.addWidget(self.open)
self.line2 = QtWidgets.QLineEdit(self.centralwidget)
self.line2.setPlaceholderText('Recieved from import page')
self.verticalLayout.addWidget(self.line2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
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.open.setText(_translate("MainWindow", "Open import page"))
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_())
import_ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_ImportPage(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Import")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.get = QtWidgets.QPushButton('Send data to MainWindow', self.centralwidget)
self.verticalLayout.addWidget(self.get)
self.line = QtWidgets.QLineEdit(self.centralwidget)
self.line.setObjectName("open")
self.line.setPlaceholderText('Type here what you want to send to MainWindow')
self.verticalLayout.addWidget(self.line)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
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", "Import"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_ImportPage()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Should I change the structure of the code to achieve this? Is there anything I'm missing?
Thank you for answering.
You can access the ImportPage instance in MainProgram as you create it in that scope. So you should make the necessary connections and get the text:
class MainProgram(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainProgram, self).__init__(parent)
self.setupUi(self)
self.open.clicked.connect(self.open_import_page)
self.window1 = ImportPage(self)
self.window1.get.clicked.connect(self.on_clicked)
def open_import_page(self):
self.window1.show()
def on_clicked(self):
print(self.window1.line.text())