Python: retrieve objects's value from a class object -


here problem, i'm creating tkinter gui , in class object call text tipped in textfield , make array of it

but can't find how retrieve , use object later on in script.

here script

import pandas pd pandas import * import numpy np import tkinter tkinter import * pylab import * ttk import * import scrolledtext  df = pd.dataframe(np.random.randint(100, 500, size = (10, 20)), index = list('abcdefghij'))     mylist = df.index.values.tolist()      rootwin = tkinter.tk()     class graphgui():         def __init__(self, rootwin):             self.textfield = scrolledtext.scrolledtext(rootwin, width=30, height=10)             #add text:             self.textfield.insert(insert, "replace")             self.textfield.grid(column=0, row=4)              self.button = button(rootwin, text="process!", command=self.clicked)             self.button.grid(column=1, row=4)           def clicked(self):             etext = self.textfield.get('1.0', end)             converted = etext.encode('ascii','ignore')             myarray = converted.splitlines()             print(myarray)       def clicked2():         print(clicked.myarray)      start = tkinter.button(rootwin, text="start!", command=clicked2).grid(column=7, row=8)      app = graphgui( rootwin )     rootwin.mainloop() 

the problem when click on process button no problem myarray printed. when use start button can't retrieve it. there way retrieve myarray out of class object? possible print myarray start button without having use process button first?

##### edit

here final working script help

class graphgui():     def __init__(self, rootwin):         self.textfield = scrolledtext.scrolledtext(rootwin, width=30, height=10)         #add text:         self.textfield.insert(insert, "replace accession list")         self.textfield.grid(column=6, row=4)          self.button = button(rootwin, text="process!", command=self.clicked)         self.button.grid(column=7, row=4)      @property     def myarray(self):         e_text = self.textfield.get('1.0', end)         converted = e_text.encode('ascii','ignore')         return converted.splitlines()      def clicked(self):         print(self.myarray)         print(variablea.get())    #not defined here#         print(variableb.get())  def clicked2():     print(app.myarray)  start = tkinter.button(rootwin, text="start!", command=clicked2).grid(column=7, row=10) 

looks you're trying access local variable of object method. that's not possible. if need it, try defining own callback instead.

class graphgui():      def __init__(self, rootwin):         self.textfield = scrolledtext.scrolledtext(rootwin,                                                    width=30,                                                    height=10)         # add text:         self.textfield.insert(insert, "replace")         self.textfield.grid(column=0, row=4)          self.button = button(rootwin, text="process!", command=self.clicked)         self.button.grid(column=1, row=4)      def clicked(self):         etext = self.textfield.get('1.0', end)         converted = etext.encode('ascii', 'ignore')         myarray = converted.splitlines()         return myarray  rootwin = tkinter.tk()  app = graphgui(rootwin) start = tkinter.button(rootwin, text="start!", command=app.clicked) start.grid(column=7, row=8)  # code assigned `start = none` btw rootwin.mainloop() 

alternatively, myarray defined property of class.

class graphgui():     ...      @property     def myarray(self):         e_txt = self.textfield.get('1.0', end)         converted = e_txt.encode('ascii', 'ignore')         return converted.splitlines()      def clicked(self):         print(self.myarray)         # whatever else has   rootwin = tkinter.tk() start = tkinter.button(rootwin, text="start!",                        command=lambda: app.myarray) ... 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -