Cannot copy the filename browsed into the main program. Where can i write my main function where i have to write my piece of code. Exactly where?

StackOverflow https://stackoverflow.com/questions/23464855

Question

I am new to trying this TKinter . I have to browse files and folders. What i need is to get the pathname of the files and folders i have browsed. i Am not not able to understand where to put my piece of code in this script? . How can i do it? or should i write a seperate . Am stuck in here. I browsed through stack overflow many have problem of getting filenames returned back to their program s with no proper solution. Can anyone help me on this?Advance thanks.

from Tkinter import Tk, RIGHT, BOTH, RAISED,Label
from ttk import Frame, Button, Style
from PIL import Image, ImageTk
import tkFileDialog
import tkMessageBox
import glob  
#global root
f1=""
f2=""

def FileBrowser():
    img1_path = tkFileDialog.askopenfilename(parent=root,title='Provide the Query Image ')
    global f1
    f1=img1_path
    #print img1_path

def PathBrowser():
    img2_path =tkFileDialog.askdirectory(parent=root,title='Provide the path for Training Dataset ')
    global f2
    f2=img2_path
    #print img2_path

def matcher():
    imlist=glob.glob(f2)
    print imlist
    for file in imlist:
        print file


class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)   

        self.parent = parent

        self.initUI()

    def initUI(self):
        global root
        self.parent.title("Medical CBIR")
        self.style = Style()
        self.style.theme_use("default")

        frame = Frame(self, relief=RAISED, borderwidth=1)
        w = Label(self, text="QUERY IMAGE")
        w.pack()
        style = Style()
        style.configure("TFrame", background="#333")        

        im = Image.open('D:/database/Mixture/1.png')
        img = ImageTk.PhotoImage(im)
        label1 = Label(self, image=img)
        label1.image = img
        label1.place(x=155, y=70)
        frame.pack(fill=BOTH, expand=1)
        self.pack(fill=BOTH, expand=1)

        Retrieve_Image=Button(self,text="Retrieve Related Images",command=matcher)
        Retrieve_Image.pack(side=RIGHT, padx=5, pady=5)

        Training_Image = Button(self, text="Path of Training Images",command=PathBrowser)
        Training_Image.pack(side=RIGHT, padx=5, pady=5)

        Test_image = Button(self,text="Browse Test_Image",command=FileBrowser)
        Test_image.pack(side=RIGHT, padx=5, pady=5)




def main():
    global root
    root = Tk()
    #root.geometry("300x200+300+300")
    root.geometry("500x500")
    app = Example(root)
    root.mainloop()  


if __name__ == '__main__':
    main() 

# Comparison.py(2nd file)
import cv2
import sys
import GUItkinter

img1_path =GUITKinter.FileBrowser()
img2_path =GUITKinter.PathBrowser()
imlist=glob.glob(f2)
for file in imlist:
     compare(img1_path,file) #Function to compare images for similarity
#display the retrieved results

Error: Bad window pathname in img1_path =GUITKinter.FileBrowser()

Was it helpful?

Solution

You have to work out the code yourself. But what I'll do is provide you a way how to format your code & how to implement the problem you are having.

import tkFileDialog
from Tkinter import Tk,Button,StringVar,Entry

class MainClass():

    def __init__(self,master):
        self.parent=master
        self.mainfunc()

    def mainfunc(self):
            self.path_setup = StringVar()
            browse=Button(root,text="Browse",command=lambda:self.path_setup.set(tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])))
            browse.grid(row=1,column=1,sticky='E')

            path_entry = Entry(root, textvariable=self.path_setup,state='disabled')
            path_entry.grid(row=1,column=0,sticky='E')

if __name__ == '__main__':
    root = Tk()
    root.title("My Program")
    root.wm_resizable(0,0)
    client = MainClass(root)
    root.mainloop()

It's a "piece" of code which I was working on. I have formatted it for you.

Hope this gets you going :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top