Python Tkinter中的按钮错误-选择文件,添加其路径

Python Tkinter中的按钮错误-选择文件,添加其路径,python,button,tkinter,path,Python,Button,Tkinter,Path,我的Tkinter代码有问题。代码的目标是有4个按钮,2个浏览按钮,用于选择要相互比较的领域。第三个是为输出的文件选择目标。第四个是提取我的python代码,用它来计算这两个文件 我的浏览按钮有问题,每当我在窗口中选择文件时,它们都会返回: pathlabel.delete(0, END) NameError: name 'pathlabel' is not defined 或 预期的结果是保存所选文件的路径,这样我就可以提取这些路径以在python代码中使用 我在网上查找了有关pathlab

我的Tkinter代码有问题。代码的目标是有4个按钮,2个浏览按钮,用于选择要相互比较的领域。第三个是为输出的文件选择目标。第四个是提取我的python代码,用它来计算这两个文件

我的浏览按钮有问题,每当我在窗口中选择文件时,它们都会返回:

pathlabel.delete(0, END)
NameError: name 'pathlabel' is not defined

预期的结果是保存所选文件的路径,这样我就可以提取这些路径以在python代码中使用

  • 我在网上查找了有关pathlabel和Infle函数的文档,但没有看到任何有用的内容。谁能看出我哪里出了问题,为什么会出现这些错误

  • 有没有人对如何在本地保存路径(可能是一个变量)以便我以后可以提取它们有什么建议?到目前为止,我只取得了有限的成功

  • 多谢各位

    下面是我的代码:

    import os
    from Tkinter import *
    from tkinter import filedialog
    
    content = 'apple'
    file_path = 'squarebot'
    
    #FUNCTIONS
    def browsefunc(): #browse button to search for files
        filename = filedialog.askopenfilename()
        infile = open(filename, 'r')
        content = infile.read()
        pathadd = os.path.dirname(filename)+filename
    
        pathlabel.delete(0, END)
        pathlabel.insert(0, pathadd)
    
        return content
    
    def browsefunc2(): #browse button to search for files
        filename2 = filedialog.askopenfilename()
        infile = open(filename2, 'r')
        content = infile.read(filename2)
        pathadd = os.path.dirname(filename2)+filename2
    
        pathlabel.delete(0, END)
        pathlabel.insert(0, pathadd)
    
        return content
    
    def process_file(content): #process reconciliation code
        print(content)
    
    def directoryname():
        directoryname = filedialog.askdirectory() # pick a folder
    
    
    #GUI
    
    root = Tk()
    
    root.title('Reconciliation Converter')
    root.geometry("598x150")
    
    mf = Frame(root)
    mf.pack()
    
    
    f1 = Frame(mf, width=600, height=250) #file1
    f1.pack(fill=X)
    
    f2 = Frame(mf, width=600, height=250) #file2
    f2.pack(fill=X)
    
    f3 = Frame(mf, width=600, height=250) #destination folder
    f3.pack(fill=X)
    
    f4 = Frame(mf, width=600, height=250) #reconcile button
    f4.pack()
    
    file_path = StringVar
    
    
    Label(f1,text="Select file 1 (Only txt files)").grid(row=0, column=0, sticky='e') #file1 button
    entry = Entry(f1, width=50, textvariable=file_path)
    entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
    
    Label(f2,text="Select file 2 (Only csv files)").grid(row=0, column=0, sticky='e') #file2 button
    entry = Entry(f2, width=50, textvariable=file_path)
    entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
    
    Label(f3,text="Select Your Destination Folder").grid(row=0, column=0, sticky='e') #destination folder button
    entry = Entry(f3, width=50, textvariable=directoryname)
    entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
    
    Button(f1, text="Browse", command=browsefunc).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#file1 button
    Button(f2, text="Browse", command=browsefunc2).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#file2 button
    Button(f3, text="Browse", command=browsefunc).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#destination folder button
    Button(f4, text="RECONCILE NOW", width=32, command=lambda: process_file(content)).grid(sticky='ew', padx=10, pady=10)#reconcile button
    
    
    root.mainloop()
    

    请注意,此代码可以进行优化,但这应该可以让您继续:

    import os
    from tkinter import *
    from tkinter import filedialog
    
    content = 'apple'
    file_path = 'squarebot'
    
    #FUNCTIONS
    def browsefunc(): #browse button to search for files
        filename = filedialog.askopenfilename()
        infile = open(filename, 'r')
        content = infile.read()
        pathadd = os.path.dirname(filename)+filename
        file_path1.set(pathadd)
        return content
    
    def browsefunc2(): #browse button to search for files
        filename2 = filedialog.askopenfilename()
        infile = open(filename2, 'r')
        content = infile.read()
        pathadd = os.path.dirname(filename2)+filename2
        file_path2.set(pathadd)
        return content
    
    def browsefunc3(): #browse button to search for files
        directory = filedialog.askdirectory(initialdir='.')
        directoryname.set(directory)
        return content
    
    def process_file(content): #process reconciliation code
        print('------------------------------')
        print(file_path1.get())
        print(file_path2.get())
        print(directoryname.get())
    
    #GUI
    
    root = Tk()
    
    root.title('Reconciliation Converter')
    root.geometry("698x150")
    
    mf = Frame(root)
    mf.pack()
    
    f1 = Frame(mf, width=600, height=250) #file1
    f1.pack(fill=X)
    f2 = Frame(mf, width=600, height=250) #file2
    f2.pack(fill=X)
    f3 = Frame(mf, width=600, height=250) #destination folder
    f3.pack(fill=X)
    f4 = Frame(mf, width=600, height=250) #reconcile button
    f4.pack()
    
    file_path1 = StringVar()
    file_path2 = StringVar()
    directoryname = StringVar()
    
    Label(f1,text="Select file 1 (Only txt files)").grid(row=0, column=0, sticky='e') #file1 button
    entry1 = Entry(f1, width=50, textvariable=file_path1)
    entry1.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
    
    Label(f2,text="Select file 2 (Only csv files)").grid(row=0, column=0, sticky='e') #file2 button
    entry2 = Entry(f2, width=50, textvariable=file_path2)
    entry2.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
    
    Label(f3,text="Select Your Destination Folder").grid(row=0, column=0, sticky='e') #destination folder button
    entry3 = Entry(f3, width=50, textvariable=directoryname)
    entry3.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
    
    Button(f1, text="Browse", command=browsefunc).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#file1 button
    Button(f2, text="Browse", command=browsefunc2).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#file2 button
    Button(f3, text="Browse", command=browsefunc3).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#destination folder button
    Button(f4, text="RECONCILE NOW", width=32, command=lambda: process_file(content)).grid(sticky='ew', padx=10, pady=10)#reconcile button
    
    root.mainloop()
    

    为什么在执行代码时,文件_path1.get()和文件_path2()都会附加到directoryname.get()中?谢谢你的帮助!我真的很感激。我不确定我是否理解,你说的“附加到”是什么意思?这段代码所做的就是打印出从条目中检索到的路径。在您的代码中,您使用pathadd=os.path.dirname(filename2)+filename2和pathadd=os.path.dirname(filename)+filename打印目录路径,然后在目录路径后附加文件路径,创建一个不存在的路径。我只是把它切换到了os.path.join(filename,filename),它有一个“智能”连接功能,可以连接两个路径,同时删除冗余。这是您的原始代码,但连接也是一个选项。但是,您可能希望检查以下内容:
    import os
    from tkinter import *
    from tkinter import filedialog
    
    content = 'apple'
    file_path = 'squarebot'
    
    #FUNCTIONS
    def browsefunc(): #browse button to search for files
        filename = filedialog.askopenfilename()
        infile = open(filename, 'r')
        content = infile.read()
        pathadd = os.path.dirname(filename)+filename
        file_path1.set(pathadd)
        return content
    
    def browsefunc2(): #browse button to search for files
        filename2 = filedialog.askopenfilename()
        infile = open(filename2, 'r')
        content = infile.read()
        pathadd = os.path.dirname(filename2)+filename2
        file_path2.set(pathadd)
        return content
    
    def browsefunc3(): #browse button to search for files
        directory = filedialog.askdirectory(initialdir='.')
        directoryname.set(directory)
        return content
    
    def process_file(content): #process reconciliation code
        print('------------------------------')
        print(file_path1.get())
        print(file_path2.get())
        print(directoryname.get())
    
    #GUI
    
    root = Tk()
    
    root.title('Reconciliation Converter')
    root.geometry("698x150")
    
    mf = Frame(root)
    mf.pack()
    
    f1 = Frame(mf, width=600, height=250) #file1
    f1.pack(fill=X)
    f2 = Frame(mf, width=600, height=250) #file2
    f2.pack(fill=X)
    f3 = Frame(mf, width=600, height=250) #destination folder
    f3.pack(fill=X)
    f4 = Frame(mf, width=600, height=250) #reconcile button
    f4.pack()
    
    file_path1 = StringVar()
    file_path2 = StringVar()
    directoryname = StringVar()
    
    Label(f1,text="Select file 1 (Only txt files)").grid(row=0, column=0, sticky='e') #file1 button
    entry1 = Entry(f1, width=50, textvariable=file_path1)
    entry1.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
    
    Label(f2,text="Select file 2 (Only csv files)").grid(row=0, column=0, sticky='e') #file2 button
    entry2 = Entry(f2, width=50, textvariable=file_path2)
    entry2.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
    
    Label(f3,text="Select Your Destination Folder").grid(row=0, column=0, sticky='e') #destination folder button
    entry3 = Entry(f3, width=50, textvariable=directoryname)
    entry3.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
    
    Button(f1, text="Browse", command=browsefunc).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#file1 button
    Button(f2, text="Browse", command=browsefunc2).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#file2 button
    Button(f3, text="Browse", command=browsefunc3).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#destination folder button
    Button(f4, text="RECONCILE NOW", width=32, command=lambda: process_file(content)).grid(sticky='ew', padx=10, pady=10)#reconcile button
    
    root.mainloop()