NameError:全局名称'路径'没有定义

时间:2016-08-25 21:45:48

标签: python user-interface tkinter

我是学习GUI的初学者。我的python版本是2.7,我使用的是Windows

我该怎么做才能改变" path"的价值?点击按钮?

这是我的代码的一部分。 :)

class Sign_page(tk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        tk.Frame.__init__(self, parent)

        img_path = "C:/"
        Path = tk.Text(self, width = 45, height=1)
        Path.insert("end",img_path)
        Path.grid(row=2,column=0)

        ask_path = tk.Button(self, text = "...", command = lambda: asking_path(self))
        ask_path.grid(row=2,column=1)

此部分不在课堂上" Sign_page"

def asking_path(self):
    fileName = askopenfilename(initialdir = "C:/")
    img_path = fileName
    Path.delete("1.0","end")
    Path.insert("end",img_path)

2 个答案:

答案 0 :(得分:2)

不用担心这个错误。 django在最新版本中使用的是“路径”而不是“ URL”。 您只需要在url.py文件中导入路径 使用这个:-

从django.urls导入路径

答案 1 :(得分:0)

Path无法从asking_path访问,因为它未在范围内定义。一种解决方案是在Path中创建__init__时在实例上设置属性。通常,您应该为Python变量使用基于小写的变量名称(" path"),并为类名称使用标题大小写(" SignPage")。

class SignPage(tk.Frame):
    def __init__(self, parent, controller):
        ...
        path = tk.Text(self, width = 45, height=1)
        path.insert("end",img_path)
        path.grid(row=2,column=0)
        self.path = path
        ...

    def asking_path(self):
        ...
        self.path.delete("1.0","end")
        self.path.insert("end",img_path)