2017-10-06 79 views
2

是否有可能在每行的兩側都在textwidget中證明兩個不同字符串?我嘗試了以下,但它不像預期的那樣工作。在tkinter文本小部件中左右調整字符串

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() 
+0

你想擁有的左邊和右邊的 'G' 的數量,對嗎?但我認爲不可能以不同的方式證明同一條線的兩個部分是合理的。 –

+0

是的,這是我想要的,壞的是不可能的... – Max2603

+0

我想你將不得不使用2個文本小部件,一個用於數字,一個用於文本。 –

回答

5

您可以使用右對齊的製表符來逐行執行此操作,就像您可能在文字處理器中執行操作一樣。

訣竅是,只要窗口改變大小,就需要重新設置tabstop。您可以使用<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() 

enter image description here

相關問題