Tkinter canvas: text object font size?

I am making pretty pictures using tkinter canvas and overlaying text over the circles like in the following image:

http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2012/11/6/1352220546059/Causes-of-deaths-graphic-008.jpg

I want the font size to depend on the same number that the circle size depends on.

tempfont = tkFont.Font(family='Helvetica',size=int(round(ms*topnode[1])))
self.display.create_text(center[0],center[1],fill = "#FFFFFF",text = int(round(ms*topnode[1])),font = tempfont)

      

My problem is that when I use the above code, the overlaid text is a constant size for each text object. The text itself is correct as it displays the number I want the font size to be, just not in the correct font size. I've experimented with putting constant integers in the sizing (works as it intended) and adding del (tempfont) right after the above two lines of code, but I haven't found yet what else fixes this issue.

What am I doing wrong?

There is a separate small program here that reproduces the problem:

from Tkinter import *
import tkFont

class TestApp(Frame):
    def __init__(self, master=None, height = 160, width = 400):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.display = Canvas(self, width = 800, height = 320, bg = "#FFFFFF")
        self.display.grid(row=0,column=0)

        def recurtext(tsize):
            if tsize > 20:
                recurtext(tsize-10)
            tempfont = tkFont.Font(family='Helvetica',size=tsize)
            self.display.create_text(800 - (tsize*12),160, text = str(tsize), font = tempfont)

        recurtext(60)

app = TestApp()
app.master.title("Test")
app.mainloop()

      

The bottom line is that it recurtext

resizes the font recursively, but shows

writes out the font size at that size ... or I think that's what it needs. It might be a bug with tkinter, but I still hope I'm the one who got it wrong in the logic here.

+3


source to share


1 answer


I have never encountered this behavior before; this looks like a Tkinter bug. The good news is that this appears to be a workaround. If you give each font a unique name, the problem goes away.

The following example shows multiple lines, each with a different font size:



import Tkinter as tk
import tkFont

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.display = tk.Canvas(self, width=400, height=600, background="black")
        self.display.pack(side="top", fill="both", expand=True)
        y = 10
        for size in range (2, 38, 2):
            tempfont = tkFont.Font(family='Helvetica',size=size, 
                                   name="font%s" % size)
            self.display.create_text(10, y, fill = "#FFFFFF",text = size, 
                                     font = tempfont, anchor="nw")
            y = y + tempfont.metrics()["linespace"]

if __name__ == "__main__":
    root = tk.Tk()
    frame = Example(parent=root)
    frame.pack(side="top", fill="both", expand=True)

    root.mainloop()

      

+1


source







All Articles