對於屏幕上的鍵盤,我需要26個按鈕,將鍵盤的一個字母添加到一個字符串。我得到的第一個問題是,按下按鈕後字符串不會被保存。例如,用戶按下'A','A'被打印並且應該被存儲。用戶現在按下'B','B'被打印並且'A'消失。第二個問題是該按鈕僅在第一次執行該功能。我的代碼所以下面遠了:按鈕excecu命令一次(Tkinter)
window = Tk()
window.attributes('-fullscreen', True)
window.configure(background='yellow')
master = Frame(window)
master.pack()
e = Entry(master)
e.pack()
e.focus_set()
nieuw_station = ""
def printanswer():
global nieuw_station
nieuw_station = e.get()
print(nieuw_station)
gekozen_station(nieuw_station)
invoeren = Button(master, text="Invoeren", width=10, command=printanswer)
invoeren.pack()
def letter_toevoegen(nieuw_station, letter):
nieuw_station += letter
print(nieuw_station)
return nieuw_station
a = Button(master, text="A", width=1, command=letter_toevoegen(nieuw_station, "a"))
a.pack()
b = Button(master, text="B", width=1, command=letter_toevoegen(nieuw_station, "b"))
b.pack()
window.mainloop()
預期結果將是:用戶按下「A」,「A」得到印刷&存儲。現在用戶按'B','AB'打印&存儲。最後用戶按'C','ABC'現在打印&存儲。無論何時用戶按下'invoeren'按鈕,它都會以新字符串作爲參數發送到下一個函數(這實際上有效)。
肯定不知道。要嘗試這種方式,謝謝你!很好的深入解釋btw! –