how to create virtual keyboard using python and tkinter - part - 1

hello friends,

                         today, i am going to show you how to create virtual keyboard using python, tkinter and pyautogui module or you can say this is our first part of how to create virtual keyboard using python and pyautogui tutorial series .


because it can be hard for beginners to understand.

so, i am dividing my tutorial in 2 part.



In 1st part,

 we will work on GUI (Graphic User Interface).







And In 2nd Part,

We will Work on virtual keyboard events.


so, let's start our first part

first,

we need to write master window codes.

Code 1.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# import modules
try:
    import Tkinter
except:
    import tkinter as Tkinter

import pyautogui

# Creating Main Window
def main():
 root = Tkinter.Tk(className=" Python Virtual KeyBoard")

 root.mainloop()
 return

# Function Trigger
if __name__=='__main__':
 main()

next,

we need to create a list of keys, functions, layout in one.

hmm, i know this looks difficult but don't worry i already solved it.

let me share my code with you.

here, in my keyboard key table format,

you can easily add more keyboard button very easily,

you can make changes very easily.

etc.. etc  now, i am providing

 my codes.

here

Code 2.



 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
keys =[ 
[
# =========================================
# ===== Keyboard Configurations ===========
# =========================================

 [
  # Layout Name
  ("Function_Keys"),

  # Layout Frame Pack arguments
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   # list of Keys in one row
   ('esc'," ", 'F1', 'F2','F3','F4',"",'F5','F6','F7','F8',"",'F9','F10','F11','F12')
  ]
 ],

 [
  ("Character_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   ('~\n`','!\n1','@\n2','#\n3','$\n4','%\n5','^\n6','&\n7','*\n8','(\n9',')\n0','_\n-','+\n=','|\n\\','backspace'),
   ('tab','q','w','e','r','t','y','u','i','o','p','{\n[','}\n]','   '),
   ('capslock','a','s','d','f','g','h','j','k','l',':\n;',"\"\n'","enter"),
   ("shift",'z','x','c','v','b','n','m','<\n,','>\n.','?\n/',"shift"),
   ("ctrl", "[+]",'alt','\t\tspace\t\t','alt','[+]','[=]','ctrl')
  ]
 ]
],
[
 [
  ("System_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   (
    "print\nscreen\nsys",
    "scroll\nlock",
    "pause\nbreak"
   )
  ]
 ],
 [
  ("Editing_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   (
    "insert",
    "home",
    "page\nup"
    ),
   ( "delete",
    "end",
    "page\ndown"
    ),
  ]
 ],

 [
  ("Navigation_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   (
    "up",
    ),
   ( "right",
    "down",
    "left"
    ),
  ]
 ],

],
[

 [
  ("Numeric_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   ("num\nlock","/","*","-"),
   ("7","8","9","+"),
   ("4","5","6"," "),
   ("0","1","2","3"),
   ("0",".","enter")
  ]
 ],

]

]


Now,

as you can see,

i tried to create this table very easy to under stand and

very easy to customize. etc etc.

now, let's move ahead and try to create a class that can easily extract data from our keyboard key

 table and provide us a well looking Keyboard GUI.

here,

i am providing comments in my code for defining what my script is doing

Code 3.



 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
##  Frame Class
class Keyboard(Tkinter.Frame):
 def __init__(self, *args, **kwargs):
  Tkinter.Frame.__init__(self, *args, **kwargs)

  # Function For Creating Buttons
  self.create_frames_and_buttons()

 # Function For Extracting Data From KeyBoard Table
 # and then provide us a well looking
 # keyboard gui
 def create_frames_and_buttons(self):
  # take section one by one
  for key_section in keys:
   # create Sperate Frame For Every Section
   store_section = Tkinter.Frame(self)
   store_section.pack(side='left',expand='yes',fill='both',padx=10,pady=10,ipadx=10,ipady=10)
   
   for layer_name, layer_properties, layer_keys in key_section:
    store_layer = Tkinter.LabelFrame(store_section)#, text=layer_name)
    #store_layer.pack(side='top',expand='yes',fill='both')
    store_layer.pack(layer_properties)
    for key_bunch in layer_keys:
     store_key_frame = Tkinter.Frame(store_layer)
     store_key_frame.pack(side='top',expand='yes',fill='both')
     for k in key_bunch:
      k=k.capitalize()
      if len(k)<=3:
       store_button = Tkinter.Button(store_key_frame, text=k, width=2, height=2)
      else:
       store_button = Tkinter.Button(store_key_frame, text=k.center(5,' '), height=2)
      if " " in k:
       store_button['state']='disable'
      #flat, groove, raised, ridge, solid, or sunken
      store_button['relief']="sunken"
      store_button['bg']="powderblue"
      store_button['command']=lambda q=k: self.button_command(q)
      store_button.pack(side='left',fill='both',expand='yes')
  return

  # Function For Detecting Pressed Keyword.
 def button_command(self, event):
  print event
  return

Now,

Our Keyboard Gui class Is Ready.

so, let's assemble all codes in one script.

here, my code

Code 4.



  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
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python
# ---------------- READ ME ---------------------------------------------
# This Script is Created Only For Practise And Educational Purpose Only
# This Script Is Created For http://bitforestinfo.blogspot.com
# This Script is Written By
#
#
##################################################
######## 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
######################################################
'''

# import modules
try:
    import Tkinter
except:
    import tkinter as Tkinter

import pyautogui

keys =[ 
[
# =========================================
# ===== Keyboard Configurations ===========
# =========================================

 [
  # Layout Name
  ("Function_Keys"),

  # Layout Frame Pack arguments
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   # list of Keys
   ('esc'," ", 'F1', 'F2','F3','F4',"",'F5','F6','F7','F8',"",'F9','F10','F11','F12')
  ]
 ],

 [
  ("Character_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   ('~\n`','!\n1','@\n2','#\n3','$\n4','%\n5','^\n6','&\n7','*\n8','(\n9',')\n0','_\n-','+\n=','|\n\\','backspace'),
   ('tab','q','w','e','r','t','y','u','i','o','p','{\n[','}\n]','   '),
   ('capslock','a','s','d','f','g','h','j','k','l',':\n;',"\"\n'","enter"),
   ("shift",'z','x','c','v','b','n','m','<\n,','>\n.','?\n/',"shift"),
   ("ctrl", "[+]",'alt','\t\tspace\t\t','alt','[+]','[=]','ctrl')
  ]
 ]
],
[
 [
  ("System_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   (
    "print\nscreen\nsys",
    "scroll\nlock",
    "pause\nbreak"
   )
  ]
 ],
 [
  ("Editing_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   (
    "insert",
    "home",
    "page\nup"
    ),
   ( "delete",
    "end",
    "page\ndown"
    ),
  ]
 ],

 [
  ("Navigation_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   (
    "up",
    ),
   ( "right",
    "down",
    "left"
    ),
  ]
 ],

],
[

 [
  ("Numeric_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   ("num\nlock","/","*","-"),
   ("7","8","9","+"),
   ("4","5","6"," "),
   ("0","1","2","3"),
   ("0",".","enter")
  ]
 ],

]

]

##  Frame Class
class Keyboard(Tkinter.Frame):
 def __init__(self, *args, **kwargs):
  Tkinter.Frame.__init__(self, *args, **kwargs)

  # Function For Creating Buttons
  self.create_frames_and_buttons()

 # Function For Extracting Data From KeyBoard Table
 # and then provide us a well looking
 # keyboard gui
 def create_frames_and_buttons(self):
  # take section one by one
  for key_section in keys:
   # create Sperate Frame For Every Section
   store_section = Tkinter.Frame(self)
   store_section.pack(side='left',expand='yes',fill='both',padx=10,pady=10,ipadx=10,ipady=10)
   
   for layer_name, layer_properties, layer_keys in key_section:
    store_layer = Tkinter.LabelFrame(store_section)#, text=layer_name)
    #store_layer.pack(side='top',expand='yes',fill='both')
    store_layer.pack(layer_properties)
    for key_bunch in layer_keys:
     store_key_frame = Tkinter.Frame(store_layer)
     store_key_frame.pack(side='top',expand='yes',fill='both')
     for k in key_bunch:
      k=k.capitalize()
      if len(k)<=3:
       store_button = Tkinter.Button(store_key_frame, text=k, width=2, height=2)
      else:
       store_button = Tkinter.Button(store_key_frame, text=k.center(5,' '), height=2)
      if " " in k:
       store_button['state']='disable'
      #flat, groove, raised, ridge, solid, or sunken
      store_button['relief']="sunken"
      store_button['bg']="powderblue"
      store_button['command']=lambda q=k: self.button_command(q)
      store_button.pack(side='left',fill='both',expand='yes')
  return

  # Function For Detecting Pressed Keyword.
 def button_command(self, event):
  print event
  return

# Creating Main Window
def main():
 root = Tkinter.Tk(className=" Python Virtual KeyBoard")
 Keyboard(root).pack()
 root.mainloop()
 return

# Function Trigger
if __name__=='__main__':
 main()


after executing this codes.

i got a virtual keyboard gui.

as screenshot given below.




friends, in my next part of tutorial, i will show you how to add virtual keyboard events in this GUI.

so, stay tuned with us.

and if you like my post then,  don't forget to subscribe our blog and youtube channel.


have a nice day.

written by:
                   S.S.B
                  surajsinghbisht054@gmail.com

For More Update, Visit Our Regularly. 

And Subscribe Our Blog, 

Follow Us and share it.
For Any Type of Suggestion Or Help
Contact me:
S.S.B
surajsinghbisht054@gmail.com

Share this

Related Posts

Previous
Next Post »