Float field required in order for any of this code to work on matplotlib. If float field not input, form dialog does not open.
from matplotlib.backend_tools import ToolBase from matplotlib.backends.qt_editor._formlayout import fedit import matplotlib.pyplot as plt import matplotlib import pandas as pd import os
class Save(ToolBase):
default_keymap = 'X'
description = 'Save dataframe to csv, xlsx, json, ubin, or hdf'
image = ''
df = pd.read_csv('C:\Python\PY3810_64v1\Lib\site-packages\\data\sample.csv')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def trigger(self, *args):
datalist = [('File Type', ['', '', 'xlsx', 'csv', 'json', 'ubin', 'hdf']),
('', 0.0), # if this line is commented out, the code stops working with matplotlib plot --- this is where the issue lies
('File Path', os.getcwd())]
def apply_callback(data):
(file, path) = data
if file == 'csv':
Save.df.to_csv('path')
fedit(datalist, 'Excel', 'Save the current dataframe', parent=None, apply=apply_callback)
matplotlib.use('Qt5Agg', force=True) plt.rcParams['toolbar'] = 'toolmanager' fig, ax = plt.subplots() fig.canvas.manager.toolmanager.add_tool('Save', Save) fig.canvas.manager.toolbar.add_tool('Save', 1, -1) plt.show()
There are several ways to fix this issue. One is to add more validators and the other is to instantiate a QtDialog object inside the parent setup function by using:
QLineEdit(value, self).textChanged.connect(lambda text: QDialog.update_buttons())
If you want to know more, you have to spend your time learning about signals, slots, and object oriented programming.