-
Notifications
You must be signed in to change notification settings - Fork 0
/
material-design-entry.py
68 lines (57 loc) · 2.33 KB
/
material-design-entry.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
# coding: utf-8
from Tkinter import *
class MaterialEntry(Frame):
def __init__(self, *args, **kws):
self.placeholder = kws.pop('placeholder', '')
self.placeholder_color = kws.get('phcolor', '#999')
self.fg_color = kws.get('bg', '#000')
Frame.__init__(self, *args, **kws)
self.entry = Entry(self)
self.entry.configure(
insertwidth=kws.get('insertwidth', 1),
border=kws.get('border', 0),
highlightthickness=kws.get('highlightthickness', 5),
)
self.entry.update_idletasks()
self.canvas = Canvas(self, bd=0, highlightthickness=0, height=2, bg='#ddd', width=self.entry.winfo_width())
_parent_color = self.master['bg']
self.entry.configure(
bg=_parent_color,
highlightcolor=_parent_color
)
self.update_placeholder()
self.entry.bind('<Any-KeyPress>', self._kb_handler, '+')
self.entry.bind('<FocusOut>', lambda e : self.update_placeholder(), "+")
self.entry.bind('<FocusIn>', lambda e : self.update_placeholder(), "+")
self.entry.bind('<1>', lambda e : self.update_placeholder(), "+")
self.entry.bind('<FocusIn>', self._focus_in, '+')
self.entry.bind('<FocusOut>', self._focus_out, '+')
self.entry.grid(pady=0, padx=5, row=0, column=0)
self.canvas.grid(row=1, column=0, sticky=W+E)
def _focus_in(self, event):
self.canvas['bg'] = '#00aacc'
def _focus_out(self, event):
self.canvas['bg'] = '#ddd'
def update_placeholder(self):
print "enter"
if self.entry.get() == '':
self.entry.configure(foreground=self.placeholder_color)
self.entry.insert(0, self.placeholder)
if self.entry.get() == self.placeholder and self.entry['fg'] == self.placeholder_color:
self.entry.icursor(0)
def _kb_handler(self, event):
# if event.keysym is visible key
if self.entry.get() == self.placeholder and self.entry['fg'] == self.placeholder_color:
self.entry['fg'] = self.fg_color
self.entry.delete(0,END)
top = Tk()
Label(top, text="Login", bg=top['bg'], font=('TkDefaultFont',10,'bold')).grid(row=0,column=0,pady=5, padx=5)
e = MaterialEntry(placeholder="Username")
e.grid(row=1, column=0,padx=5, pady=5)
e.entry.focus_force()
f = MaterialEntry(placeholder="Password")
f.grid(row=2, column=0,padx=5, pady=5)
f.focus_force()
Button(top, relief=FLAT, bg='#ddd', text="Ok", width=20,bd=0,highlightthickness=5).grid(row=3, column=0, pady=5, padx=5)
top.bind('<Escape>', lambda e : top.quit(), "+")
top.mainloop()