如何在 Tkinter 中将来自 Entry 的输入拆分为单个字符?

How to split an input from an Entry, in Tkinter, into individual characters?

我正在用 Tkinter 制作一个加密程序,需要将输入到条目中的单词分隔成单独的字符。

例如,如果用户输入单词“monkey”, 它会被放入这样的数组中 seperatedWord = ["m","o","n","k","e","y"]

我该怎么做?

如 acw1668 所评论。

separatedWord = list(input_word)

你可以typecast一个字符串作为列表

试试这个代码:

from tkinter import *


class MyEntry(Entry):
    def __init__(self, root, textvariable):
        Entry.__init__(self, master=root, textvariable=textvariable)

        #binding your trace handler to your textvariable
        textvariable.trace_add("write", self._traceHandler)

    #or just use this handler
    def _traceHandler(self, x, y, z):
        # code block
        print(self.getSeparatedWord())

    #you can call this
    def getSeparatedWord(self):
        value = self.get()
        return list(value)


root = Tk()
my_textvar = StringVar()
my_entry = MyEntry(root, my_textvar)
my_entry.pack()
root.mainloop()