2

是否可以在每行两侧的文本小部件中证明两个不同的字符串是合理的?我尝试了以下方法,但它没有像预期的那样工作。

from tkinter import *

root = Tk()

t = Text(root, height=27, width=30)
t.tag_configure("right", justify='right')
t.tag_configure("left", justify='left')
for i in range(100):
    t.insert("1.0", i)
    t.tag_add("left", "1.0", "end")
    t.insert("1.0", "g\n")
    t.tag_add("right", "1.0", "end")
t.pack(side="left", fill="y")

root.mainloop()
4

1 回答 1

7

您可以使用右对齐制表位逐行执行此操作,就像您在文字处理器中执行此操作一样。

诀窍是,每当窗口改变大小时,您都需要重置制表位。您可以使用<Configure>在窗口大小更改时调用的绑定来执行此操作。

例子:

import tkinter as tk

def reset_tabstop(event):
    event.widget.configure(tabs=(event.width-8, "right"))

root = tk.Tk()
text = tk.Text(root, height=8)
text.pack(side="top", fill="both", expand=True)
text.insert("end", "this is left\tthis is right\n")
text.insert("end", "this is another left-justified string\tthis is another on the right\n")

text.bind("<Configure>", reset_tabstop)
root.mainloop()

在此处输入图像描述

于 2017-10-06T12:09:08.380 回答