【发布时间】:2018-04-06 11:32:09
【问题描述】:
我正在尝试为我的 GUI 编写代码。我想要一个可以在 Entry 小部件中添加字母的键盘。我很接近它,但问题是它在单击按钮时仅将字母“a”添加到条目中。
正如您在我的代码中看到的,我在命令中添加了“a”。 command=lambda: set_text('a') 当然这就是为什么它只打印'a'的原因。但是,如果我从 forloop 中获取字母并使 set_text(letter) 在 Entry 小部件中仅显示 H。
我还尝试删除第二个循环,将其更改为 set_text(lst[count]) 现在所有按钮都将“A”添加到条目中。
知道我做错了什么吗?
我的代码:
from tkinter import *
from ttk import *
def maakbuttons():
count = 0
lst = []
if count <= 7:
for letter in 'ABCDEFGH':
lst.append(letter)
for letter in lst:
Buttons = Button(master=root, text=letter, command=lambda: set_text('a'))
Buttons.place(x=20, y=30 +50 * count)
count+=1
def set_text(text):
a = e.get() + text
e.delete(0, len(e.get()))
e.insert(0, a)
def remove_letter():
last = len(e.get())-1
if last >= 0:
e.delete(last)
root= Tk()
a = root.wm_attributes('-fullscreen', 1)
e = Entry(root,width=10)
e.place(x=500, y=500)
maakbuttons()
root.mainloop()
【问题讨论】:
标签: python user-interface tkinter keyboard