无法将浏览的文件名复制到主程序中。我在哪里可以编写我的主函数,我必须编写我的代码。究竟在哪里?

时间:2014-05-05 04:34:42

标签: python python-2.7 tkinter python-idle

我是尝试这个TKinter的新手。我必须浏览文件和文件夹。我需要的是获取我浏览过的文件和文件夹的路径名。我无法理解在这个脚本中放置我的代码的位置? 。我该怎么做?或者我应该写一个单独的。我被困在这里。我浏览堆栈溢出很多都有问题,没有适当的解决方案将文件名返回给他们的程序。任何人都可以帮我吗?谢谢。

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

错误:img1_path中的窗口路径名错误= GUITKinter.FileBrowser()

1 个答案:

答案 0 :(得分:0)

你必须自己编写代码。但我要做的是为您提供一种如何格式化代码的方法。如何实现你遇到的问题。

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()

这是我正在研究的“片段”代码。我已经为你格式化了。

希望这能让你前进:)