如何将滚动文本右对齐?

How can I align scrolled text to the right?

问题很简单。滚动的文本框将以阿拉伯语写入其中。 因此,每当我在框的右侧键入时,我都需要对齐文本。没有任何属性,例如 justify。我也用了标签,但是我做不到。

show_note = scrolledtext.ScrolledText(canvas, width=int(monitorWidth / 65), height=int(monitorHeight / 130),
                                      font=("Times New Roman", 14))

canvas.create_window(cord(0.45, 0.65), window=show_note, anchor="ne")

是否有滚动文本控件的替代品,但具有这些属性(宽度、高度、对齐并可以写入多行)?

我做了一些搜索,找不到任何其他方法来做到这一点,除了将每个按键绑定到一个函数,adds/edits 标签到 select 文本框中的所有项目:

from tkinter import *
from tkinter import scrolledtext

root = Tk()

def tag(e):
    text.tag_add('center','1.0','end')

text = scrolledtext.ScrolledText(root)
text.pack()
text.tag_config('center',justify='right')

text.bind('<KeyRelease>',tag)

root.mainloop()

您也可以制作执行此操作的自定义小部件,而不是为每个文件重复代码,因此更好的形式是:

from tkinter import *
from tkinter import scrolledtext

root = Tk()

class JustifyText(scrolledtext.ScrolledText):
    def __init__(self,master,*args,**kwargs):
        scrolledtext.ScrolledText.__init__(self,master,*args,**kwargs)
        self.tag_config('center',justify='right')
        
        self.bind('<KeyRelease>',self.tag)

    
    def tag(self,e):
        text.tag_add('center','1.0','end')

text = JustifyText(root)
text.pack()


root.mainloop()

是的,您可以通过采用文本框的当前宽度并将插入光标移动到该点,使其从该点开始,从而使其更加完美。