]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FontDialog.py
3 from wx
.lib
import stattext
5 #---------------------------------------------------------------------------
7 class TestPanel(wx
.Panel
):
8 def __init__(self
, parent
, log
):
9 wx
.Panel
.__init
__(self
, parent
, -1)
12 btn
= wx
.Button(self
, -1, "Select Font")
13 self
.Bind(wx
.EVT_BUTTON
, self
.OnSelectFont
, btn
)
15 self
.sampleText
= stattext
.GenStaticText(self
, -1, "Sample Text")
16 self
.sampleText
.SetBackgroundColour(wx
.WHITE
)
18 self
.curFont
= self
.sampleText
.GetFont()
19 self
.curClr
= wx
.BLACK
21 fgs
= wx
.FlexGridSizer(cols
=2, vgap
=5, hgap
=5)
26 fgs
.Add(self
.sampleText
, 0, wx
.ADJUST_MINSIZE|wx
.GROW
)
28 fgs
.Add((15,15)); fgs
.Add((15,15)) # an empty row
30 fgs
.Add(wx
.StaticText(self
, -1, "PointSize:"))
31 self
.ps
= wx
.StaticText(self
, -1, "")
32 font
= self
.ps
.GetFont()
33 font
.SetWeight(wx
.BOLD
)
35 fgs
.Add(self
.ps
, 0, wx
.ADJUST_MINSIZE
)
37 fgs
.Add(wx
.StaticText(self
, -1, "Family:"))
38 self
.family
= wx
.StaticText(self
, -1, "")
39 self
.family
.SetFont(font
)
40 fgs
.Add(self
.family
, 0, wx
.ADJUST_MINSIZE
)
42 fgs
.Add(wx
.StaticText(self
, -1, "Style:"))
43 self
.style
= wx
.StaticText(self
, -1, "")
44 self
.style
.SetFont(font
)
45 fgs
.Add(self
.style
, 0, wx
.ADJUST_MINSIZE
)
47 fgs
.Add(wx
.StaticText(self
, -1, "Weight:"))
48 self
.weight
= wx
.StaticText(self
, -1, "")
49 self
.weight
.SetFont(font
)
50 fgs
.Add(self
.weight
, 0, wx
.ADJUST_MINSIZE
)
52 fgs
.Add(wx
.StaticText(self
, -1, "Face:"))
53 self
.face
= wx
.StaticText(self
, -1, "")
54 self
.face
.SetFont(font
)
55 fgs
.Add(self
.face
, 0, wx
.ADJUST_MINSIZE
)
57 fgs
.Add((15,15)); fgs
.Add((15,15)) # an empty row
59 fgs
.Add(wx
.StaticText(self
, -1, "wx.NativeFontInfo:"))
60 self
.nfi
= wx
.StaticText(self
, -1, "")
61 self
.nfi
.SetFont(font
)
62 fgs
.Add(self
.nfi
, 0, wx
.ADJUST_MINSIZE
)
64 # give it some border space
65 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
66 sizer
.Add(fgs
, 0, wx
.GROW|wx
.ADJUST_MINSIZE|wx
.ALL
, 25)
73 self
.sampleText
.SetFont(self
.curFont
)
74 self
.sampleText
.SetForegroundColour(self
.curClr
)
75 self
.ps
.SetLabel(str(self
.curFont
.GetPointSize()))
76 self
.family
.SetLabel(self
.curFont
.GetFamilyString())
77 self
.style
.SetLabel(self
.curFont
.GetStyleString())
78 self
.weight
.SetLabel(self
.curFont
.GetWeightString())
79 self
.face
.SetLabel(self
.curFont
.GetFaceName())
80 self
.nfi
.SetLabel(self
.curFont
.GetNativeFontInfo().ToString())
84 def OnSelectFont(self
, evt
):
86 data
.EnableEffects(True)
87 data
.SetColour(self
.curClr
) # set colour
88 data
.SetInitialFont(self
.curFont
)
90 dlg
= wx
.FontDialog(self
, data
)
92 if dlg
.ShowModal() == wx
.ID_OK
:
93 data
= dlg
.GetFontData()
94 font
= data
.GetChosenFont()
95 colour
= data
.GetColour()
97 self
.log
.WriteText('You selected: "%s", %d points, color %s\n' %
98 (font
.GetFaceName(), font
.GetPointSize(),
105 # Don't destroy the dialog until you get everything you need from the
110 #---------------------------------------------------------------------------
112 def runTest(frame
, nb
, log
):
113 win
= TestPanel(nb
, log
)
116 #---------------------------------------------------------------------------
120 This class allows you to use the system font selection dialog
121 from within your program. Generally speaking, this allows you
122 to select a font by its name, font size, and weight, and
123 on some systems such things as strikethrough and underline.
125 As with other dialogs used in wxPython, it is important to
126 use the class' methods to extract the information you need
127 about the font <b>before</b> you destroy the dialog. Failure
128 to observe this almost always leads to a program failure of
129 some sort, often ugly.
131 This demo serves two purposes; it shows how to use the dialog
132 to GET font information from the user, but also shows how
133 to APPLY that information once you get it.
137 if __name__
== '__main__':
140 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])