| 1 | |
| 2 | import wx |
| 3 | |
| 4 | #--------------------------------------------------------------------------- |
| 5 | |
| 6 | class TestPanel(wx.Panel): |
| 7 | def __init__(self, parent, log): |
| 8 | wx.Panel.__init__(self, parent, -1) |
| 9 | self.log = log |
| 10 | |
| 11 | btn = wx.Button(self, -1, "Select Font") |
| 12 | self.Bind(wx.EVT_BUTTON, self.OnSelectFont, btn) |
| 13 | |
| 14 | self.sampleText = wx.TextCtrl(self, -1, "Sample Text") |
| 15 | |
| 16 | self.curFont = self.sampleText.GetFont() |
| 17 | self.curClr = wx.BLACK |
| 18 | |
| 19 | fgs = wx.FlexGridSizer(cols=2, vgap=5, hgap=5) |
| 20 | fgs.AddGrowableCol(1) |
| 21 | fgs.AddGrowableRow(0) |
| 22 | |
| 23 | fgs.Add(btn) |
| 24 | fgs.Add(self.sampleText, 0, wx.ADJUST_MINSIZE|wx.GROW) |
| 25 | |
| 26 | fgs.Add((15,15)); fgs.Add((15,15)) # an empty row |
| 27 | |
| 28 | fgs.Add(wx.StaticText(self, -1, "PointSize:")) |
| 29 | self.ps = wx.StaticText(self, -1, "") |
| 30 | font = self.ps.GetFont() |
| 31 | font.SetWeight(wx.BOLD) |
| 32 | self.ps.SetFont(font) |
| 33 | fgs.Add(self.ps, 0, wx.ADJUST_MINSIZE) |
| 34 | |
| 35 | fgs.Add(wx.StaticText(self, -1, "Family:")) |
| 36 | self.family = wx.StaticText(self, -1, "") |
| 37 | self.family.SetFont(font) |
| 38 | fgs.Add(self.family, 0, wx.ADJUST_MINSIZE) |
| 39 | |
| 40 | fgs.Add(wx.StaticText(self, -1, "Style:")) |
| 41 | self.style = wx.StaticText(self, -1, "") |
| 42 | self.style.SetFont(font) |
| 43 | fgs.Add(self.style, 0, wx.ADJUST_MINSIZE) |
| 44 | |
| 45 | fgs.Add(wx.StaticText(self, -1, "Weight:")) |
| 46 | self.weight = wx.StaticText(self, -1, "") |
| 47 | self.weight.SetFont(font) |
| 48 | fgs.Add(self.weight, 0, wx.ADJUST_MINSIZE) |
| 49 | |
| 50 | fgs.Add(wx.StaticText(self, -1, "Face:")) |
| 51 | self.face = wx.StaticText(self, -1, "") |
| 52 | self.face.SetFont(font) |
| 53 | fgs.Add(self.face, 0, wx.ADJUST_MINSIZE) |
| 54 | |
| 55 | fgs.Add((15,15)); fgs.Add((15,15)) # an empty row |
| 56 | |
| 57 | fgs.Add(wx.StaticText(self, -1, "wx.NativeFontInfo:")) |
| 58 | self.nfi = wx.StaticText(self, -1, "") |
| 59 | self.nfi.SetFont(font) |
| 60 | fgs.Add(self.nfi, 0, wx.ADJUST_MINSIZE) |
| 61 | |
| 62 | # give it some border space |
| 63 | sizer = wx.BoxSizer(wx.VERTICAL) |
| 64 | sizer.Add(fgs, 0, wx.GROW|wx.ADJUST_MINSIZE|wx.ALL, 25) |
| 65 | |
| 66 | self.SetSizer(sizer) |
| 67 | self.UpdateUI() |
| 68 | |
| 69 | |
| 70 | def UpdateUI(self): |
| 71 | self.sampleText.SetFont(self.curFont) |
| 72 | self.ps.SetLabel(str(self.curFont.GetPointSize())) |
| 73 | self.family.SetLabel(self.curFont.GetFamilyString()) |
| 74 | self.style.SetLabel(self.curFont.GetStyleString()) |
| 75 | self.weight.SetLabel(self.curFont.GetWeightString()) |
| 76 | self.face.SetLabel(self.curFont.GetFaceName()) |
| 77 | self.nfi.SetLabel(self.curFont.GetNativeFontInfo().ToString()) |
| 78 | self.Layout() |
| 79 | |
| 80 | |
| 81 | def OnSelectFont(self, evt): |
| 82 | data = wx.FontData() |
| 83 | data.EnableEffects(True) |
| 84 | data.SetColour(self.curClr) # set colour |
| 85 | data.SetInitialFont(self.curFont) |
| 86 | |
| 87 | dlg = wx.FontDialog(self, data) |
| 88 | |
| 89 | if dlg.ShowModal() == wx.ID_OK: |
| 90 | data = dlg.GetFontData() |
| 91 | font = data.GetChosenFont() |
| 92 | colour = data.GetColour() |
| 93 | |
| 94 | self.log.WriteText('You selected: "%s", %d points, color %s\n' % |
| 95 | (font.GetFaceName(), font.GetPointSize(), |
| 96 | colour.Get())) |
| 97 | |
| 98 | self.curFont = font |
| 99 | self.curClr = colour |
| 100 | self.UpdateUI() |
| 101 | |
| 102 | # Don't destroy the dialog until you get everything you need from the |
| 103 | # dialog! |
| 104 | dlg.Destroy() |
| 105 | |
| 106 | |
| 107 | #--------------------------------------------------------------------------- |
| 108 | |
| 109 | def runTest(frame, nb, log): |
| 110 | win = TestPanel(nb, log) |
| 111 | return win |
| 112 | |
| 113 | #--------------------------------------------------------------------------- |
| 114 | |
| 115 | |
| 116 | overview = """\ |
| 117 | This class allows you to use the system font selection dialog |
| 118 | from within your program. Generally speaking, this allows you |
| 119 | to select a font by its name, font size, and weight, and |
| 120 | on some systems such things as strikethrough and underline. |
| 121 | |
| 122 | As with other dialogs used in wxPython, it is important to |
| 123 | use the class' methods to extract the information you need |
| 124 | about the font <b>before</b> you destroy the dialog. Failure |
| 125 | to observe this almost always leads to a program failure of |
| 126 | some sort, often ugly. |
| 127 | |
| 128 | This demo serves two purposes; it shows how to use the dialog |
| 129 | to GET font information from the user, but also shows how |
| 130 | to APPLY that information once you get it. |
| 131 | |
| 132 | """ |
| 133 | |
| 134 | if __name__ == '__main__': |
| 135 | import sys,os |
| 136 | import run |
| 137 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
| 138 | |