使用Python tkinter画布的上标问题 [英] Problems with superscript using Python tkinter canvas

查看:1272
本文介绍了使用Python tkinter画布的上标问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用canvas.create_text(...)来向图形添加文本。我使用unicode在以下方式有点成功:

I am trying to use canvas.create_text(...) to add text to a drawing. I have been somewhat successful using unicode in the following way:

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不会增加大小。工作,我假设它是相同的下标。此外,当我保存画布作为后记,问题上标已经走了...但是当我打印出保存的图像,上标返回。

It works, but when increasing the font size, superscripts 4,5,6,7,8,9,0 do not increase in size. Only 1,2,3 work. I'm assuming it's the same for subscripts. Also, when I save the canvas as a postscript, the problem superscripts are gone...but when I print out that saved image, the superscripts return.

我只是完全错了我的方法吗?我只是希望使这项工作,所以任何帮助将非常感谢你。

Am I just completely wrong with my approach? I'm just looking to make this work so any help would be greatly appreciated. Thank you.

推荐答案

您的问题来自于在您的平台和您的字体中处理Unicode
正如在维基百科:上标1,2和3以前在拉丁语-1
中处理,因此得到不同的字体支持。

Your problem comes from the handling of Unicode on your platform and in your fonts. As explained on wikipedia : superscript 1,2 and 3 where previously handled in Latin-1 and thus get different support in fonts.

我没有注意到固定大小的问题,但是在大多数字体上(在Linux和MacOS上),1,2,3都没有与4-9正确对齐。

I did not notice fixed size issue, but on most fonts (on Linux and MacOS), 1,2,3 are not properly aligned with 4-9.

我的建议是选择一个符合你需要的字体(你可以看看DejaVu系列,提供libre高品质的字体)。

My advice would be to select a font that match your need (you may look at DejaVu family, which provide libre high quality fonts).

这里是一个小应用程序,用于说明处理不同字体或大小的上标。

Here is a litte application to illustrate the handling of superscripts with different fonts or size.

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

这篇关于使用Python tkinter画布的上标问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆