0

我目前正在为回文检测器编写 GUI,但遇到了一些问题。我可以启动程序,并且 GUI 似乎很好。除了基本的按钮之外,所有按钮都可以使用:Testa Palindrom(翻译为测试回文)。

每当我单击该按钮时,我都会收到 NameError: global name 'palindromentry' is not defined。

您将在下面找到整个代码,但首先快速解释一下它的作用。

def ordnaText 降低文本并修复它以进行检查。def testap 使用 for 循环测试它是否是回文。def visaResultat 显示结果(这可以在 label1.config 中看到)。

我的问题是:为什么我会收到这个错误?我这辈子都想不通。

import tkinter
import tkinter.messagebox
def main():

    gui()
    tkinter.mainloop()

def gui():
    main_window = tkinter.Tk()
    top_frame = tkinter.Frame()
    mid_frame = tkinter.Frame()
    bottom_frame = tkinter.Frame()


    main_window.title("Palindromdetektor")
    main_window.geometry("400x400")



    label1 = tkinter.Label(top_frame, text = "Skriv in ett palindrom nedan för att testa det!",
                                bg = "green", width = 60, height = 6)
    button1 = tkinter.Button(mid_frame, text = "Testa palindrom", height = 3, width = 22,
                                bg = "Purple", command = mainaction) 

    button2 = tkinter.Button(bottom_frame, text= "Instruktioner", height = 3, width = 22,
                                bg = "Purple", command = messagebox) 

    button3 = tkinter.Button(bottom_frame, text ="Spara palindrom", height = 3, width = 22,
                                bg = "Purple") #command = sparapalindrom) 

    button4 = tkinter.Button(bottom_frame, text ="Avsluta programmet", height = 3, width = 22,
                                bg = "Purple", command=main_window.destroy)



    palindromentry = tkinter.Entry(mid_frame, width = 67)
    palindromentry.pack()

    top_frame.pack()
    mid_frame.pack()
    bottom_frame.pack()

    label1.pack()
    button1.pack()
    button2.pack()
    button3.pack()
    button4.pack()



def ordnaText(text):
        nytext = ("") 
        fixadText = text.lower() 
        for i in fixadText: 
            if i.isalnum(): 
                nytext = (nytext + i) 
        return nytext 



def testap(nytext):
        palindrom  = True 
        for i in range (0, len(nytext)):
            if (nytext[i]) != (nytext[len(nytext)-i-1]): 
                palindrom = False 
        return palindrom

def visaResultat(palindrom):
    if palindrom:
            label1.config(text="Ja, detta är ett palindrom!", bg="green")
    else:
            label1.config(text="Nej, detta är inte ett palindrom!", bg="red")

def messagebox():
        tkinter.messagebox.showinfo("Hjälp", "Detta är ett program som testar vare sig en text är ett palindrom eller inte.\n \
Skriv in din text i rutan och tryck på Testa Palindrom-knappen så får du se ditt resultat högst upp.\n \
Om du vill avsluta programmet klickar du på knappen som heter Avsluta programmet.\n \
Detta program är kodat av Olof Unge som nås via mail: olofunge@hotmail.com.\n \
Tack för att du använder detta program!")

#def sparapalindrom():
 #   try:
  #      if palindrom:
   #         myfile = open("palindrom.txt", "a")
    #        myfile.write(text\n)
     #       myfile.close()
      #  else:
       #     label1.config(text="Du kan bara spara godkända palindrom.")
    # except IOError:
     #   label1.config(text="Kunde inte hitta / skapa filen.")



def mainaction():
    global text
    text = palindromentry.get()
    ordnaText(text)
    testap(ordnaText(text))
    visaResultat(testap(ordnaText(text)))

main()

如果您能坚持主题而不发表任何评论,我将非常感激,因为其他一切都很好。非常感谢!

此致。

这是在 Python 3.0 中编码的

4

1 回答 1

1

palindromentrygui函数中定义。因此,它的范围仅限于该单一功能。当您单击按钮时,该函数被调用,但它对函数的范围mainaction一无所知。gui

这里有几个选项(按我的喜好排序):

  1. 使所有这些东西成为一个类,以便您可以在函数调用之间共享状态
  2. 将定义移动mainactiongui函数中(它需要在执行操作的按钮之前定义......)。
  3. 声明回文globalgui
于 2013-01-07T16:17:58.533 回答