2017-07-14 64 views
0

我想着色temp變量,但沒有運氣:應用標籤

from Tkinter import * 
root = Tk() 
text = Text(root) 
temp='Hello' 
text.insert(INSERT, "Hello %s" %temp) 
text.pack() 
x=1 
y=10 
text.tag_add("tag1", "1.4", "@%d,%d" %(x, y)) 
text.tag_config("tag1", background="blue", foreground="yellow") 
root.mainloop() 

而除了這樣做,如果有任何的方式,有沒有指定的方式指數使用變量xy?我認爲這可以解決我的問題。

Python 2.7版 - 視窗

+0

您是否知道'@'會將數字視爲像素? –

+0

我不知道'@'做了什麼。抱歉。但我發現了一個解決方案來獲取使用變量的索引,請參閱下面的答案。 –

回答

1

嗯,我能做到這一點來設置使用變量指標。

from tkinter import * 

root = Tk() 

text = Text(root) 
temp='Hello' 
text.insert(INSERT, "Hello %s" %temp) 
text.pack(expand=1, fill=BOTH) 

text.tag_configure("BOLD", foreground='green') 
def get_start(): 
    x=1 
    y=1 
    return '%d.%d' %(x,y) 
def get_end(): 
    x=1 
    y=7 
    return '%d.%d' %(x,y) 
def test(): 
    text.tag_add("BOLD", get_start(), get_end()) 

bold_btn = Button(root, text="Bold", command=test) 
bold_btn.pack(side="left") 


root.mainloop()