|
| 1 | +from tkinter import * |
| 2 | +from tkinter import messagebox |
| 3 | +import tkinter.messagebox as mbox |
| 4 | +import tkinter as tk |
| 5 | + |
| 6 | + |
| 7 | +root = Tk() |
| 8 | +root.title("Virtual Keyboard") |
| 9 | +root.geometry('1000x700') |
| 10 | + |
| 11 | +class Keypad(tk.Frame): |
| 12 | + |
| 13 | + cells = [ |
| 14 | + ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'], |
| 15 | + ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm','n', 'o', 'p', 'q','r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], |
| 16 | + ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M','N', 'O', 'P', 'Q','R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'], |
| 17 | + ['!', '@', '#', '$', '%', '&', '*', '/', '\'', '.', ',', ';', ':', '?', '<', '>','😀','😋','😂','🌞','🌴','🍕','🏳', '♻', '✔', '👍'], |
| 18 | + ] |
| 19 | + |
| 20 | + def __init__(self, *args, **kwargs): |
| 21 | + super().__init__(*args, **kwargs) |
| 22 | + |
| 23 | + self.target = None |
| 24 | + self.memory = '' |
| 25 | + |
| 26 | + for y, row in enumerate(self.cells): |
| 27 | + for x, item in enumerate(row): |
| 28 | + b = tk.Button(self, text=item, command=lambda text=item:self.append(text),font=("Arial", 14), bg = "light green", fg = "blue", borderwidth=3, relief="raised") |
| 29 | + b.grid(row=y, column=x, sticky='news') |
| 30 | + |
| 31 | + x = tk.Button(self, text='Space', command=self.space,font=("Arial", 14), bg = "light green", fg = "blue", borderwidth=3, relief="raised") |
| 32 | + x.grid(row=0, column=10, columnspan='4', sticky='news') |
| 33 | + |
| 34 | + x = tk.Button(self, text='tab', command=self.tab,font=("Arial", 14), bg = "light green", fg = "blue", borderwidth=3, relief="raised") |
| 35 | + x.grid(row=0, column=14, columnspan='3', sticky='news') |
| 36 | + |
| 37 | + x = tk.Button(self, text='Backspace', command=self.backspace,font=("Arial", 14), bg = "light green", fg = "blue", borderwidth=3, relief="raised") |
| 38 | + x.grid(row=0, column=17,columnspan='3', sticky='news') |
| 39 | + |
| 40 | + x = tk.Button(self, text='Clear', command=self.clear,font=("Arial", 14), bg = "light green", fg = "blue", borderwidth=3, relief="raised") |
| 41 | + x.grid(row=0, column=20, columnspan='3', sticky='news') |
| 42 | + |
| 43 | + x = tk.Button(self, text='Hide', command=self.hide,font=("Arial", 14), bg = "light green", fg = "blue", borderwidth=3, relief="raised") |
| 44 | + x.grid(row=0, column=23, columnspan='3', sticky='news') |
| 45 | + |
| 46 | + |
| 47 | + def get(self): |
| 48 | + if self.target: |
| 49 | + return self.target.get("1.0", "end-1c") |
| 50 | + |
| 51 | + def append(self, text): |
| 52 | + if self.target: |
| 53 | + self.target.insert('end', text) |
| 54 | + |
| 55 | + def clear(self): |
| 56 | + if self.target: |
| 57 | + self.target.delete('1.0', 'end') |
| 58 | + |
| 59 | + def backspace(self): |
| 60 | + if self.target: |
| 61 | + text = self.get() |
| 62 | + text = text[:-1] |
| 63 | + self.clear() |
| 64 | + self.append(text) |
| 65 | + |
| 66 | + def space(self): |
| 67 | + if self.target: |
| 68 | + text = self.get() |
| 69 | + text = text + " " |
| 70 | + self.clear() |
| 71 | + self.append(text) |
| 72 | + |
| 73 | + def tab(self): # 5 spaces |
| 74 | + if self.target: |
| 75 | + text = self.get() |
| 76 | + text = text + " " |
| 77 | + self.clear() |
| 78 | + self.append(text) |
| 79 | + |
| 80 | + def copy(self): |
| 81 | + #TODO: copy to clipboad |
| 82 | + if self.target: |
| 83 | + self.memory = self.get() |
| 84 | + self.label['text'] = 'memory: ' + self.memory |
| 85 | + print(self.memory) |
| 86 | + |
| 87 | + def paste(self): |
| 88 | + #TODO: copy from clipboad |
| 89 | + if self.target: |
| 90 | + self.append(self.memory) |
| 91 | + |
| 92 | + def show(self, entry): |
| 93 | + self.target = entry |
| 94 | + |
| 95 | + self.place(relx=0.5, rely=0.5, anchor='c') |
| 96 | + |
| 97 | + def hide(self): |
| 98 | + self.target = None |
| 99 | + |
| 100 | + self.place_forget() |
| 101 | + |
| 102 | +#------------------------------------------------------- |
| 103 | + |
| 104 | +def print_output(): |
| 105 | + mbox.showinfo("Text Entered", "Text Entered :\n\n" + text_enter.get('1.0',END)) |
| 106 | + |
| 107 | +# firstclick1 = True |
| 108 | +# def on_text_enter_click(event): |
| 109 | +# """function that gets called whenever entry1 is clicked""" |
| 110 | +# global firstclick1 |
| 111 | +# if firstclick1: # if this is the first time they clicked it |
| 112 | +# firstclick1 = False |
| 113 | +# text_enter.delete('1.0', "end") # delete all the text in the entry |
| 114 | + |
| 115 | +def des_f1(): |
| 116 | + f1.destroy() |
| 117 | + |
| 118 | + |
| 119 | +f1 = Frame(root, height=700, width=1000) |
| 120 | +f1.propagate(0) |
| 121 | +f1.pack(side='top') |
| 122 | + |
| 123 | +# start1 = Label(f1, text='VIRTUAL', font=("Arial", 100), fg="magenta") |
| 124 | +# start1.place(x=250, y=100) |
| 125 | +# |
| 126 | +# start2 = Label(f1, text='KEYBOARD', font=("Arial", 100), fg="magenta") |
| 127 | +# start2.place(x=150, y=300) |
| 128 | + |
| 129 | +c = Canvas(f1, width=1000, height=700) |
| 130 | +c.pack() |
| 131 | +p1 = PhotoImage(file='Images/keyboard.gif') |
| 132 | +c.create_image(200, 100, image=p1, anchor=NW) |
| 133 | + |
| 134 | +start1 = Label(f1, text='VIRTUAL KEYBOARD', font=("Arial", 50), fg="magenta", underline = 0) |
| 135 | +start1.place(x=150, y=10) |
| 136 | + |
| 137 | +startb = Button(f1, text="START",command=des_f1,font=("Arial", 30), bg = "light green", fg = "blue", borderwidth=3, relief="raised") |
| 138 | +startb.place(x = 180 , y =550 ) |
| 139 | + |
| 140 | + |
| 141 | +f2 = Frame(root, height=700, width=1000) |
| 142 | +f2.propagate(0) |
| 143 | +f2.pack(side='top') |
| 144 | + |
| 145 | +keypad = Keypad(root) |
| 146 | + |
| 147 | +start2 = Label(f2, text='ENTER TEXT HERE...', font=("Arial", 40), fg="magenta") |
| 148 | +start2.place(x=200, y=460) |
| 149 | + |
| 150 | +text_enter = tk.Text(f2, height=10, width=80, font=("Arial", 15), bg="light yellow", fg="brown",borderwidth=3, relief="solid") |
| 151 | +text_enter.insert(END, 'Enter your text here from virtual keyboard...') # default line in text area, can be cleared when touched |
| 152 | +# text_enter.bind('<FocusIn>', on_text_enter_click) |
| 153 | +text_enter.place(x=50, y=10) |
| 154 | + |
| 155 | +keyboardb = Button(f2, text="KEYBOARD",command=lambda:keypad.show(text_enter),font=("Arial", 30), bg = "light green", fg = "blue", borderwidth=3, relief="raised") |
| 156 | +keyboardb.place(x =100 , y =550 ) |
| 157 | + |
| 158 | +printb = Button(f2, text="PRINT",command=print_output,font=("Arial", 30), bg = "light green", fg = "blue", borderwidth=3, relief="raised") |
| 159 | +printb.place(x =450 , y =550 ) |
| 160 | + |
| 161 | +def exit_win(): |
| 162 | + if messagebox.askokcancel("Exit", "Do you want to exit?"): |
| 163 | + root.destroy() |
| 164 | + |
| 165 | + |
| 166 | +exitb = Button(root, text="EXIT",command=exit_win,font=("Arial", 30), bg = "red", fg = "blue", borderwidth=3, relief="raised") |
| 167 | +exitb.place(x =700 , y =550 ) |
| 168 | + |
| 169 | + |
| 170 | +root.protocol("WM_DELETE_WINDOW", exit_win) |
| 171 | +root.mainloop() |
0 commit comments