【问题标题】:Changing position of cursor in Entry widget在 Entry 小部件中更改光标的位置
【发布时间】:2015-07-03 00:37:28
【问题描述】:

我试图在按下按钮时将 Entry 小部件的光标向左移动。通过我的尝试,我可以获得光标的当前位置,但我无法移动它。
我在网上搜索,但没有找到任何有用的东西。如何在 Entry 小部件中移动光标位置?

#! /usr/bin/python
from Tkinter import *

class App(Frame):
    def __init__(self,parent):
        Frame.__init__(self,parent)
        self.parent=parent
        self.button = Button(self.parent, text="Shift_cursor_left", fg="red")
        self.button.pack(side=LEFT)
        self.entry_label=Entry(self.parent,width=10,bd="1",bg="cyan",font=("Helvetica",15),text="python",justify=RIGHT)
        self.entry_label.pack()
        self.entry_label.focus()
        self.entry_label.insert(0,"Python")

        self.button["command"]=self.shift_cursor()

    def shift_cursor(self):
        position = self.entry_label.index(INSERT)
        print position
        # self.entry_label.mark_set(INSERT,'1.2')

root=Tk()
app=App(root)
root.mainloop()

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    mark_set 是 Text 小部件的方法,对于 Entry 小部件使用 icursor 方法:

    self.entry_label.icursor(0)
    

    另外,Button 的命令应该是函数引用,而不是函数调用,所以改变

    self.button["command"]=self.shift_cursor()
    

    self.button["command"]=self.shift_cursor
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多