[英]How to use textfield key press event using python
如果我在文本字段上寫一些東西並按鍵盤上的 Enter 鍵。我在文本字段顯示消息框中輸入的內容。 這一點得到錯誤 if(format(k=event.char(13)))) :如果我設置了輸入鍵代碼。 我在下面添加了完整的代碼。
from tkinter import *
root = Tk()
root.geometry("800x800")
global e1
def callback(event):
if(format(k=event.char(13)))):
msg = e1.get()
print(msg)
Label(root, text="Student Name").place(x=140, y=40)
e1 = Entry(root)
e1.place(x=140, y=10)
e1.bind('<Key>',callback)
root.mainloop()
試試這個
from tkinter import *
root = Tk()
root.geometry("800x800")
def callback(event):
msg = e1.get()
print(msg)
Label(root, text="Student Name").place(x=140, y=40)
e1 = Entry(root)
e1.place(x=140, y=10)
e1.bind('<Return>',callback) #<Return> is equivalent to your Enter key
root.mainloop()
當您在輸入小部件上單擊 Enter 鍵時,將調用該函數並打印輸出。 我還刪除了global
因為在外部函數中使用它是沒有意義的。
希望能幫到你。
干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.