I created a GUI project in pyqt5 that lets us enter a country’s name in the text input field, then we are getting data from the CoVid-19 API for the particular country. I added a feature (button) which give us a possibility to save matplotlib’s plot with statistics from API to my folder called “stats”. Let’s assume that I enter USA, download the pdf. It’s ok. Then I enter Germany, download the pdf. It’s ok, I got USA.pdf and Germany.pdf in /stats/, but if I remove 2 of them and enter third country, let’s say Canada and click download button, it creates Canada.pdf and also USA and Germany again. I do not know when I made a mistake, but I am thinking of passing lambda function to “clicked” event as a parameter has something to do with that. Here is the code:
App.py
def create_download_btn(self, data): self.download_btn.show() self.download_btn.move(320, 30) self.download_btn.resize(140, 40) self.download_btn.setText('Download PDF') self.download_btn.clicked.connect(lambda: self.create_pdf_plot(data)) self.download_btn.setStyleSheet('background-color: #fff;' 'color: #777;' 'border-style: outset;' 'border-width: 3px;' 'border-color: rgb(220, 20, 60);' 'border-radius: 5px;' 'font-size: 16px;' 'font-weight: bold;' 'padding: 5px;') def create_pdf_plot(self, data): export_to_pdf(data) self.download_btn.hide() self.create_download_label() self.download_label.setText(f"You've downloaded the file successfully!n " f"Path: /stats/{data[-1]['Country']}.pdf")
create_pdf.py
def export_to_pdf(data): """ Creating and exporting plot to a PDF file basing on the given data. :param data: Number of total cases, deaths, active cases, fetched from the API :return: void """ days = np.array([]) confirmed_cases = np.array([]) deaths = np.array([]) active_cases = np.array([]) # Looping over every index of JSON data for day, data_elem in enumerate(data): days = np.append(days, day) confirmed_cases = np.append(confirmed_cases, data_elem['Confirmed']) deaths = np.append(deaths, data_elem['Deaths']) active_cases = np.append(active_cases, data_elem['Active']) # Creating a plot plt.plot(days, confirmed_cases, color='black', label='Confirmed cases', linewidth=3) plt.plot(days, deaths, color='red', label='Deaths', linewidth=3) plt.plot(days, active_cases, color='green', label='Active cases', linewidth=3) plt.title(f"{data[-1]['Country']} COVID-19 statistics") # Creating and displaying table in a program's console using pandas table = [['Numbers', confirmed_cases[-1], deaths[-1], active_cases[-1]]] df_table = pd.DataFrame(table) df_table.columns = (f"{data[-1]['Country']}", 'Confirmed cases', 'Deaths', 'Active cases') print(df_table) # Setting axis and saving the plot to a PDF file plt.xlabel('Days since the first confirmed case') plt.legend() plt.savefig(f"./stats/{data[-1]['Country']}.pdf") plt.clf()
Advertisement
Answer
You have connect
ed a new action(download new country’s pdf file) without un-connect
ing the old action(download old country’s pdf file), hence clicking it will trigger all the actions. Which makes it create all three files.
You can remove the connect
ion after you’re done with it to solve the bug.
Edit:
You can use self.download_btn.disconnect()
to remove the connect
ion.