如何在输入后在画布中显示提示

时间:2017-03-18 02:09:47

标签: python tkinter tkinter-canvas

我正在尝试制作一个有文字限制的论坛程序。当我尝试运行我创建的代码时,它表示当我在上面明确定义时没有定义text1。

 def forum():
     mk = Tk()
     canvas = Canvas(mk, width=425, height=425, bg ='grey')
     canvas.pack()
     Labelforum1 = Label(mk, text="Forum", bg='gray', font=('Times', 30))
     Labelforum1.pack()
     Labelforum1.place(relx=0.5, rely=0.1, anchor=CENTER)
     Labelforum2 = Label(mk, text="Name:", bg='gray')
     Labelforum2.pack()
     Labelforum2.place(relx=0.11, rely=0.22, anchor=CENTER)
     Entryforum1 = Entry(mk, bd =5, width=20)
     Entryforum1.pack()
     Entryforum1.place(relx=0.40, rely=0.22, anchor=CENTER)
     Labelforum3 = Label(mk, text="Prompt:", bg='gray')
     Labelforum3.pack()
     Labelforum3.place(relx=0.12, rely=0.33, anchor=CENTER)
     text1 = Text(mk, width=50, height=6)
     text1.pack()
     text1.place(relx=0.49, rely=0.50, anchor=CENTER)
     Buttonforforum = Button(mk, text="Post", bg ='grey', command=begin)
     Buttonforforum.pack()
     Buttonforforum.place(relx=0.9, rely=0.70, anchor=CENTER)
     bfforum = Button(mk, text="Go to message board", command=begin)
     bfforum.pack()
     bfforum.place(relx=0.5, rely=0.86, anchor=CENTER)
     labelc = Label(mk, text="(200 character limit)", bg = 'grey')
     labelc.pack()
     labelc.place(relx=0.19, rely=0.69, anchor=CENTER)

def begin():
    answer = text1.get("1.0", "end-1c")
    if len(answer) <= 10:
        label = Label(mk, text="This prompt is being added to the forum")
        label.pack()
        label.place(relx=0.5, rely=0.75, anchor=CENTER)
    else:
        label1 = Label(mk, text="This prompt is too long")
        label1.pack()
        label1.place(relx=0.5, rely=0.75, anchor=CENTER)

为什么它会给我这个错误。非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

正如Bryan Oakley在评论中所说,text1是一个局部变量,所以它只存在于forum函数中。要在begin函数中使用它,您需要将其声明为全局变量。这同样适用于mk

以下是将mktext1声明为全局变量的方法:

def forum():
     global text1, mk
     mk = Tk()
     # ...

编辑:无错误运行的完整代码

from tkinter import *

def forum():
    global mk, text1
    mk = Tk()
    canvas = Canvas(mk, width=425, height=425, bg ='grey')
    canvas.pack()
    Labelforum1 = Label(mk, text="Forum", bg='gray', font=('Times', 30))
    Labelforum1.pack()
    Labelforum1.place(relx=0.5, rely=0.1, anchor=CENTER)
    Labelforum2 = Label(mk, text="Name:", bg='gray')
    Labelforum2.pack()
    Labelforum2.place(relx=0.11, rely=0.22, anchor=CENTER)
    Entryforum1 = Entry(mk, bd =5, width=20)
    Entryforum1.pack()
    Entryforum1.place(relx=0.40, rely=0.22, anchor=CENTER)
    Labelforum3 = Label(mk, text="Prompt:", bg='gray')
    Labelforum3.pack()
    Labelforum3.place(relx=0.12, rely=0.33, anchor=CENTER)
    text1 = Text(mk, width=50, height=6)
    text1.pack()
    text1.place(relx=0.49, rely=0.50, anchor=CENTER)
    Buttonforforum = Button(mk, text="Post", bg ='grey', command=begin)
    Buttonforforum.pack()
    Buttonforforum.place(relx=0.9, rely=0.70, anchor=CENTER)
    bfforum = Button(mk, text="Go to message board", command=begin)
    bfforum.pack()
    bfforum.place(relx=0.5, rely=0.86, anchor=CENTER)
    labelc = Label(mk, text="(200 character limit)", bg = 'grey')
    labelc.pack()
    labelc.place(relx=0.19, rely=0.69, anchor=CENTER)

def begin():
    answer = text1.get("1.0", "end-1c")
    if len(answer) <= 10:
        label = Label(mk, text="This prompt is being added to the forum")
        label.pack()
        label.place(relx=0.5, rely=0.75, anchor=CENTER)
    else:
        label1 = Label(mk, text="This prompt is too long")
        label1.pack()
        label1.place(relx=0.5, rely=0.75, anchor=CENTER)

if __name__ == "__main__":
    forum()
    mk.mainloop()