我使用Tkinter文本框创建了文本编辑器。我需要输入单词,输入时。没有调用任何功能。我可以使用某些功能来获取输入详细信息。这是我的代码,
from tkinter import Tk, scrolledtext, Menu, filedialog, END, messagebox, simpledialog, StringVar
root = Tk(className=" Text Editor")
textArea = scrolledtext.ScrolledText(root, width=100, height=80)
def newFile():
if len(textArea.get('1.0', END+'-1c')) > 0:
if messagebox.askyesno("Save ?", "Do you wish to save ?"):
saveFile()
else:
textArea.delete('1.0', END)
root.title("TEXT EDITOR")
def openFile():
textArea.delete('1.0', END)
file = filedialog.askopenfile(parent=root, mode='rb', title='Select a text file', filetypes=(("Text file", "*.txt"), ("All files", "*.*")))
if file != None:
contents = file.read()
textArea.insert('1.0', contents)
file.close()
def saveFile():
file = filedialog.asksaveasfile(mode='w')
if file != None:
data = textArea.get('1.0', END+'-1c')
file.write(data)
file.close()
menu = Menu(root)
root.config(menu=menu)
fileMenu = Menu(menu)
menu.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="New", command=newFile)
fileMenu.add_command(label="Open", command=openFile)
fileMenu.add_command(label="Save", command=saveFile)
fileMenu.add_command(label="Find")
fileMenu.add_separator()
fileMenu.add_command(label="Exit")
helpMenu = Menu(menu)
menu.add_cascade(label="Help")
menu.add_cascade(label="About")
textArea.pack()
root.mainloop()
答案 0 :(得分:1)
您可以尝试以下方法:
from tkinter import Tk, scrolledtext, Menu, filedialog, END, messagebox, simpledialog, StringVar
root = Tk(className=" Text Editor")
textArea = scrolledtext.ScrolledText(root, width=100, height=80)
# --- added extra code for getting key pressed starts here --- #
word = ''
text = ''
def key(event):
# if you only need the character of the key pressed #
print("You Pressed: ", event.char)
# if you need the words after pressing ' '(space) key, or the total text #
global word, text
if event.char == ' ':
text = text + ' ' + word
print("Current Word You Entered: ", word)
print("Total Text is: ", text)
word = ''
else:
word += event.char
# binding key function to key pressed #
textArea.bind("<Key>", key)
# --- added extra code for getting key pressed ends here --- #
def newFile():
if len(textArea.get('1.0', END+'-1c')) > 0:
if messagebox.askyesno("Save ?", "Do you wish to save ?"):
saveFile()
else:
textArea.delete('1.0', END)
root.title("TEXT EDITOR")
def openFile():
textArea.delete('1.0', END)
file = filedialog.askopenfile(parent=root, mode='rb', title='Select a text file', filetypes=(("Text file", "*.txt"), ("All files", "*.*")))
if file != None:
contents = file.read()
textArea.insert('1.0', contents)
file.close()
def saveFile():
file = filedialog.asksaveasfile(mode='w')
if file != None:
data = textArea.get('1.0', END+'-1c')
file.write(data)
file.close()
menu = Menu(root)
root.config(menu=menu)
fileMenu = Menu(menu)
menu.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="New", command=newFile)
fileMenu.add_command(label="Open", command=openFile)
fileMenu.add_command(label="Save", command=saveFile)
fileMenu.add_command(label="Find")
fileMenu.add_separator()
fileMenu.add_command(label="Exit")
helpMenu = Menu(menu)
menu.add_cascade(label="Help")
menu.add_cascade(label="About")
textArea.pack()
root.mainloop()