Fonts & Tinker

Discussion in 'Python' started by Angel, Jan 26, 2013.

  1. Angel

    Angel Guest

    I am changing the default font for a Tkinter application:

    class FuelControl(Tkinter.Frame):
    def __init__(self,master):
    self.version='0.02'
    self.font=tkFont.Font(family="Helvetica",size=18)
    print self.font.actual()
    ..
    ..
    ..

    and everything looks ok:

    {'family': 'Nimbus Sans L', 'weight': 'normal', 'slant': 'roman', 'overstrike': 0, 'underline': 0, 'size': 18}

    and the size of the text are 18 on the screen. Then a button creates a new window through this callback:

    def loc_add(self):
    addw=Tix.Tk()
    addw.title('Add location')
    print self.font.actual()
    Tkinter.Label(addw,text='Nickname:', font=self.font).grid(row=0,column=0)
    Tkinter.Label(addw,text='Fullname:', font=self.font).grid(row=1,column=0)
    Tkinter.Label(addw,text='Address:', font=self.font).grid(row=2,column=0)
    Tkinter.Label(addw,text='Fuel name:',font=self.font).grid(row=3,column=0)
    ....

    The self.font stays with the right value:

    {'family': 'Nimbus Sans L', 'weight': 'normal', 'slant': 'roman', 'overstrike': 0, 'underline': 0, 'size': 18}

    but the real displayed fonts in the window are smaller (default size of 12, maybe).

    Am I missing something?

    Thanks in advance,
    A.
    Angel, Jan 26, 2013
    #1
    1. Advertising

  2. Angel

    Angel Guest

    Dammm it should be Tkinter for subject..:D
    Angel, Jan 26, 2013
    #2
    1. Advertising

  3. Angel

    Paul Guest

    class FontSpec:
    """Wrapper for something like 'Arial 10 bold #red'
    """

    tkf = None # Tk Font
    spec = "" # specification
    tkspec = "" # specification for Tk
    family = None
    size = 0
    color = "black"
    weight = "normal"
    slant = "roman"
    underline = 0
    overstrike = 0
    linespace = 0
    descent = 0
    ascent = 0

    def __init__(self, spec=None):
    """spec: familty with capital letter, color with # (#red, ##FF00FF),
    size - int, other are styles"""
    try:
    if not spec:
    return

    spec = spec.split()

    family = [s for s in spec if s.istitle()]
    if family:
    self.family = family[0]
    spec.remove(self.family)

    color = [s for s in spec if s.startswith('#')]
    if color:
    self.color = color[0]
    spec.remove(self.color)
    self.color = self.color[1:]

    size = [s for s in spec if s.isdigit()]
    if size:
    self.size = size[0]
    spec.remove(self.size)
    self.size = int(self.size)

    if "bold" in spec:
    self.weight = "bold"

    if "italic" in spec:
    self.slant = "italic"

    if "underline" in spec:
    self.underline = 1

    if "overstrike" in spec:
    self.overstrike = 1

    # create tkFont for metrics
    self.tkf = tkFont.Font(family=self.family, size=self.size, weight=self.weight,
    slant=self.slant, underline=self.underline, overstrike=self.overstrike)

    self.ascent = self.tkf.metrics("ascent")

    self.descent = self.tkf.metrics("descent")

    self.linespace = self.tkf.metrics("linespace")

    # tkspec - specific. of font in Tk standard
    self.tkspec = []
    if self.family:
    self.tkspec.append(self.family)
    if self.size:
    self.tkspec.append(str(self.size))
    if self.weight == "bold":
    self.tkspec.append("bold")
    if self.slant == "italic":
    self.tkspec.append("italic")
    if self.underline:
    self.tkspec.append("underline")
    if self.overstrike:
    self.tkspec.append("overstrike")
    self.tkspec = " ".join(self.tkspec)

    except:
    raise ValueError("invalid font specification")

    def __str__(self):
    return self.tkspec

    --- only for ideas
    Paul, Jan 26, 2013
    #3
  4. Angel

    Rick Johnson Guest

    On Friday, January 25, 2013 10:41:36 PM UTC-6, Angel wrote:
    > I am changing the default font for a Tkinter application:
    >
    >
    >
    > class FuelControl(Tkinter.Frame):
    >
    > def __init__(self,master):
    >
    > self.version='0.02'
    >
    > self.font=tkFont.Font(family="Helvetica",size=18)
    >
    > print self.font.actual()



    You may want to check out these universal Tkinter widget methods:

    w.option_add(pattern, value, priority=None)
    w.option_clear()
    w.option_get(name, classname)
    w.option_readfile(fileName, priority=None)

    http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html

    While you are there, poke around the docs a bit because there is tons of good info you are going to need in the future. May want to get familiar with the new ttk widgets and themes.
    Rick Johnson, Jan 27, 2013
    #4
  5. Re: [SPAM] Fonts & Tinker

    Dnia 2013-01-25, piÄ… o godzinie 20:41 -0800, Angel pisze:
    > but the real displayed fonts in the window are smaller (default size of 12, maybe).
    >
    > Am I missing something?
    >
    > Thanks in advance,
    > A.
    >


    Did you tried this by simple:

    ---------------------------
    root = Tk()
    root.option_add('*Font', "Heveltica 14")
    ---------------------------

    We'll see if it's a local tkinter installation problem.



    --
    Åukasz Posadowski
    Åukasz Posadowski, Jan 28, 2013
    #5
  6. Angel

    Angel Guest

    THis one workd fine:

    ......option_add('*Font', "Heveltica 14")

    Thanks!

    Á.
    Angel, Jan 30, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Tanner

    Different fonts in multiline text box...

    Michael Tanner, Jul 25, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    589
    Steve C. Orr, MCSD
    Jul 25, 2003
  2. Guest
    Replies:
    1
    Views:
    536
    yuri vanzine
    Dec 29, 2003
  3. yuri vanzine

    Fonts installed on the web client???

    yuri vanzine, Dec 29, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    406
    Chris Jackson
    Dec 29, 2003
  4. Yuri Vanzine

    Fonts installed on the web client?

    Yuri Vanzine, Dec 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    338
    Yuri Vanzine
    Dec 30, 2003
  5. sam
    Replies:
    0
    Views:
    392
Loading...

Share This Page