Fonts & Tinker

A

Angel

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.
 
P

Paul

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
 
R

Rick Johnson

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.
 
Å

Åukasz Posadowski

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:
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top