无法从Python3.7 Entry字段使用.get()

时间:2019-08-20 08:59:21

标签: python python-3.x tkinter

我只想创建一个程序,这样,当我单击“打印”按钮时,它将从输入字段中获取数据并输出到下面的文本字段中。我对.get()的使用是否有问题?不确定是什么问题吗?

import tkinter as tk
from tkinter import *

wd=tk.Tk()

def inkq():
    string= str(nd.get())
    ketqua.insert(END, string)

nd=Entry(wd).pack()
btn=Button(wd, text ="Print", command = inkq).pack()
btn1=Button(wd, text ="Quit", command = wd.destroy).pack()
ketqua=Text(wd, height =10, width = 40).pack()

wd.mainloop()

1 个答案:

答案 0 :(得分:1)

问题是.pack()返回None,它没有get方法。 从我运行代码后的以下错误打印中可以看出。

AttributeError:'NoneType'对象没有属性'get'

import tkinter as tk
from tkinter import *

wd=tk.Tk()

def inkq():
    string= str(nd.get())
    ketqua.insert(END, string)
nd=Entry(wd) #Save entry befor packing
nd.pack()

btn=Button(wd, text ="Print", command = inkq).pack()
btn1=Button(wd, text ="Quit", command = wd.destroy).pack() 
ketqua=Text(wd, height =10, width = 40) #Save entry before packing
ketqua.pack()

wd.mainloop()