How To Access Variable Defined Browse Button Class Outside Class Scope
I have called a class of browse button(to select file location) from main function. This class has browse function to capture file-path variable that stores the path. Now I just want to use this file path defined in class function outside this class like print this variable outside the class scope
I have already tried giving loc as global, other access methods but not of them worked I guess its due to arguments being passed to class.
'''Using Tkinter module'''
class Browse(tk.Frame,object):
# here __init__ ,_create_widgets,_display_widgets are defined then I have,
def browse(self):
""" Browses a .xlsx file or all files and then puts it on the entry.
"""
self.filepath.set(fd.askopenfilename(initialdir=self._initaldir,
filetypes=self._filetypes))
print(self.filepath.get()), self #Path of ATP choosen by user
loc = self.filepath.get() #want to excess this out of class
I want to print 'loc' value (able to print inside) outside the the class scope how can I access the same. I guess the problem is being caused due to arguments my class has, though not sure.
Answer
As it stands, loc
is a local variable in the browse
function, and it will cease to exist when the browse
function returns. If you write it as:
self.loc = self.filepath.get()
Then if you have a Browse
object called b
, you can just write b.loc
to access it.
If you want to access the variable even if there is no Browse
object to hand, you will need a class variable. Set with:
Browse.loc = self.filepath.get()
and access with Browse.loc
. The problem with class variables is the same as with all global variables though - what if you have two Browse
objects? and what if you try to access the class variable from multiple threads?
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module