Add User

self.root = tk.Toplevel()
self.root.title("ADD USER")
self.root.configure(bg="#585858")
self.root.resizable(width=0, height=0)
win_width = 1280
win_height = int(1280 * 56.25 / 100)
self.root.geometry(str(win_width) + "x" + str(win_height))
Util.center(self.root)
# Get screen size
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
Util.set_font_size(screen_width, screen_height, win_width, win_height)
# White window frame credential
self.width = win_width * 48 / 100
self.height = win_height * 94 / 100
After creation of window, we will add a canvas background.
bg_canvas = RoundBackgroundFrame(self.root, self.width, self.height, padding, corner_radius, self.color, "#585858")
bg_canvas.place(width=self.width, height=self.height,x=win_width / 2 - self.width / 2, y=win_height / 2 - self.height / 2)
class RoundBackgroundFrame(tk.Canvas):
def __init__(self, parent, width, height, padding, cornerradius, color, bg):
tk.Canvas.__init__(self, parent, borderwidth=0, relief="flat", highlightthickness=0, bg=bg)
self.width = width
self.height = height
self.padding = padding
self.cornerradius = cornerradius
self.color = color
original = Image.open("images/corner.png")
resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
self.image_r_t = ImageTk.PhotoImage(resized)
original = original.rotate(90, expand=0)
resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
self.image_l_t = ImageTk.PhotoImage(resized)
original = original.rotate(90, expand=0)
resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
self.image_l_b = ImageTk.PhotoImage(resized)
original = original.rotate(90, expand=0)
resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
self.image_r_b = ImageTk.PhotoImage(resized)
self.shape(width, height, padding, cornerradius, color)
(x0, y0, x1, y1) = self.bbox("all")
width = (x1 - x0)
height = (y1 - y0)
self.configure(width=width, height=height)
def shape(self, width, height, padding, cornerradius, color):
self.create_polygon((padding, height - cornerradius - padding, padding, cornerradius + padding,
padding + cornerradius, padding, width - padding - cornerradius, padding,
width - padding, cornerradius + padding, width - padding,
height - cornerradius - padding, width - padding - cornerradius, height - padding,
padding + cornerradius, height - padding), fill=color, outline=color)
self.create_image(width - self.image_r_t.width(), 0, image=self.image_r_t, anchor=NW)
self.create_image(0, 0, image=self.image_l_t, anchor=NW)
self.create_image(0, height - self.image_l_b.width(), image=self.image_l_b, anchor=NW)
self.create_image(width - self.image_r_b.width(), height - self.image_r_b.width(), image=self.image_r_b, anchor=NW)
self.width = self.width * 98 / 100
self.height = self.height * 96 / 100
self.base_frame = Frame(self.root, width=self.width, height=self.height, bg=self.color)
bg_canvas.create_window(self.width / 100, self.height * 2 / 100, anchor=NW, window=self.base_frame)
# Add Heading Name
add_heading_label(self.base_frame, self.color, AppConstant.FONT_SIZE, self.height, self.width)
def add_heading_label(base_frame, color, font_size, height, width):
label_heading = Label(base_frame, text="Add User", anchor=CENTER, bg=color,font=("Lucida Grande", font_size + 6))
label_heading.place(width=width * 90 / 100, height=height * 7 / 100,x=width * 5 / 100, y=height * 8 / 100)
# Add Regisstration UI
self.user_name = CustomEntrySimple(self.base_frame, 500, 60, ("Lucida Grande", AppConstant.FONT_SIZE - 6),
self.color, "User Name")
self.user_name.place(x=50, y=150)
self.user_email_id = CustomEntrySimple(self.base_frame, 500, 60,("Lucida Grande", AppConstant.FONT_SIZE - 6),
self.color, "Email Id")
self.user_email_id.place(x=50, y=230)
self.user_contact_number = CustomEntrySimple(self.base_frame, 500, 60,("Lucida Grande", AppConstant.FONT_SIZE - 6),
self.color, "Contact Number")
self.user_contact_number.place(x=50, y=310)
self.user_password = CustomEntrySimple(self.base_frame, 500, 60,("Lucida Grande", AppConstant.FONT_SIZE - 6),
self.color, "Enter Password")
self.user_password.place(x=50, y=390)
self.add_button = RoundedButton(self.base_frame, 250, 130 / 2.56, self.color, "images/button33.png","ADD",font=("Lucida Grande", AppConstant.FONT_SIZE - 2),
command=lambda: self.add_user_click(*args))
self.add_button.place(x=175, y=self.height * 0.80)
Here is our class CustomEntrySimple:
class CustomEntrySimple(tk.Canvas):
def __init__(self, parent, width, height, font, color, text):
tk.Canvas.__init__(self, parent, borderwidth=0, relief="flat", highlightthickness=0, bg=color)
self.width = width
self.height = height
self.color = color
self.create_text(6, 5, anchor=W, font=font, text=text, fill="#000000")
self.create_line(5, height - 8, width - 5, height - 8, fill="#808080")
frame = Frame(parent,width=width * 95 / 100, height=height * 50 / 100, bg=color)
self.entry = Entry(frame, bg=color, bd=0, highlightthickness=0, font=font)
self.entry.place(relwidth=1, relheight=1, x=0, y=0)
self.create_window(6, height / 2 - (height * 50 / 100) / 2, anchor=NW, window=frame)
self.update()
(x0, y0, x1, y1) = self.bbox("all")
width = (x1 - x0)
height = (y1 - y0)
self.configure(width=width, height=height)
Now UI complete, here is logic of add user in database when click "Add" Button.
def add_user_click(self, *args):
var = ""
user_name = ""
user_email_id = ""
user_contact_number = ""
user_password = ""
is_name_entered = False
is_email_id_entered = False
is_contact_number_entered = False
is_password_entered = False
# name
if self.user_name.entry.get() == "" or self.user_name.entry.get() == " ":
var += "Book Count Should not be empty, "
else:
user_name = self.user_name.entry.get()
is_name_entered = True
# email
if self.user_email_id.entry.get() == "" or self.user_email_id.entry.get() == " ":
var += "Book Name Should not be empty, "
else:
user_email_id = self.user_email_id.entry.get()
is_email_id_entered = True
# contact number
if self.user_contact_number.entry.get() == "" or self.user_contact_number.entry.get() == " ":
var += "Author Name Should not be empty, "
else:
user_contact_number = self.user_contact_number.entry.get()
is_contact_number_entered = True
# password
if self.user_password.entry.get() == "" or self.user_password.entry.get() == " ":
var += "Book Price Should not be empty, "
else:
user_password = self.user_password.entry.get()
is_password_entered = True
if is_name_entered and is_email_id_entered and is_contact_number_entered and is_password_entered:
conn = Util.connect_db()
cursor = conn.cursor()
cursor.execute(
'INSERT INTO user_data(user_name, user_contact_no, user_email_id, user_password) VALUES(?,?,?,?)',
(user_name, user_contact_number, user_email_id, user_password))
conn.commit()
messagebox.showinfo("Success", "User has been added successfully")
self.user_name.entry.delete(0, 'end')
self.user_email_id.entry.delete(0, 'end')
self.user_contact_number.entry.delete(0, 'end')
self.user_password.entry.delete(0, 'end')
self.user_name.entry.focus()
# self.on_closing()
self.root.destroy()
args[0].deiconify()
else:
messagebox.showerror("showerror", var)
Here is logic for get user id.
def get_user_id(s_id):
user_id = "USR"
count = 0
if s_id == 0:
count = 1
while s_id > 0:
count = count + 1
s_id = s_id // 10
count = 3 - count
while count > 0:
user_id = user_id + "0"
count = count - 1
return user_id
At last we will manage closing of window
def on_closing():
root.destroy()
args[0].deiconify()
root.protocol("WM_DELETE_WINDOW", on_closing)
Programmer Mirta is for learning and training. Projects might be simple to improve learning. Projects are constantly reviewed to avoid errors, but we cannot assure full correctness of all content. While using Programmer Mitra, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2021 by Programmer Mitra. All Rights Reserved.