您的問題來自您的平臺和字體上的Unicode處理。 如解釋on wikipedia:上標1,2和3以前處理拉丁文-1 ,從而得到不同的字體支持。
我沒有注意到固定大小的問題,但在大多數字體(在Linux和MacOS上),1,2,3與4-9沒有正確對齊。
我的建議是選擇一種符合您需要的字體(您可以查看DejaVu系列,它提供免費的高質量字體)。
這是一個litte應用程序來說明處理不同字體或大小的上標。
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()
工程很漂亮。謝謝! – 2012-04-13 12:56:58