tkinter 文本输入小部件的 .get 方法缺少参数错误 [英] Missing argument error for .get method of tkinter text entry widget
问题描述
我正在尝试为文本编辑器运行以下代码.
I am attempting to run the following code for a text editor.
def newfile():
current = None
def create_file(entry):
nonlocal current
current = open(entry.get(),'w')
e.master.destroy()
chdir(askdirectory())
name=Tk()
name.title("Name the File?")
prompt=Label(name, text="Enter name for new file:")
prompt.grid(row=0)
e=Entry(name)
e.grid(row=1)
e.insert(0, "Untitled")
create=Button(name, text="Create", command = lambda: create_file(e))
create.grid(row=2, column=3)
name.mainloop()
return current
但我收到此错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__
return self.func(*args)
File "<pyshell#1>", line 15, in <lambda>
create=Button(name, text="Create", command = lambda: create_file(e))
File "<pyshell#1>", line 5, in create_file
current = open(entry.get(),'w')
TypeError: an integer is required
它需要一个整数参数.有人知道那是什么吗?
It wants an integer argument. Does anyone know what that is?
推荐答案
Entry 小部件的实例不需要 get
方法的任何参数.你正确地调用它.标准的 open
命令也不需要整数.我的猜测是,entry
或 open
之一不是您认为的那样.也许您有一个方法或另一个具有其中一个名称的对象?
An instance of the Entry widget does not require any arguments for the get
method. You are calling it correctly. Neither does the standard open
command require an integer. My guess is, one of entry
or open
is not what you think it is. Maybe you have a method or another object with one of those names?
我建议将调用 get
和 open 放在不同的行上,以确保您知道该语句的哪一部分引发了错误:
I suggest putting the call to get
and the open on separate lines, to make sure you know which part of that statement is throwing the error:
text = entry.get()
current = open(text, 'w')
这篇关于tkinter 文本输入小部件的 .get 方法缺少参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!