Reputation: 321
I am trying to implement a GUI in PyQt for data analysis. Basically what it does is, it lets the user browse the location of data and plots it in an embedded matplotlib environment. This the code I have so far.
from PyQt5 import uic, QtWidgets
import sys
import pandas as pd
import singlecurve as sc
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
dfG = []
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi('gui.ui', self)
self.LoadData.clicked.connect(self.getfiles)
self.LoadDataB.clicked.connect(self.getfiles)
GraphArea = PlotCanvas(self, width = 9, height = 7)
GraphArea.move(0,20)
self.ExperimentNumber.valueChanged.connect(lambda: GraphArea.plot(self.ExperimentNumber.value(), self.Smoothing.value()))
self.Smoothing.valueChanged.connect(lambda: GraphArea.plot(self.ExperimentNumber.value(), self.Smoothing.value()))
self.readyArea.setStyleSheet("background-color: yellow")
self.readyArea.setText("No Data")
def getfiles(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', "/Users/03abc/Desktop/python", '*.xlsm *.xls *.xlsx')
df = pd.read_excel(fileName)
global dfG
dfG = df
self.readyArea.setStyleSheet("background-color: green")
self.readyArea.setText("Ready")
class PlotCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
def plot(self, data, smoothing):
data = sc.MovingMedianCalculator(dfG.iloc[:,data], smoothing)
print(data)
print("lado")
#data = [10,20,20,50,70,90 ]
#data = [random.random() for i in range (10) ]
self.figure.clear()
ax = self.figure.add_subplot(111)
ax.plot( data, 'r-')
ax.margins(0, 0.05)
#ax.set_title('PyQt Matplotlib Example')
self.draw()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Ui()
window.show()
sys.exit(app.exec_())
I am using a global variable to exchange the data between Ui class and the Plotcanvas class. From my programming in C, I know that global variables are to be avoided if possible. This seems to be the case in python too. A button has been implemented and by clicking on it, users can browse and select the dataset they want. This data is then modified. But I cannot seem to figure out, what is the best way to make this data obtained from the Ui available to other python classes and functions?
Upvotes: 1
Views: 307
Reputation: 244301
As you point out, you should avoid using the globale variables (see Why are global variables evil?), in your case it is also unnecessary since it is enough to save the information in an attribute of the class.
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
self.dfG = pd.DataFrame()
uic.loadUi("gui.ui", self)
self.LoadData.clicked.connect(self.getfiles)
self.LoadDataB.clicked.connect(self.getfiles)
self.graphArea = PlotCanvas(self, width=9, height=7)
self.graphArea.move(0, 20)
self.ExperimentNumber.valueChanged.connect(self.onvalueChanged)
self.Smoothing.valueChanged.connect(self.onvalueChanged)
self.readyArea.setStyleSheet("background-color: yellow")
self.readyArea.setText("No Data")
def getfiles(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
self, "Open file", "/Users/03abc/Desktop/python", "*.xlsm *.xls *.xlsx"
)
if fileName:
self.dfG = pd.read_excel(fileName)
self.readyArea.setStyleSheet("background-color: green")
self.readyArea.setText("Ready")
def onvalueChanged(self):
self.graphArea.plot(self.dfG, self.Smoothing.value())
Upvotes: 2