所以,我从sublime text 2切换到Pycharm,并且它一直告诉我.get()在sublime中工作正常时需要2个参数(给定1个)。有人可以帮助我,我无法找到解决方案,但我可能只是找不到运气。这是我的代码:
import Tkinter
import codecs
import json
def write(): # def function to create file.json with encoding
print('Creating new text file')
name_with_ext = (E1.get()) + '.json' # name of file coerced with +.json
file_name = E1.get()
try:
with codecs.open(name_with_ext, 'a', 'utf8') as f:
f.write(json.dumps(file_name, sort_keys=True, ensure_ascii=False)) # encode
except:
print('Something went wrong! Can\'t tell what?')
Tkinter.sys.exit(0) # quit Python
def edit_gui():
global T1
global name_with_ext
name_with_ext = (E1.get()) + '.json' # name of file coerced with +.json
# gui for editing text
k = Tkinter.Tk()
T1 = Tkinter.Text(k, width=100, height=50)
T1.pack(side=Tkinter.RIGHT)
b3 = Tkinter.Button(k, text='Text to add:', command=edit_text)
b3.pack(side=Tkinter.BOTTOM)
k.mainloop()
def edit_text():
print('Editing text')
new_line = (T1.get()) # new entry variable
with open(name_with_ext, "r+") as f:
old = f.read()
f.seek(0)
if old is True:
f.write(old + ' ' + new_line)
else:
f.write(new_line)
# define mGUI for program
root = Tkinter.Tk()
E1 = Tkinter.Entry(root, bd=5)
E1.pack(side=Tkinter.RIGHT)
B1 = Tkinter.Button(root, text='create file:', command=write)
B1.pack(side=Tkinter.LEFT)
B2 = Tkinter.Button(root, text='edit file:', command=edit_gui)
B2.pack(side=Tkinter.LEFT)
root.mainloop()
write()
答案 0 :(得分:2)
你需要为get()方法提供索引。我假设您想要在文本框中获取所有文本。
new_line = (T1.get("1.0",'end-1c'))