将动态用户输入插入 text() 框中
Inserting dynamic user input into text() box
我想弄清楚如何从一个 text() 框中获取用户输入,然后将其插入到另一个 text() 框中的已插入文本之间,并让它实时自动更新.
简化示例代码:
from Tkinter import *
root = Tk()
hello = Label(text="hello, what's your name?")
hello.grid(sticky=W)
mynameisLabel = Label(text="My name is:")
mynameisLabel.grid(row=1, sticky=W)
responseEntry = Text(width=40, height=1)
responseEntry.grid(row=1, sticky=E)
conclusionText = Text(width=40, height=5)
conclusionText.insert(END, "Ah, so your name is ")
# here is where I intend to somehow .insert() the input from responseEntry
conclusionText.insert(END, "?")
conclusionText.grid(row=2, columnspan=2)
root.mainloop()
我对此的解决方案是将文本小部件 responseEntry
绑定到要释放的键,然后有一个小功能,以便每次发生这种情况时,文本都会被重写。这就是它的样子:
from Tkinter import *
root = Tk()
hello = Label(text="hello, what's your name?")
hello.grid(sticky=W)
mynameisLabel = Label(text="My name is:")
mynameisLabel.grid(row=1, sticky=W)
responseEntry = Text(width=40, height=1)
responseEntry.grid(row=2, sticky=E)
conclusionText = Text(width=40, height=5)
conclusionText.insert(END, "Ah, so your name is ?")
conclusionText.grid(row=3, columnspan=2)
# This function is called whenever a key is released
def typing(event):
name = responseEntry.get("1.0",END) # Get string of our name
conclusionText.delete("1.0", END) # delete the text in our conclusion text widget
conclusionText.insert(END, "Ah, so your name is " + name[:-1] + "?") # Update text in conclusion text widget. NOTE: name ends with a new line
responseEntry.bind('<KeyRelease>', typing) # bind responseEntry to keyboard keys being released, and have it execute the function typing when this occurs
root.mainloop()
我想弄清楚如何从一个 text() 框中获取用户输入,然后将其插入到另一个 text() 框中的已插入文本之间,并让它实时自动更新.
简化示例代码:
from Tkinter import *
root = Tk()
hello = Label(text="hello, what's your name?")
hello.grid(sticky=W)
mynameisLabel = Label(text="My name is:")
mynameisLabel.grid(row=1, sticky=W)
responseEntry = Text(width=40, height=1)
responseEntry.grid(row=1, sticky=E)
conclusionText = Text(width=40, height=5)
conclusionText.insert(END, "Ah, so your name is ")
# here is where I intend to somehow .insert() the input from responseEntry
conclusionText.insert(END, "?")
conclusionText.grid(row=2, columnspan=2)
root.mainloop()
我对此的解决方案是将文本小部件 responseEntry
绑定到要释放的键,然后有一个小功能,以便每次发生这种情况时,文本都会被重写。这就是它的样子:
from Tkinter import *
root = Tk()
hello = Label(text="hello, what's your name?")
hello.grid(sticky=W)
mynameisLabel = Label(text="My name is:")
mynameisLabel.grid(row=1, sticky=W)
responseEntry = Text(width=40, height=1)
responseEntry.grid(row=2, sticky=E)
conclusionText = Text(width=40, height=5)
conclusionText.insert(END, "Ah, so your name is ?")
conclusionText.grid(row=3, columnspan=2)
# This function is called whenever a key is released
def typing(event):
name = responseEntry.get("1.0",END) # Get string of our name
conclusionText.delete("1.0", END) # delete the text in our conclusion text widget
conclusionText.insert(END, "Ah, so your name is " + name[:-1] + "?") # Update text in conclusion text widget. NOTE: name ends with a new line
responseEntry.bind('<KeyRelease>', typing) # bind responseEntry to keyboard keys being released, and have it execute the function typing when this occurs
root.mainloop()