Tkinter 文本:设置制表符空间不起作用 [英] Tkinter Text: Setting tab spaces not working

查看:25
本文介绍了Tkinter 文本:设置制表符空间不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码应该将按下时的制表符空格数设置为 4.问题是它没有这样做(而是 8 个).我查看了以前的 StackOverflow 问题,它们非常模糊.他们描述了使用 getPixels 函数,但从未解释如何使用它.加上 tkinter/tcl 文档很糟糕,所以我很困惑.

The following code is supposed to set the number of tab spaces when is pressed to 4. The problem is it doesn't do that (it does 8 instead). I have looked on previous StackOverflow questions, and they are extremely darn vague. They describe using the getPixels function, but never expalin how to use it. Plus tkinter / tcl documentation is terrible so I am confused.

import tkinter as tk

root = tk.Tk()
text = tk.Text(root)
text.pack()

def tab(arg):
    print("tab pressed")
    text.insert(tk.INSERT, " " * 4)

text.bind("<Tab>", tab)
root.mainloop()

推荐答案

您非常接近您所需要的.您需要做的就是将 return 'break' 添加到您的 tab(arg) 函数中.这将阻止 tkinter 进一步传播事件.见下文

You are very close to what you need. All you need to do is add return 'break' to your tab(arg) function. This will keep tkinter from propagating the event further. see below

import tkinter as tk

root = tk.Tk()
text = tk.Text(root)
text.pack()

def tab(arg):
    print("tab pressed")
    text.insert(tk.INSERT, " " * 4)
    return 'break'

text.bind("<Tab>", tab)
root.mainloop()

通过添加返回break",您可以阻止 tkinter 进一步传播事件.您将获得 8 个空格,因为它使用了制表符,然后您又在其上添加了 4 个空格.

By adding the return 'break' you stop tkinter from propagating the event further. You are getting 8 spaces, because it is taking the tab and then you are adding an additional 4 spaces on top of that.

希望这有帮助:)

这篇关于Tkinter 文本:设置制表符空间不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆