【问题标题】: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?无法将浏览的文件名复制到主程序中。我在哪里可以写我的主要功能,我必须写我的一段代码。具体在哪里?
【发布时间】:2014-05-05 04:34:42
【问题描述】:

我是尝试这个 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() 中的窗口路径名错误

【问题讨论】:

  • 请正确格式化问题。另外,您遇到的错误是什么?
  • @shaktimaan- 我有一段代码,我希望从 tkinter 复制路径名和文件名,但由于一些回调错误,我无法使用它。TKinter 不支持它。我该怎么做以及将我的代码放在这个程序中的什么位置?

标签: python python-2.7 tkinter python-idle


【解决方案1】:

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

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

这是我正在处理的“一段”代码。我已经为你格式化了。

希望这能让你继续前进:)

【讨论】:

  • @shaktimaan- 我已经编辑了我的代码。我有一个单独的代码写在其他页面上。在该页面中,我有用于查找两个图像之间相似性的代码。我只是想要我的程序的 GUI,但我无法将我在 TKinter 中浏览的文件的路径复制到我的程序代码中
  • “其他页面”是什么意思?
  • 我的一段代码位于 File_comparison.py 中,我在这里编写的以下代码存储在 Tkinter.py 中,我想要一个 GUI 来选择文件,当我选择文件时我想要路径将其复制到我的 File_comparison.py 中,以便我可以删除在文件名路径中键入的硬编码。但是 TKInter 不允许返回文件名。如何合并这两个文件以提供良好的 GUI。
  • @Jonas 1. 从不将您的文件命名为 Tkinter.py。它是内置 Tkinter 模块的名称。 2.只需导入另一个文件并调用其返回文件名的函数。喜欢this
  • @shaktiman。我以前试过,但无法链接它。我有两个文件 GUItkinter.py 和 compare.py。 GUItkinter.py 是我在上面发布的。
猜你喜欢
  • 2021-07-31
  • 2016-10-07
  • 1970-01-01
  • 2016-11-21
  • 2012-08-14
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-31
相关资源
最近更新 更多