首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将浏览的文件名复制到主程序中。我在哪里可以写我的主要功能,在那里我必须写我的代码。确切地说在哪里?

无法将浏览的文件名复制到主程序中。我在哪里可以写我的主要功能,在那里我必须写我的代码。确切地说在哪里?
EN

Stack Overflow用户
提问于 2014-05-05 04:34:42
回答 1查看 77关注 0票数 0

我是第一次尝试这个TKinter。我必须浏览文件和文件夹。我需要的是获得我浏览过的文件和文件夹的路径名。我不明白在这个脚本中我的代码放在哪里?我该怎么做呢?或者我应该写一个单独的。我被困在这里了。我浏览了堆栈溢出,许多人在没有适当解决方案的情况下将文件名返回到他们的程序中有问题。有人能帮我吗?提前谢谢。

代码语言:javascript
复制
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()中的坏窗口路径名

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-05 04:54:35

你得自己算出密码。但是,我要做的是为您提供一种如何格式化代码的方法&如何实现您遇到的问题。

代码语言:javascript
复制
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()

这是我正在做的一段代码。我已经帮你格式化了。

希望这能让你去:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23464855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档