Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.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 Tkinter 2.7在选定路径中创建目录_Python_Tkinter_Directory - Fatal编程技术网

使用Python Tkinter 2.7在选定路径中创建目录

使用Python Tkinter 2.7在选定路径中创建目录,python,tkinter,directory,Python,Tkinter,Directory,我想在Tkinter Python 2.7中创建一个类,在从filedialog中选择目录位置后,使用用户在字段中引入的名称创建一个新目录。 举个例子,我想要这样的东西: 用户介绍目录的名称,并应创建以下结构: $HOME\a\<name_introduced_by_the_user_in_the_field>\b $HOME\a\<name_introduced_by_the_user_in_the_field>\c 错误: Exception in Tkinte

我想在Tkinter Python 2.7中创建一个类,在从filedialog中选择目录位置后,使用用户在字段中引入的名称创建一个新目录。 举个例子,我想要这样的东西:

用户介绍目录的名称,并应创建以下结构:

$HOME\a\<name_introduced_by_the_user_in_the_field>\b 

$HOME\a\<name_introduced_by_the_user_in_the_field>\c
错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
    return self.func(*args)
  File "program.py", line 118, in create_dir
    call(["mkdir",self.projectnamevar.get()])
AttributeError: PageThree instance has no attribute 'projectnamevar'
我怎样才能完成全部工作


p.S.我对编程非常陌生

在类中找不到变量projectnamevar,因为您没有将其保存为该变量,请使用

self.projectnamevar = tk.StringVar()
另外,您可能希望使用操作系统模块,而不是在系统上调用它,您可以这样使用它

import os
path = "~/my/path/"
os.mkdir(path)

由于
projectnamevar
变量在init函数中,它有点像是本地变量,所以在init函数中将它们定义为
self.projectnamevar
,并在其他函数中使用相同的方法。谢谢您的回复。我已经尝试了你的代码,但是这一个只是创建了一个新的目录,这个目录被称为用户想要的,而不是问题中提到的整个结构。你能看一下吗?@Albert我不知道你想如何根据你演示的示例创建目录结构,你想在某个位置创建一个用户定义名称的文件夹,然后再创建一个子目录吗?
import os
path = "~/my/path/"
os.mkdir(path)
import os, sys
if sys.version_info[0] == 3:
    from tkinter import *
    from tkinter import messagebox
    from tkinter import filedialog
    from tkinter.ttk import *
elif sys.version_info[0] == 2:
    print ("The Script is written for Python 3.6.4 might give issues with python 2.7, let the author know")
    print ("Note Python 2.7 CSV has a empty line between each result. couldn't find a fix for that")
    from Tkinter import *
    import tkMessageBox as messagebox
    import tkFileDialog as filedialog
    from ttk import Combobox

class temp:
    def __init__(self):
        self.top = Tk()
        self.lab = Label(self.top, text='UserFiled')
        self.en = Entry(self.top, width =25)
        self.but = Button(self.top, text='Submit',command = self.chooseFolder)
        self.lab.grid(row=0, column=0)
        self.en.grid(row=0, column=1)
        self.but.grid(row=0, column=2)
        self.top.mainloop()
    def chooseFolder(self):
        directory = filedialog.askdirectory()
        print(directory)
        newPath = os.path.join(directory, self.en.get())
        if not os.path.exists(newPath):
            os.chdir(directory)
            os.mkdir(self.en.get())

obj = temp()