所以,最近,我在 python Tkinter 上制作这个游戏,其中计算机给我一个随机单词,我必须输入我在给我的单词中看到的单词和字母,所以这里的问题是,“ans” 是一个输入小部件,我希望它的工作方式就像我输入完在给定单词中看到的单词或字母后,我应该按回车键,当我按回车键时,计算机应该看到小部件内写的任何内容,并且将其放入变量中,以便我可以将该变量键入为屏幕右侧(a1-a18)上显示的标签文本,按下 Enter 键后,它应该执行所有操作,并删除之前写入其中的所有内容条目小部件
def word():
rt = Tk()
rt.geometry("2445x990+0+0")
rt.title("the minion game")
tops = Frame(rt, width =1540, height = 10,bg = "steel blue", relief = SUNKEN)
tops.pack(side=TOP)
side = Frame(rt, width = 1540, height = 800, bg = "steel blue", relief = SUNKEN)
side.pack(side=LEFT)
sider = Frame(rt, width = 831, height = 800, bg = "steel blue", relief = SUNKEN)
sider.pack(side=RIGHT)
words = ["banana", "period", "bottle", "company", "pencil", "fishes"]
word = random.choice(words)
lblInfo = Label(tops, font=('arial',30,'bold', 'underline'), text =" P1 Your word is: " + word + " ", width = 67, justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
lblInfo.grid(row=0, column=0)
#left side
ans = tk.Entry(side,textvariable = name ,bg = "steel blue", font=('arial',15,'bold'),width = 40, insertwidth=4, bd=15, justify = "left").grid(row=3,column=0)
lblInfo = Label(side, font=('arial',50,'bold'), text = " Type here", width = 17,justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
lblInfo.grid(row=1, column=0)
lblInfo = Label(side, font=('arial',50,'bold', 'underline'), text = "", width = 9,justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
lblInfo.grid(row=2, column=0)
lblInfo = Label(side, font=('arial',50,'bold', 'underline'), text = "", width = 9,height = 5,justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
lblInfo.grid(row=4, column=0)
lblInfo = Label(side, font=('arial',50,'bold', 'underline'), text = "", width = 9,height = 3,justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
lblInfo.grid(row=0, column=0)
lblInfo = Label(side, font=('arial',5,'bold', 'underline'), text = "", width = 1,height = 35,justify = "left" , bg = "black", bd = 1, anchor = 'w')
lblInfo.grid(row=0, column=1)
lblInfo = Label(side, font=('arial',5,'bold', 'underline'), text = "", width = 1,height = 100,justify = "left" , bg = "black", bd = 1, anchor = 'w')
lblInfo.grid(row=4, column=1)
lblInfo = Label(side, font=('arial',5,'bold', 'underline'), text = "", width = 1,height = 13,justify = "left" , bg = "black", bd = 1, anchor = 'w')
lblInfo.grid(row=3, column=1)
lblInfo = Label(side, font=('arial',5,'bold', 'underline'), text = "", width = 1,height = 13,justify = "left" , bg = "black", bd = 1, anchor = 'w')
lblInfo.grid(row=1, column=1)
lblInfo = Label(side, font=('arial',5,'bold', 'underline'), text = "", width = 1,height = 15,justify = "left" , bg = "black", bd = 1, anchor = 'w')
lblInfo.grid(row=2, column=1)
#right side
lblInfo = Label(sider, font=('arial',30,'bold', 'underline'), text = "", width = 1,height = 2,justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
lblInfo.grid(row=0, column=0)
lblInfo = Label(sider, font=('arial',35,'bold'), text = "your words:", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
lblInfo.grid(row=1, column=0)
a1 = Label(sider, font=('arial',15,'bold'), text = "a1", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a1.grid(row=2, column=0)
a2 = Label(sider, font=('arial',15,'bold'), text = "a4", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a2.grid(row=3, column=0)
a3 = Label(sider, font=('arial',15,'bold'), text = "a3", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a3.grid(row=4, column=0)
a4 = Label(sider, font=('arial',15,'bold'), text = "a2", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a4.grid(row=5, column=0)
a5 = Label(sider, font=('arial',15,'bold'), text = "a5", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a5.grid(row=6, column=0)
a6 = Label(sider, font=('arial',15,'bold'), text = "a6", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a6.grid(row=7, column=0)
a7 = Label(sider, font=('arial',15,'bold'), text = "a7", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a7.grid(row=8, column=0)
a8 = Label(sider, font=('arial',15,'bold'), text = "a8", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a8.grid(row=9, column=0)
a9 = Label(sider, font=('arial',15,'bold'), text = "a9", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a9.grid(row=10, column=0)
a10 = Label(sider, font=('arial',15,'bold'), text = "a10", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a10.grid(row=11, column=0)
a11 = Label(sider, font=('arial',15,'bold'), text = "a11", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a11.grid(row=12, column=0)
a12 = Label(sider, font=('arial',15,'bold'), text = "a12", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a12.grid(row=13, column=0)
a13 = Label(sider, font=('arial',15,'bold'), text = "a13", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a13.grid(row=14, column=0)
a14 = Label(sider, font=('arial',15,'bold'), text = "a10", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a14.grid(row=15, column=0)
a15 = Label(sider, font=('arial',15,'bold'), text = "a11", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a15.grid(row=16, column=0)
a16 = Label(sider, font=('arial',15,'bold'), text = "a12", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a16.grid(row=17, column=0)
a17 = Label(sider, font=('arial',15,'bold'), text = "a13", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a17.grid(row=18, column=0)
a18 = Label(sider, font=('arial',15,'bold'), text = "a13", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
a18.grid(row=19, column=0)
如果有人知道如何做到这一点以及将其添加到此代码中的何处,请告诉我。
谢谢你
试试这个:
def word():
global ans
...
ans = Entry(side, ...)
ans.grid(...)
ans.bind("<Return>", enter_pressed)
...
def enter_pressed(event):
print(ans.get())
ans.delete(0, "end")
我还建议你看一些编程教程和这个。
要绑定返回/回车键,请使用
entry.bind("<Return>", func)
:
from tkinter import *
class UI:
def __init__(self):
self.entry = Entry(root)
# Bind the return key
self.entry.bind("<Return>", self.func)
self.entry.pack()
def func(self, event):
print(self.entry.get())
root = Tk()
UI()
root.mainloop()
这是一种不使用
global
(因为我听说它们是“邪恶的”)的方法:
from tkinter import Entry, Tk
def func(event):
print(event.widget.get())
event.widget.delete(0, 'end')
def main():
root = Tk()
entry = Entry(root)
entry.pack()
entry.bind('<Return>', func)
root.mainloop()
main()