我试图用canvas.create_text(...)将文本添加到绘图。 我已经有点成功以下列方式使用Unicode:
mytext = u'U\u2076' #U^6
canvas.create_text(xPos,yPos,text = mytext, font = ("Times","30")
canvas.pack()
它的工作原理,但增加了字体大小时,标4,5,6,7,8,9,0规模上不去。 只有1,2,3的工作。 我假设它是标相同。 此外,当我在画布另存为PostScript,这个问题上标都消失了......但是当我打印出保存图像,上标返回。
我只是完全错误的用我的方法? 我只是希望使这项工作,所以任何帮助,将不胜感激。 谢谢。
你的问题来自统一的操作平台上,并在您的字体。 如所解释的维基百科上 :上标1,2和3,其中先前在Latin-1的处理,并由此获得的字体不同的支撑。
我没有注意到固定大小的问题,但大多数字体(在Linux和MacOS),1,2,3不能正常使用4-9对齐。
我的建议是,选择符合您的需要(你可以看幻觉记忆的家庭,提供自由报高品质的字体)的字体。
这里是一个豆蔻应用程序来说明不同的字体或大小上标的的处理。
from Tkinter import *
import tkFont
master = Tk()
canvas = Canvas(master, width=600, height=150)
canvas.grid(row=0, column=0, columnspan=2, sticky=W+N+E+S)
list = Listbox(master)
for f in sorted(tkFont.families()):
list.insert(END, f)
list.grid(row=1, column=0)
font_size= IntVar()
ruler = Scale(master, orient=HORIZONTAL, from_=1, to=200, variable=font_size)
ruler.grid(row=1, column=1, sticky=W+E)
def font_changed(*args):
sel = list.curselection()
font_name = list.get(sel[0]) if len(sel) > 0 else "Times"
canvas.itemconfig(text_item, font=(font_name,font_size.get()))
#force redrawing of the whole Canvas
# dirty rectangle of text items has bug with "superscript" character
canvas.event_generate("<Configure>")
def draw():
supernumber_exception={1:u'\u00b9', 2:u'\u00b2', 3:u'\u00b3'}
mytext =""
for i in range(10):
mytext += u'U'+ (supernumber_exception[i] if i in supernumber_exception else unichr(8304+i))+" "
return canvas.create_text(50, 50,text = mytext, anchor=NW)
text_item = draw()
list.bind("<<ListboxSelect>>", font_changed)
font_size.trace("w", font_changed)
font_size.set(30)
master.grid_columnconfigure(0, weight=0)
master.grid_columnconfigure(1, weight=1)
master.grid_rowconfigure(0, weight=1)
master.grid_rowconfigure(1, weight=0)
master.mainloop()