在指定的文件中写入信息Tkinter Python 2.7

在指定的文件中写入信息Tkinter Python 2.7,python,file,tkinter,Python,File,Tkinter,我想写一个文件,它的名字由其他类中声明的两个变量组成 示例:我的文件应写入以下目录:$HOME/nameofproject/clouds.yml Home在另一个类中定义为: def create_dir(self): home=expanduser("~") 项目名称在另一个类中定义为条目: self.projectnamevar=tk.StringVar() projectname=tk.Entry(self,textvariable=self.projectnamevar) pr

我想写一个文件,它的名字由其他类中声明的两个变量组成

示例:我的文件应写入以下目录:$HOME/nameofproject/clouds.yml Home在另一个类中定义为:

def create_dir(self):
    home=expanduser("~")
项目名称在另一个类中定义为条目:

self.projectnamevar=tk.StringVar()
projectname=tk.Entry(self,textvariable=self.projectnamevar)
projectname.pack()
以下是相关代码:

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family="Helvetica", size=18, weight="bold", slant="italic")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.container = container
        self.current_frame = None

        self.num_networks = tk.IntVar()  # Var shared between pages
        self.projectnamevar = tk.IntVar()
        self.num_tenants = tk.IntVar()
        self.home = tk.IntVar()
        self.show_frame("StartPage")

    def show_frame(self, page_name):
        """Show a frame for the given page name"""
        if self.current_frame:
            self.current_frame.destroy()
            self.current_frame = None

        frame_class = globals()[page_name]  
        frame = frame_class(parent=self.container, controller=self)
        frame.grid(row=0, column=0, sticky="nsew")
        frame.tkraise()
        self.current_frame = frame

class PageThree(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self,text="Insert the name of your project",font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        self.projectnamevar=tk.StringVar()
        projectname=tk.Entry(self,textvariable=self.projectnamevar)
        projectname.pack()
        button1 = tk.Button(self, text="Create the directory", command=self.create_dir)
        button1.pack()
        button2 = tk.Button(self, text="Specify the number of tenants", command=lambda: controller.show_frame("PageFive"))
        button2.pack()

    def create_dir(self):
        home=expanduser("~")
        path1=home+"/"+self.projectnamevar.get()+"/"+self.projectnamevar.get()+"/variables/"
        path2=home+"/"+self.projectnamevar.get()+"/"+self.projectnamevar.get()+"/tasks/"
        os.makedirs(path1)
        os.makedirs(path2)

class PageFour(tk.Frame):

    def getclouds(self): 
        homepath=self.controller.home.get()
        projectpath=self.controller.projectnamevar.get()     
        with open("{}/{}/clouds.yml".format(homepath,projectpath), "w") as f:
           f.write("--- #" + "\n")
           f.write("clouds:" + "\n")
           for tenantname in self.entry_vars:
              f.write("   "+tenantname.get()+":"+ "\n")
              f.write("    auth:"+"\n")
              f.write("      auth_url: http://127.0.0.1:5000/v3/"+"\n")
              f.write("      username: admin"+"\n")
              f.write("      password: bad8462d0f904e79"+"\n")
              f.write("      project_name:"+tenantname.get()+"\n")
              f.write("      user_domain_name: Default"+"\n")
              f.write("      project_domain_name: Default")

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self,text="Define the tenants",font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        self.entries = []
        self.entry_vars = []

        for t in range(1, self.controller.num_tenants.get()+1):
            tenantname = tk.StringVar()
            tenant = tk.Entry(self, textvariable=tenantname)
            self.entry_vars.append(tenantname)
            self.entries.append(tenant)
            tenant.pack()
        button1 = tk.Button(self,text="Go to the start page",command=lambda: controller.show_frame("StartPage"),)
        button1.pack()
        button2 = tk.Button(self, text="Create the clouds.yml", command=self.getclouds)
        button2.pack()
错误如下所示:

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 135, in getclouds
    with open("{}/{}/clouds.yml".format(homepath,projectpath), "w") as f:
IOError: [Errno 2] No such file or directory: '0/0/clouds.yml'
主路径和项目名称的打印看起来像是什么都没有得到。
你知道怎么解决这个问题吗

您得到的错误
TypeError:+:'instance'和'str'的不支持的操作数类型
告诉您正在使用不支持的操作数类型。这可能是因为您正在尝试连接一个
实例
和一个
字符串
。这是不可能的,有一个简单的修复方法

更改:

homepath = self.controller.home
致:

也就是说,您不应该在Python中使用
+
组合字符串,因为它已被弃用。相反,正确的首选方法是使用
format()

因此,与其这样做,不如:

homepath+"/"+projectpath+"/clouds.yml"
这样做:

"{}/{}/clouds.yml".format(homepath, projectpath)

您返回的是IntVar,而不是它的值。您需要改用
homepath=self.controller.home.get()
。我已经尝试了您的代码,但仍然遇到错误。我已经更新了这个问题。@Albert从我看到的是你的价值观没有被拉出来。您确定在IntVar中设置了一些内容吗。另外,我也不是100%确定你能把一个目录设置成一个数字。你在用什么操作系统?我已经用你的建议更新了这个问题。我仍然得到问题中的错误。该脚本必须在Ubuntu和CentOS7上运行。在出现错误之前,我成功地创建了目录,插入了租户的名称,在按下createtheclouds.yml按钮之后,我从问题中得到了错误。知道会是什么吗?
"{}/{}/clouds.yml".format(homepath, projectpath)