【问题标题】:Changing Backspace effect in Tkinter text widget在 Tkinter 文本小部件中更改退格效果
【发布时间】:2016-11-28 09:41:06
【问题描述】:

我正在寻找一种方法来更改 Tkinter 文本小部件中的退格键效果。我正在从 Tkinter 监视文本窗口中的关键事件,我需要找到一种方法:

  • 禁用 Backspace 键的效果,但仍然可以知道它是输入的

  • 删除用户打算删除的字符

我不知道从哪里开始。我可以挑出 Backspace 事件,但不知道如何防止它删除字符。我也不知道如何实时影响输入(在用户书写时对字符应用删除线“样式”)

这是我现在所拥有的,但这并不多:

from Tkinter import *
import tkFileDialog
root=Tk()
root.title("TextnonEdit")
text=Text(root)
text.grid()

def keepit(key):
    print key.keysym
    if key.keysym == "BackSpace":
        print "do not delete, strikethrough"

# Bind entry to keypress
text.bind("<Key>", keepit)

def saveas():
    global text
    t = text.get("1.0", "end-1c")
    savelocation=tkFileDialog.asksaveasfilename()
    file1=open(savelocation, "w+")
    file1.write(t)
    file1.close()

button=Button(root, text="Save", command=saveas)
button.grid()
root.mainloop()

提前感谢您的任何回答!

【问题讨论】:

    标签: python replace tkinter backspace strikethrough


    【解决方案1】:

    问题可以通过以下方式解决:

    1. 在文本小部件上创建一个标签,并将 overstrike 属性设置为 true
    2. 在退格字符上创建绑定以应用标记
    3. 让绑定函数返回"break" 以防止默认行为

    示例:

    import Tkinter as tk
    
    class Example(tk.Frame):
        def __init__(self, parent):
            tk.Frame.__init__(self, parent)
    
            self.text = tk.Text(self)
            self.text.pack(fill="both", expand=True)
    
            # add a tag that lets us apply the overstrike attribute
            self.text.tag_configure("overstrike", overstrike=True)
    
            # add a binding on the backspace key
            self.text.bind("<BackSpace>", self.handleBackspace)
    
        def handleBackspace(self, event):
            # add the overstrike to the character before the
            # insertion cursor
            self.text.tag_add("overstrike", "insert-1c", "insert")
    
            # move the cursor to the previous position
            self.text.mark_set("insert", "insert-1c")
    
            # prevent the default behaviour
            return "break"
    
    if __name__ == "__main__":
        root = tk.Tk()
        Example(root).pack(fill="both", expand=True)
        root.mainloop()
    

    【讨论】:

      【解决方案2】:

      谢谢布赖恩!这是我经过一些调整后得到的结果,以使其能够连续使用多个退格键并删除选择:

      import Tkinter as tk
      
      x = 0
      
      class Example(tk.Frame):
          def __init__(self, parent):
              tk.Frame.__init__(self, parent)
              self.text = tk.Text(self)
              self.text.pack(fill="both", expand=True)
              # add a tag that lets us apply the overstrike attribute
              self.text.tag_configure("overstrike", overstrike=True)
      
      
              self.text.bind("<Key>", self.eyeout)
      
              # add a binding on the backspace key
              self.text.bind("<BackSpace>", self.handleBackspace)
      
          def eyeout(self, key):
              global x
              print key.keysym
              # move the cursor to the previous position
              self.text.mark_set("insert", "insert+" + str(x) + "c")
      
              print "insert+" + str(x) + "c"
      
              #reset consecutive backspace count to zero
      
              x = 0
      
          def handleBackspace(self, event):
      
              if self.text.tag_ranges(tk.SEL):
                  print ('Selected text !')
                  #if text is selected, overstrike all the selection
                  self.text.tag_add("overstrike", "sel.first", "sel.last")
                  # move cursor to end of selection
                  self.text.mark_set("insert", "sel.last")
                  # deselect to avoid overwrite
                  self.text.tag_remove(tk.SEL, "sel.first", "sel.last")
                  # counting doesn't apply to selection, reset backspace count
                  global x
                  x = 0
              else:
                  print('NO Selected Text')
                  # add the overstrike to the character before the
                  # insertion cursor
                  self.text.tag_add("overstrike", "insert-1c", "insert")
                  # counting system to know how many backspaces in a row were entered
                  print "Backspace!"
                  print x
                  global x
                  x = x+1
                  print x
                  print "Backspace increase"
                  self.text.mark_set("insert", "insert-1c")
      
              # prevent the default behaviour
              return "break"
      
      
      if __name__ == "__main__":
          root = tk.Tk()
          Example(root).pack(fill="both", expand=True)
          root.mainloop()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多