在插入点光标位置添加一个字符串 - Python

时间:2014-11-26 20:28:00

标签: python tkinter

我正在使用Tkinter在python上构建一个计算器,我想用我的计算器屏幕键盘添加数字,总是在光标所在的位置(就像Ubuntu的计算器一样)。

我一直在搜索很多内容,但除了tk.constant INSERT之外,我似乎找不到任何有关此问题的信息。至少我使用它的方式:

class MyClass
    self.string = StringVar()

    # trim...

    def addChar(self, val):
        string1=self.string.get()[:INSERT]
        string2=self.string.get()[INSERT:]
        self.string.set(string1 + val + string2)

1 个答案:

答案 0 :(得分:3)

要在Entry小部件的插入光标处插入字符或字符串,请使用insert方法,索引"insert"或tkinter常量INSERT(我更喜欢前者) :

self.e = tk.Entry(...)
...
self.e.insert("insert", "new text")
相关问题