0

i have an onclick function that takes self and event (event is of type instance ) so can i save this event in xml or .txt file so i can open it back again by browse button as an event instance and give it back to this add_point function

   def add_point(self, event):
    if self.radioButton.isChecked() == True:
        if ((event.xdata)**2 + (event.ydata)**2)**0.5 < 1 and event.ydata >0 :
            print(type(event))
            print (event)

            z = event.xdata + event.ydata * 1j


            self.xy.append([event.xdata, event.ydata])
            self.xy.append([event.xdata, -event.ydata])
            self.zero.append(z)
            print (self.zero)
            self.update()

def browse(self):

    filepath = QtGui.QFileDialog.getOpenFileName(self, 'Single File', "C:\Users\Hanna Nabil\Documents",'*.txt')
    f= str(filepath)
    file =open(filepath ,'r')
    connect = self.fig.canvas.mpl_connect
    connect('button_press_event', self.on_click)
    self.draw_cid = connect('draw_event', self.grab_background)
    with file:
        text= file.read()

        #self.add_point(c)
        print(text)
Hanna
  • 53
  • 1
  • 1
  • 4

1 Answers1

0

I think do you want to save event instance and reload it later. You could use pickle package.

import pickle

'''
    To store the instance
'''

path = 'path where do you want to store the event'

output = open(path,'wb')    # Open a file to store the instance binary (your event)

pickle.dump(self.binario, output)    # Dumping the instance binary to the file

output.close()    # Closing the file        


'''
    To load the instance
'''    

input = open(path,'rb')    # Open the file    

event = pickle.load(input)    # Loading the instance binary

input.close()    # Closing the file

The event variable is what you stored previously, and you can use it to your purpose.

The general purpose of picklepackage is to store binary files like instances or classes and reload it later in your same code or in another project.

Marco
  • 670
  • 5
  • 16
  • Thanks Marco i've used cPickle before but it gives me almost the same error when i use the method your wrote above File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems save(v) File "C:\Python27\lib\pickle.py", line 306, in save rv = reduce(self.proto) File "C:\Python27\lib\copy_reg.py", line 71, in _reduce_ex state = base(self) TypeError: the sip.wrapper type cannot be instantiated or sub-classed – Hanna Mar 21 '17 at 15:27
  • The message `TypeError: the sip.wrapper type cannot be instantiated or sub-classed` says that you can't instantiate the object with type `sip.wrapper`, which surely refers to a component of your GUI. How are you using the code or instantiate your object? It seems an error of `sip`package, which is a component of PyQT. I expect this helps you :) – Marco Mar 21 '17 at 15:49
  • yes exactly um trying to save the instance event which comes from connect('button_press_event', self.on_click) the main object for my code is to draw zero and poles on a unit circle when user click.. i need to save some drawn points and make user able to restore them by browse button – Hanna Mar 21 '17 at 20:58
  • MPL MouseEvent: xy=(77,184) xydata=(-0.996450831025,0.0242382271468) button=1 dblclick=False inaxes=Axes(0.177465,0.1;0.67007x0.8) ________________________________ that's what appear to me when i print event – Hanna Mar 21 '17 at 20:58
  • I have seen that you used `cPickle` package instead of `pickle`. And the error you wrote says _TypeError: the sip.wrapper type cannot be instantiated or sub-classed_. I think that is because of you imported `cPickle`. Check the following link to know about the differences: [Should I use Pickle or cPickle?](http://stackoverflow.com/questions/37111980/should-i-use-pickle-or-cpickle) If doesn't runs, answers this comment and I'll check what can be happening. Regards :) – Marco Mar 22 '17 at 02:34
  • I've seen this link [In PyQt, how can I save the items and selected item in a combobox](http://stackoverflow.com/questions/17666756/in-pyqt-how-can-i-save-the-items-and-selected-item-in-a-combobox) in which the best answer explains that `pickle` can't save PyQT instances. It suggests to retrieve the instance data and save it separately. I suggest you to create your own class to store the `event` attributes you want and save it with `pickle`. When you reopen your app, you can reload the class and reinstance `event` with the attributes of the class that you loaded. – Marco Mar 22 '17 at 02:45