1

I'm trying to make "My own Photo Editor" but I face a problem while I try to get the get working dir that I stored in a function variable. I tried many other ways but I still don't get "my current directory info" on updated base.

Try to make a new Image Editor on python with the help of tkinter GUI.

photo= ''
def open_image(event=None):
    global photo
    directory= ''
    url= filedialog.askopenfilename(initialdir=os.getcwd(), title='Select File', 
                        filetypes=(('Text File', '*.jpg'),('All files', '*.*')))

    img= Image.open(url)
    Max_size= (1030,686)
    img.thumbnail(Max_size)
    photo= ImageTk.PhotoImage(img)
    img_label= tk.Label(frame,image=photo, bg='red')
    img_label.focus()
    img_label.grid(row=0, column=0, sticky='s', padx=13, pady=17)

    directory= url
    label= tk.Label(main_application, text=directory, borderwidth=5, relief='sunken', padx=0, pady=5, font=('arial',7,'bold'))
    label.pack(side=tk.BOTTOM, anchor='w', pady=5, fill='both')
    return directory

save_dir= open_image()

I want directory variable value which have my 'url of a image'. When I call it outside from a function like (save_dir= open_image()) it save url/directory path but after it gives me the same path again and again. Even if I change the directory it show the same directory which I opened first time. I want updated directory info.

  • 1
    Please have a look at [this answer](https://stackoverflow.com/a/423596/3567888). It describes the danger of using `global` variables. – zypro Jul 20 '19 at 13:35
  • create minimal working code with your problem. How do you use this `save_dir` ?In other function ? You may have to assign to global variable. BTW: You use `askopenfilename` which gives filename, not directory. – furas Jul 20 '19 at 14:23
  • ohhh ok. But can you please tell me how can I get this filename from outside my function. and I used save_dir just to show that it print the file full name where it placed too, (complete location of my file.). – Hassam Chaudhary Jul 20 '19 at 17:15
  • I didn't use #save_dir in my real work. I showed this as an example. But still I can't get my filename from outside the function. – Hassam Chaudhary Jul 20 '19 at 17:17
  • It's just my #beginning lvl in python. That's why I am doing each practice to get python on my finger tips. – Hassam Chaudhary Jul 20 '19 at 17:24
  • variables in function are local and they exist only when you run function. After running function you can't get value from local variables because they don't exist any more - you have to use `return` or `global` to assign value to external variables. To assign new value you have to run it again - at least you have to run again `askopenfilename` to get new directory. – furas Jul 20 '19 at 23:19
  • how did you "change the directiory" ? did you use `askopenfilename` ? did you assign value from `askopenfilename` again to `save_dir` ? – furas Jul 20 '19 at 23:21
  • Yes I am just passing value through 1 variable to other variable. Whenever I run 'Open_image' function I get new file directory location or path in 'url' name variable. I understand what you said yesterday and now I solved my problem. Thank you your comment helped me to search more on 'Global variable" – Hassam Chaudhary Jul 21 '19 at 07:15

0 Answers0