How to create Font Selecting Dialog Window For Tkinter Text widget - Python MagicStick Text Editor - Part 12

Namaste Friends,




Fonts are Normal Text Characters use for Representing Text Data. Font provide us different visual style for representing our text character in Text Editor. So, Today In This Post, I am Going To Show you how to create automatic font selecting dialog box that can configure Tkinter Text Widget Font Options Automatically.


Friends, This Is our 12th Tutorial Of Python MagicStick Project But,  if you are a new visitor, Then I will suggest you to read our tutorials number wise. because in these tutorial series i am showing how to use python tkinter module to create a real life application.


Links For Tutorials

MagicStick Text Editor Part 3
MagicStick Text Editor Part 4
MagicStick Text Editor Part 5
MagicStick Text Editor Part 6
MagicStick Text Editor Part 7
MagicStick Text Editor Part 8
MagicStick Text Editor Part 9
Or You Can Also Download Our Updated Example Codes Of Python MagicStick Text Editor Project For Understanding This Project More Clearly.

Check Here Our Github Repo


Now, let's focus to our main topic.

For this purpose we will use tkFont module. This Module Provide us ability of doing changes in Tkinter Text Widget Automatically. This Module works like a Automatic Interface between Tkinter Text Editor Font Options and The Usee. In Simple Words, This module is also very easy, compitable and stable for changing Tkinter Text Widget Font Options.



To make this script easy to understand we will create a separate empty script "FontChooser.py" in "magicsticklibs/" folder and write our codes  in it.

In This Script, First, We Will Create A Simple Class of Tkinter Toplevel Widget. And Then, we will add Our Different Functions and Other Tkinter Widget On it.


So, let's do some practical coding's.

1. FontChooser.py


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
##
##
## ################################################
## ###### Please Don't Remove Author Name #########
## ############# Thanks ###########################
## ################################################
##
##
__author__='''

######################################################
                By S.S.B Group                          
######################################################

    Suraj Singh
    Admin
    S.S.B Group
    surajsinghbisht054@gmail.com
    http://bitforestinfo.blogspot.com/

    Note: We Feel Proud To Be Indian
######################################################
'''
from Graphics import Tkinter
from Graphics import tkFont, ttk



class Font_wm(Tkinter.Toplevel):
    def __init__(self, pad):
        Tkinter.Toplevel.__init__(self)
        self.text = pad
        Font = tkFont.Font()
        self.text.config(font=Font)
        self.mainfont= Font
        self.title('Font Chooser')
        self.now_get_font_options()
        

    def now_get_font_options(self, event=None):
        #{'family': 'Arial', 'weight': 'normal', 'slant': 'roman', 'overstrike': 0, 'underline': 0, 'size': 10}   self.mainfont.actual()

        # Variable
        self.var=Tkinter.StringVar()# For Font Face
        self.var.set(self.mainfont.actual('family'))
        self.var1=Tkinter.IntVar()  # for Font Size
        self.var1.set(self.mainfont.actual('size'))
        self.var2=Tkinter.StringVar() # For Bold
        self.var2.set(self.mainfont.actual('weight'))
        self.var3=Tkinter.StringVar() # For Italic
        self.var3.set(self.mainfont.actual('slant'))
        self.var4=Tkinter.IntVar()# For Underline
        self.var4.set(self.mainfont.actual('underline'))
        self.var5=Tkinter.IntVar() # For Overstrike
        self.var5.set(self.mainfont.actual('overstrike'))


        # Font Sample
        self.font_1=tkFont.Font()
        for i in ['family', 'weight', 'slant', 'overstrike', 'underline', 'size']:
            self.font_1[i]=self.mainfont.actual(i)

        # Function
        def checkface(event):
            try:
                self.var.set(str(self.listbox.get(self.listbox.curselection())))
                self.font_1.config(family=self.var.get(), size=self.var1.get(), weight=self.var2.get(), slant=self.var3.get(), underline=self.var4.get(), overstrike=self.var5.get())
            except:
               pass
        def checksize(event):
            try:
                self.var1.set(int(self.size.get(self.size.curselection())))
                self.font_1.config(family=self.var.get(), size=self.var1.get(), weight=self.var2.get(), slant=self.var3.get(), underline=self.var4.get(), overstrike=self.var5.get())
            except:
                pass            
        def applied():
            self.result=(self.var.get(), self.var1.get(), self.var2.get(), self.var3.get(), self.var4.get(), self.var5.get())
            self.mainfont['family']=self.var.get()
            self.mainfont['size']=self.var1.get()
            self.mainfont['weight']=self.var2.get()
            self.mainfont['slant']=self.var3.get()
            self.mainfont['underline']=self.var4.get()
            self.mainfont['overstrike']=self.var5.get()
        def out():
            self.result=(self.var.get(), self.var1.get(), self.var2.get(), self.var3.get(), self.var4.get(), self.var5.get())
            self.mainfont['family']=self.var.get()
            self.mainfont['size']=self.var1.get()
            self.mainfont['weight']=self.var2.get()
            self.mainfont['slant']=self.var3.get()
            self.mainfont['underline']=self.var4.get()
            self.mainfont['overstrike']=self.var5.get()
            self.destroy()
        def end():
            self.result=None
            self.destroy()
            
        # Main window Frame
        self.mainwindow=ttk.Frame(self)
        self.mainwindow.pack(padx=10, pady=10)
        # Main LabelFrame
        self.mainframe=ttk.Frame(self.mainwindow)
        self.mainframe.pack(side='top',ipady=30, ipadx=30,expand='no', fill='both')
        self.mainframe0=ttk.Frame(self.mainwindow)
        self.mainframe0.pack(side='top', expand='yes', fill='x', padx=10, pady=10)
        self.mainframe1=ttk.Frame(self.mainwindow)
        self.mainframe1.pack(side='top',expand='no', fill='both')
        self.mainframe2=ttk.Frame(self.mainwindow)
        self.mainframe2.pack(side='top',expand='yes', fill='x', padx=10, pady=10)
        # Frame in [  main frame]
        self.frame=ttk.LabelFrame(self.mainframe, text='Select Font Face')
        self.frame.pack(side='left', padx=10, pady=10, ipadx=20, ipady=20, expand='yes', fill='both')
        self.frame1=ttk.LabelFrame(self.mainframe, text='Select Font size')
        self.frame1.pack(side='left', padx=10, pady=10, ipadx=20, ipady=20, expand='yes', fill='both')
        ttk.Entry(self.frame, textvariable=self.var).pack(side='top', padx=5, pady=5, expand='yes', fill='x')
        self.listbox=Tkinter.Listbox(self.frame, bg='gray70')
        self.listbox.pack(side='top', padx=5, pady=5, expand='yes', fill='both')
        for i in tkFont.families():
            self.listbox.insert(Tkinter.END, i)

        # Frame in [ 0. mainframe]
        self.bold=ttk.Checkbutton(self.mainframe0, text='Bold', onvalue='bold', offvalue='normal', variable=self.var2)
        self.bold.pack(side='left',expand='yes', fill='x')
        self.italic=ttk.Checkbutton(self.mainframe0, text='Italic', onvalue='italic', offvalue='roman',variable=self.var3)
        self.italic.pack(side='left', expand='yes', fill='x')
        self.underline=ttk.Checkbutton(self.mainframe0, text='Underline',onvalue=1, offvalue=0, variable=self.var4)
        self.underline.pack(side='left', expand='yes', fill='x')
        self.overstrike=ttk.Checkbutton(self.mainframe0, text='Overstrike',onvalue=1, offvalue=0, variable=self.var5)
        self.overstrike.pack(side='left', expand='yes', fill='x')
        
        # Frame in [ 1. main frame]
        ttk.Entry(self.frame1, textvariable=self.var1).pack(side='top', padx=5, pady=5, expand='yes', fill='x')
        self.size=Tkinter.Listbox(self.frame1, bg='gray70')
        self.size.pack(side='top', padx=5, pady=5, expand='yes', fill='both')
        for i in range(30):
            self.size.insert(Tkinter.END, i)

        Tkinter.Label(self.mainframe1, bg='white',text='''
ABCDEabcde12345
''', font=self.font_1).pack(expand='no', padx=10,pady=10)

        # Frame in [ 2. mainframe]
        ttk.Button(self.mainframe2, text='   OK   ', command=out).pack(side='left', expand='yes', fill='x', padx=5, pady=5)
        ttk.Button(self.mainframe2, text=' Cancel ', command=end).pack(side='left', expand='yes', fill='x', padx=5, pady=5)
        ttk.Button(self.mainframe2, text=' Apply  ', command=applied).pack(side='left', expand='yes', fill='x', padx=5, pady=5)
        
        self.listbox.bind('<<ListboxSelect>>', checkface)
        self.size.bind('<<ListboxSelect>>', checksize)

class FontChooser:
    def __init__(self,text):
        self.text=text
        self.text.bind("<Control-F1>", self.choose)
        self.binding_function_configuration()

    def binding_function_configuration(self):
        self.text.storeobj['FontChooser']=self.choose
        return

    def choose(self, event=None):
        Font_wm(self.text)
        return


if __name__=='__main__':       
    root=Tkinter.Tk()
    
    pad = Tkinter.Text(root)
    pad.pack()
    pad.storeobj={}
    FontChooser(pad)
    root.mainloop()


To Run These Script Codes alone, Just Change line 24,25 from

"from Graphics import Tkinter"
"from Graphics import tkFont, ttk"

To
"import Tkinter"     and
"import tkFont, ttk"

Or

To Run This Codes as MagicStick Script. Don't Do Any Changes in this codes.






 Now, let's join this script in MagicStick Text Editor Project ( Follow Below Steps only, when you have not Connected this script with MagicScript Main Stream).

To Connect This Script With MagicStick project, we need to do only two steps.


1. first, Place This Script In Our "MagicStick/magicsticklibs" Project Directory.


2. Add Function Of This Script in Our "ConfigSettings.py".





Let me explain you these steps in more details.



Step 1.
 Place "FontChooser.py" In Our "MagicStick/magicsticklibs" Project Directory.



Then Our MagicStick Project Directory Structure will look like this.



MagicStick_Editor_Part_11
 |
 |---> About.txt  
 |
 |---> magicsticklibs
 |  |
 |  |---> __init__.py
 |  |     
 |  |---> Main.py
 |  |            
 |  |---> TextPad.py
 |  |
 |  |---> Graphics.py
 |  |
 |  |---> ConfigSettings.py 
 |  |
 |  |---> ScrollBar.py
 |  |
 |  |---> LineNumber.py
 |  |
 |  |---> StationeryFunctions.py
 |  |
 |  |---> PopupMenu.py
 |  |
 |  |---> FindAndReplace.py
 |  |
 |  |---> FileHandler.py
 |  |
 |  |---> FontChooser.py
 |
 |---> main.py

Step 2.
          Add "FontChooser.py" Function in Our "ConfigSettings.py" because in this project, "ConfigSettings.py" is responsible for joining libs in main stream.


ConfigSettings.py Modified Codes


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
##
##
## ################################################
## ###### Please Don't Remove Author Name #########
## ############# Thanks ###########################
## ################################################
##
##
__author__='''

######################################################
                By S.S.B Group                          
######################################################

    Suraj Singh
    Admin
    S.S.B Group
    surajsinghbisht054@gmail.com
    http://bitforestinfo.blogspot.com/

    Note: We Feel Proud To Be Indian
######################################################
'''

from LineNumber import LineMain
from ScrollBar import Scrollbar
from StationeryFunctions import StationeryFunctions
from PopupMenu import Popup
from FIndAndReplace import FindReplaceFunctions
from FileHandler import FileHandler
from FontChooser import FontChooser

class Connect:
 def __init__(self, pad):
  self.pad = pad
  self.modules_connections()


 def modules_connections(self):
  LineMain(self.pad)
  Scrollbar(self.pad)
  StationeryFunctions(self.pad)
  Popup(self.pad)
  FindReplaceFunctions(self.pad)
  FileHandler(self.pad)
  FontChooser(self.pad)
  return


Boom! Our FontChooser.py script is now ready to use and also, this script is now connected with MagicStick Project Main stream. To run this script in main stream of project. just run "main.py" script or you can also run this script alone and parallel without touching project main stream just run "FontChooser.py".


This Tutorial Ends Here,


In My Next Tutorial, We will Continue This project.


Written By
        SSB





Share this

Related Posts

Previous
Next Post »