无法调用Tkinter Entry小部件的get方法

时间:2017-03-26 14:30:35

标签: python tkinter

我有这个错误:

  text = e1.get()
AttributeError: 'NoneType' object has no attribute 'get'

当我运行此代码时:

from Tkinter import *
import subprocess
import os

master = Tk()
master.geometry("1000x668")
master.title("Menu")
master.configure(background='pale green')
#master.iconbitmap(r"C:\Users\André\Desktop\python\menu.ico")
w = Label(master, text="Abrir", bg="pale green", fg="steel blue", font=("Algerian", 20, "bold"))
w.pack()
w.place(x=100, y=0)

def notepad():
    subprocess.Popen("notepad.exe")

buttonote = Button(master, text="Bloco de notas", wraplength=50, justify=CENTER, padx=2, bg="light sea green", height=2, width=7, command=notepad)
buttonote.pack()
buttonote.place(x=0, y=50)

def regedit():
    subprocess.Popen("regedit.exe")

buttonreg = Button(master, text="Editor de Registo", wraplength=50, justify=CENTER, padx=2, bg="light sea green", height=2, width=7, command=regedit)
buttonreg.pack()
buttonreg.place(x=60, y=50)

def skype():
    subprocess.Popen("skype.exe")

buttonskype = Button(master, text="Skype", bg="light sea green", height=2, width=7, command=skype)
buttonskype.pack()
buttonskype.place(x=120, y=50)

def steam():
    os.startfile("D:\Steam\Steam.exe")

buttonsteam = Button(master, text="Steam", bg="light sea green", height=2, width=7, command=steam)
buttonsteam.pack()
buttonsteam.place(x=178, y=50)

def save():
    text = e1.get()
    SaveFile = open('information.txt','w')
    SaveFile.write(text)
    SaveFile.close()

e1 = Entry(master, width=15)
e1.pack(padx=100,pady=4, ipadx=2)
nome = Label(master, text="Nome?", bg="pale green", fg="steel blue", font=("Arial Black", 12))
nome.pack()
nome.place(x=380, y=0)

buttonsave = Button(master, text="Guardar", bg="light sea green", height=1, width=6, command=save)
buttonsave.pack()
buttonsave.place(x=550, y=0)

mainloop ()

我在很多网站上搜索但找不到解决方案。如果有人帮我解决这个问题,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

Foo.Companion.someHelper()

它会创建一个e1 = Entry(master, width=15).pack(padx=100,pady=4, ipadx=2) 小部件,然后立即调用其Entry方法,该方法不会返回任何内容,因此pack()会被赋值e1

要修复它,只需将该行分成两个单独的语句:

None