Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在GUI上显示当前选定的文件夹?_Python_User Interface_Tkinter_Directory - Fatal编程技术网

Python 如何在GUI上显示当前选定的文件夹?

Python 如何在GUI上显示当前选定的文件夹?,python,user-interface,tkinter,directory,Python,User Interface,Tkinter,Directory,你好 我正在使用tkinter filedialog选择一个文件夹,并将其显示在GUI的条目中 这是到目前为止我的代码: import os from tkinter import * from tkinter import filedialog dir_path = '' def inPut(): indir = filedialog.askdirectory(parent=root,initialdir="/",title='Input Folder') indir =

你好

我正在使用tkinter filedialog选择一个文件夹,并将其显示在GUI的条目中

这是到目前为止我的代码:

import os
from tkinter import *
from tkinter import filedialog

dir_path = ''

def inPut():
    indir = filedialog.askdirectory(parent=root,initialdir="/",title='Input Folder')
    indir = str(indir)
    dir_path = os.path.dirname(indir)
    entry.delete(0, END)
    entry.insert(0, dir_path)
    return dir_path

root = Tk()
root.geometry("640x240")
root.title("Settings")

frametop = Frame(root)
framebottom = Frame(root)
frameright = Frame(framebottom)

text = Label(frametop, text="Input Folder").grid(row=5, column=2)
entry = Entry(frametop, width=50, text=dir_path)
entry.grid(row=5,column=4,padx=2,pady=2,sticky='we',columnspan=20)

ButtonA = Button(frametop, text="Change", command=inPut).grid(row=5, column=28)
ButtonB = Button(frameright, text="OK").grid(row=5, column=20, padx=10)

frametop.pack(side=TOP, fill=BOTH, expand=1)
framebottom.pack(side=BOTTOM, fill=BOTH, expand=1)
frameright.pack(side=RIGHT)

root.mainloop()
目前,它只返回'D:/'

  • 如何使其返回条目中的完整路径

  • 如何将“/”更改为“\”

  • 如何使应用程序在下次运行应用程序时将文件夹记住为初始目录


  • 请帮忙

    要将其写入标签,您需要有对其的引用

    多种可能性:

  • 为GUI创建一个类,并使用
    showdir
    或绑定到它的textvariable作为实例变量
  • 将引用传递到您的呼叫,以使
    showdir
    可用
  • 全局
    showdir
  • 为什么要下这个订单?最不受欢迎的

    如果您不知道如何写入或读取文件,请在开始创建GUI之前从python教程开始。如果您不知道为什么要在执行某些步骤时执行这些步骤,“将其他人编写的代码混淆在一起”是没有意义的。这就像开车不知道交通情况一样

    至于你的编辑:

    您的代码现在可以工作了,因为您不再直接访问该属性,所以您不再对条目使用即时写访问

    • 返回目录路径
      已完全过时
    • 按照父目录路径中的定义,当
      os.path.dirname(indir)
      返回时,不会得到文件夹。如果使用文件而不是目录dirname,则可以,只要使用目录,就可以使用
      os.path.abspath
    os.path.dirname(路径)¨

    返回路径名路径的目录名。这是通过向函数split()传递路径返回的对中的第一个元素。
    你想为你的目录“显示”什么?名称、内容、文件、文件夹?至于第二个问题:将目录路径写入文件,并在启动时读取。如果不存在,请使用默认值。我想显示所选的完整路径,例如“D:\Entertainment\New Movies”,我该怎么做?“将目录路径写入文件,并在启动时读取”对不起,这里没有!请提供一份合适的表格。上述代码将返回与声明的行为相反的无关错误。请修复它。很抱歉,但你的回答根本没有帮助,你告诉我一些我已经知道的事情,我知道我需要一个参考…你看过我的代码了吗?showdir=Label(frametop,text=dirtext)。grid(row=5,column=21)显然不是-您的引用需要是可访问的。函数中使用的对象需要在该范围内可用。您可以从函数中的全局范围读取变量,但要写入变量,您需要传递它们的引用,将它们作为实例变量进行访问,或者在函数中声明它们为全局变量,就像我在回答中告诉您的那样。我缩短了代码,并设法让它显示驱动器号,请看上面我调整了答案。非常感谢,这100%有效,但如何保存目录,以便在应用程序关闭和再次打开时它就在那里?(这就是“确定”按钮的作用)