如何在tkinter中为textbox编写事件?

2024-10-04 11:25:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我用tkinter用Python3.6.2编写了一个程序,编写了以下代码:

from tkinter import *
root=Tk()
def retrieve_input():
    inputValue=textBox.get("1.0","end-1c")
    print(inputValue)

textBox=Text(root, height=2, width=10)
textBox.pack()
buttonCommit=Button(root, height=1, width=10, text="Commit", 
                    command=lambda: retrieve_input())
buttonCommit.pack()
mainloop()

我想在文本框中按回车键打印“你在文本框中按回车”。 如何编写事件?在


Tags: 代码fromimport程序inputtkinterrootwidth
1条回答
网友
1楼 · 发布于 2024-10-04 11:25:51

看看这个

from tkinter import *
from tkinter import messagebox

root=Tk()

def alertme(*args):
    messagebox.showinfo("Message", "you press Enter in textbox")

def retrieve_input():
    inputValue=textBox.get("1.0","end-1c")
    print(inputValue)

textBox=Text(root, height=2, width=10)
textBox.pack()

textBox.bind("<Return>", alertme) #this line calls alertme function whenever you press Enter key

buttonCommit=Button(root, height=1, width=10, text="Commit", 
                    command=lambda: retrieve_input())
buttonCommit.pack()
mainloop()

现在它只能在文本框中工作。。。在

相关问题 更多 >