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