Python: Button widget in Tkinter -


i have begun gui programming in python 2.7 tkinter.

i want have button browse, when clicked opens windows file explorer , returns path of file selected variable. wish use path later.

i following code given here. outputs window displaying 5 buttons, buttons nothing. on clicking first button, doesn't open selected file.

likewise, on clicking second button, askopenfilename(self) function called , should return filename. mentioned, need filename later.

how value returned function variable future use?

there no point in using return inside callback button. won't return anywhere. way make callback save data store in global variable, or instance variable if use classes.

def fetchpath():     global filename     filename = tkfiledialog.askopenfilename(initialdir = 'e:') 

fwiw (and unrelated question): you're making common mistake. in python, when foo=bar().baz(), foo takes value in baz(). thus, when this:

button = button(...).pack() 

button take value of pack() returns none. should separate widget creation widget layout if expect save actual reference widget being created. if you're not, it's practice separate two.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -