def draw_login_frame(self): self.head.destroy() self.body.destroy() self.head = tk.Frame(self, width=250, height=150) self.body = tk.Frame(self, width=250, height=100) self.head.grid(row=0, column=0) self.body.grid(row=1, column=0) self.head.grid_propagate(False) self.body.grid_propagate(False) self.header = tk.Label(self.head, width=100, height=140, image=avtar, text='\nLogin', compound=tk.TOP, fg='black', font=('Arial', 12, 'bold'), cursor='hand2') self.header.bind("<Button-1>", self.to_home_page) self.header.grid(row=0, column=0, pady=8, padx=70) if self.on_home: self.email_ = CustomEntry(self.body, text='Email', width=26, textvariable=self.email_id) self.email_.grid(row=0, column=0, padx=(25, 12), pady=20) self.send = tk.Button(self.body, image=send, relief=tk.FLAT, command=self.fetch_email) self.send.grid(row=0, column=1, padx=10, pady=20) self.not_registered = tk.Label( self.body, text='Not a registered user, SignUp here', font=('Arial', 8, 'underline'), fg='blue', cursor='hand2') self.not_registered.bind("<Button-1>", self.to_signup) self.not_registered.grid(row=1, column=0, columnspan=2) else: self.header['text'] = '\nHello ' + self.username.get() self.pass_ = CustomEntry(self.body, text='Password', width=26, textvariable=self.password) self.pass_.grid(row=0, column=0, padx=20, pady=20) self.send = tk.Button(self.body, image=send, relief=tk.FLAT, command=self.verify_login) self.send.grid(row=0, column=1, padx=10, pady=20)
def draw_item_frame_widgets(self): for row, item in enumerate(self.itemList): label = CustomLabel(self.items_frame, text=(' ' + item)) label.grid(row=row, column=0, pady=(4,0)) entry = CustomEntry(self.items_frame, textvariable=self.entryValues[row]) entry.grid(row=row, column=1, pady=(4,0))
def draw_calculator_frame_widgets(self): self.input_string = tk.StringVar() self.calc_entry = CustomEntry(self.calculator, width=23, textvariable=self.input_string) self.calc_entry.grid(row=0, column=0, columnspan=4, pady=(5, 3), padx=4) self.seven = self.create_button(self.calculator, '7', '#248aa2', "white", lambda: self.get('7'), 1, 0) self.eight = self.create_button(self.calculator, '8', '#248aa2', "white", lambda: self.get('8'), 1, 1) self.nine = self.create_button(self.calculator, '9', '#248aa2', "white", lambda: self.get('9'), 1, 2) self.plus = self.create_button(self.calculator, '+', 'white', "black", lambda: self.get('+'), 1, 3) self.four = self.create_button(self.calculator, '4', '#248aa2', "white", lambda: self.get('4'), 2, 0) self.five = self.create_button(self.calculator, '5', '#248aa2', "white", lambda: self.get('5'), 2, 1) self.six = self.create_button(self.calculator, '6', '#248aa2', "white", lambda: self.get('6'), 2, 2) self.minus = self.create_button(self.calculator, '-', 'white', "black", lambda: self.get('-'), 2, 3) self.one = self.create_button(self.calculator, '1', '#248aa2', "white", lambda: self.get('1'), 3, 0) self.two = self.create_button(self.calculator, '2', '#248aa2', "white", lambda: self.get('2'), 3, 1) self.three = self.create_button(self.calculator, '3', '#248aa2', "white", lambda: self.get('3'), 3, 2) self.mult = self.create_button(self.calculator, '*', 'white', "black", lambda: self.get('*'), 3, 3) self.clear = self.create_button(self.calculator, 'C', '#248aa2', "white", self.delete_calc_text, 4, 0) self.zero = self.create_button(self.calculator, '0', '#248aa2', "white", lambda: self.get('0'), 4, 1) self.equal = self.create_button(self.calculator, '=', '#248aa2', "white", self.evaluate_expression, 4, 2) self.div = self.create_button(self.calculator, '/', 'white', "black", lambda: self.get('/'), 4, 3)
def draw_header_widgets(self): self.header = tk.Label(self.title_frame, text='InstaLoader', font=('Copperplate Gothic Light', 25, 'bold'), bg='white') self.header.grid(row=0, column=0, pady=(10, 5), padx=15) self.username = tk.StringVar() self.entry = CustomEntry(self.search_frame, width=33, textvariable=self.username) self.entry.grid(row=0, column=0, pady=(8, 5), padx=(15, 2)) self.entry.bind('<Return>', self.search_user) self.search = tk.Button(self.search_frame, image=search_icon, bg='white', cursor='hand2', command=self.search_user, relief=tk.FLAT) self.search.grid(row=0, column=1, pady=(8, 5), padx=2) self.settings = tk.Button(self.search_frame, image=settings_icon, bg='white', cursor='hand2', command=self.ask_credentials, relief=tk.FLAT, height=45, width=30) self.settings.grid(row=0, column=2, pady=0, padx=0) self.left_label = tk.Label(self.insta_frame, width=1, height=8, bg='dodgerblue3') self.insta_label = tk.Label(self.insta_frame, width=90, height=97, image=insta_icon, bg='white') self.left_label.grid(row=0, column=0) self.insta_label.grid(row=0, column=1)
def draw_bill_frame_widgets(self): self.chargeList = [ 'Items Cost', 'Service Charge', 'GST Charges', 'Discount', 'Total' ] self.chargeValues = [tk.StringVar() for item in self.chargeList] for row, item in enumerate(self.chargeList): label = CustomLabel(self.bill_frame, text=item, width=12) label.grid(row=row, column=0, pady=(3, 0)) entry = CustomEntry(self.bill_frame, textvariable=self.chargeValues[row]) entry.grid(row=row, column=1, pady=(3, 0)) self.name_frame = tk.LabelFrame(self.bill_frame) self.name_frame.grid(row=5, column=0, columnspan=2, pady=(15, 0), padx=3) self.customer_name = tk.StringVar() self.name = CustomLabel(self.name_frame, text='Name', width=7) self.name_entry = CustomEntry(self.name_frame, width=15, textvariable=self.customer_name) self.name.grid(row=0, column=0, pady=2, padx=1) self.name_entry.grid(row=0, column=1, pady=2, padx=1)
class Application(tk.Frame): def __init__(self, master=None): super().__init__(master=master) self.master=master self.pack() self._attributes() self.contacts = sql_operations.fetch_all_result() self.draw_header_frames() self.draw_all_contact_frame() def _attributes(self): self.search_term = tk.StringVar() self.search_term.set('') self.edit_contacts = False self.on_home = True self.is_new_contact = False self.data = None def draw_header_frames(self): self.header = tk.Frame(self, width=300, height=50, bg='dodgerblue') self.header.grid(row=0, column=0) self.header.grid_propagate(False) self.header_label = tk.Label(self.header, text='Contacts', font=('Algerian', 24), bg='dodgerblue', fg='white') self.header_label.grid(row=0, column=0, ipadx=2, pady=5) def draw_all_contact_frame(self): # Frames self.contacts_frame = tk.Frame(self, width=300, height=400, bg='gray') self.search_bar = tk.Frame(self.contacts_frame, width=300, height=30) self.contact_box = tk.Frame(self.contacts_frame, width=300, height=320) self.bottom_menu = tk.Frame(self.contacts_frame, width=300, height=50, bg='dodgerblue3') self.contacts_frame.grid(row=1, column=0) self.search_bar.grid(row=0, column=0) self.contact_box.grid(row=1, column=0) self.bottom_menu.grid(row=2, column=0) self.contacts_frame.grid_propagate(False) self.search_bar.grid_propagate(False) self.contact_box.grid_propagate(False) self.bottom_menu.grid_propagate(False) # Widgets self.search_entry = CustomSearch(self.search_bar, width=32, textvariable=self.search_term) self.search_entry.grid(row=0, column=0, pady=5, padx=5) self.search_term.trace_add('write', self.enumerate_content) self.search_btn = tk.Button(self.search_bar, image=search_icon, relief=tk.FLAT) self.search_btn.grid(row=0, column=1, padx=60) self.contact_scroll = tk.Scrollbar(self.contact_box, orient=tk.VERTICAL) self.contact_scroll.grid(row=1, column=1, sticky='ns', pady=12) self.contact_list = tk.Listbox(self.contact_box, selectmode=tk.SINGLE, selectbackground='sky blue', fg='black', yscrollcommand=self.contact_scroll.set, font=('Times', 11)) self.contact_list.configure(height=16, width=39) self.contact_list.bind('<Double-1>', self.show_contact) self.contact_list.bind('<Return>', self.show_contact) self.contact_list.grid(row=1, column=0, padx=2, sticky='W', pady=12) self.contact_scroll.config(command=self.contact_list.yview) self.num_contacts = tk.Label(self.bottom_menu, text=f'{len(self.contacts)} contacts saved', fg='white', bg='dodgerblue3', font=('Times', 11), width=16, anchor='w') self.num_contacts.grid(row=0, column=0, padx=5, pady=8) self.add_new_btn = tk.Button(self.bottom_menu, text=' New', fg='white', image=new_contact_icon, relief=tk.FLAT, compound=tk.LEFT, font=('Times', 11), borderwidth=1, bg='dodgerblue3', command=self.create_new_contact) self.add_new_btn.grid(row=0, column=1, padx=80, pady=5) self.enumerate_content() def draw_display_contact_frame(self): self.search_term.set('') # frames self.new_contact_frame = tk.Frame(self, width=300, height=400, bg='gray') self.create_contact_frame = tk.Frame(self.new_contact_frame, width=300, height=350) self.bottom_menu = tk.Frame(self.new_contact_frame, width=300, height=50, bg='dodgerblue3') self.new_contact_frame.grid(row=1, column=0) self.create_contact_frame.grid(row=0, column=0) self.bottom_menu.grid(row=1, column=0) self.new_contact_frame.grid_propagate(False) self.create_contact_frame.grid_propagate(False) self.bottom_menu.grid_propagate(False) # widgets self.first = tk.LabelFrame(self.create_contact_frame, text='*First Name', bg='dodgerblue3', width=250, height=40, fg='white') self.last = tk.LabelFrame(self.create_contact_frame, text='Last Name', bg='dodgerblue3', width=250, height=40, fg='white') self.phone = tk.LabelFrame(self.create_contact_frame, text='*Phone', bg='dodgerblue3', width=250, height=40, fg='white') self.email = tk.LabelFrame(self.create_contact_frame, text='Email', bg='dodgerblue3', width=250, height=40, fg='white') self.first.grid(row=0, column=0, padx=25, pady=(30,0)) self.last.grid(row=1, column=0, padx=25, pady=(30,0)) self.phone.grid(row=2, column=0, padx=25, pady=(30,0)) self.email.grid(row=3, column=0, padx=25, pady=(30,0)) self.first.grid_propagate() self.last.grid_propagate() self.phone.grid_propagate() self.email.grid_propagate() if not self.edit_contacts: self.first_name = tk.Label(self.first, width=33, anchor='w') self.last_name = tk.Label(self.last, width=33, anchor='w') self.phone_number = tk.Label(self.phone, width=33, anchor='w') self.email_id = tk.Label(self.email, width=33, anchor='w') self.first_name.grid(row=0, column=0) self.last_name.grid(row=0, column=0) self.phone_number.grid(row=0, column=0) self.email_id.grid(row=0, column=0) self.back = tk.Button(self.bottom_menu, image=back_icon, bg='dodgerblue3', relief=tk.FLAT, command=self.go_back) self.copy = tk.Button(self.bottom_menu, image=copy_icon, bg='dodgerblue3', relief=tk.FLAT, command=self.copy_contact) self.edit = tk.Button(self.bottom_menu, image=edit_icon, bg='dodgerblue3', relief=tk.FLAT, command=self.edit_contact) self.delete = tk.Button(self.bottom_menu, image=delete_icon, bg='dodgerblue3', relief=tk.FLAT, command=self.delete_contact) self.back.grid(row=0, column=0, padx=(10,0), pady=4) self.copy.grid(row=0, column=1, padx=(40,0), pady=4) self.edit.grid(row=0, column=2, padx=(40,0), pady=4) self.delete.grid(row=0, column=3, padx=(40,0), pady=4) else: self.first_name = CustomEntry(self.first, width=39) self.last_name = tk.Entry(self.last, width=39) self.phone_number = CustomPhone(self.phone, width=39) self.email_id = tk.Entry(self.email, width=39) self.first_name.focus_set() self.first_name.grid(row=0, column=0) self.last_name.grid(row=0, column=0) self.phone_number.grid(row=0, column=0) self.email_id.grid(row=0, column=0) self.cancel = tk.Button(self.bottom_menu, image=cancel_icon, bg='dodgerblue3', relief=tk.FLAT, command=self.go_back) self.save_contact = tk.Button(self.bottom_menu, image=save_icon, bg='dodgerblue3', relief=tk.FLAT, activebackground='dodgerblue3', command=self.save_contact_in_db) self.cancel.grid(row=0, column=0, padx=5) self.save_contact.grid(row=0, column=1, padx=180) # Custom Functions def enumerate_content(self, var=None,indx=None, mode=None): self.contact_list.delete(0, tk.END) term = self.search_term.get() if term == '' or term == 'Search Contact': for index, contact in enumerate(self.contacts): self.contact_list.insert(index, (' ' + contact[0] + ' ' + contact[1])) self.num_contacts['text'] = f'{len(self.contacts)} contacts saved' else: temp_contact = [contact for contact in self.contacts if (term.lower() in (contact[0].lower() + ' ' + contact[1].lower()))] for index, contact in enumerate(temp_contact): self.contact_list.insert(index, (' ' + contact[0] + ' ' + contact[1])) self.num_contacts['text'] = f'{len(temp_contact)} contacts found' def show_contact(self, event=None): if event: widget = event.widget selection=widget.curselection() if selection: self.data = self.contacts[selection[0]] if self.data: self.on_home = True self.edit_contacts = False self.draw_display_contact_frame() self.first_name['text'] = self.data[0] self.last_name['text'] = self.data[1] self.phone_number['text'] = self.data[2] self.email_id['text'] = self.data[3] self.prev = self.data self.update() def copy_contact(self): self.clipboard_clear() self.clipboard_append(self.data[2]) messagebox.showinfo('CMS', 'Phone number copied to clipboard') def edit_contact(self): self.on_home = False self.is_new_contact = False self.edit_contacts = True self.new_contact_frame.destroy() self.draw_display_contact_frame() self.first_name.delete(0, tk.END) self.first_name.insert(0, self.data[0]) self.last_name.delete(0, tk.END) self.last_name.insert(0, self.data[1]) self.phone_number.delete(0, tk.END) self.phone_number.insert(0, self.data[2]) self.email_id.delete(0, tk.END) self.email_id.insert(0, self.data[3]) def create_new_contact(self): self.on_home = True self.is_new_contact = True self.edit_contacts = True self.draw_display_contact_frame() def save_contact_in_db(self): first_name = self.first_name.get().strip().lower().capitalize() last_name = self.last_name.get().strip().lower().capitalize() phone = self.phone_number.get().strip() email = self.email_id.get().strip() temp_contact = [(contact[0] + ' ' + contact[1]) for contact in self.contacts] if (first_name == '' or first_name == 'This field is required' or phone == '' or phone == 'This field is required'): messagebox.showerror('CMS', 'Fill in the Mandatory Fields') elif len(phone) != 10: messagebox.showerror('CMS', 'Enter 10 digit phone number') elif email and not valid_email(email): messagebox.showerror('CMS', 'Invalid email') else: if self.is_new_contact: if (first_name + ' ' + last_name) in temp_contact: messagebox.showerror('CMS', 'Contact with this name already\nexist, edit & try again') else: status = sql_operations.insert_item([first_name ,last_name, phone, email]) if status == 'success': messagebox.showinfo('CMS', 'Contact Saved') else: messagebox.showerror('CMS', 'An error Occured') self.contacts = sql_operations.fetch_all_result() self.data = (first_name ,last_name, phone, email) self.show_contact() else: status = sql_operations.delete_item(self.prev[0], self.prev[1]) if status == 'success': status = sql_operations.insert_item([first_name ,last_name, phone, email]) if status == 'success': messagebox.showinfo('CMS', 'Contact Saved') else: messagebox.showerror('CMS', 'An error Occured') self.contacts = sql_operations.fetch_all_result() self.data = (first_name ,last_name, phone, email) self.prev = self.data self.show_contact() def delete_contact(self): status = sql_operations.delete_item(self.data[0], self.data[1]) if status == 'success': messagebox.showinfo('CMS', 'Contact deleted') else: messagebox.showerror('CMS', 'An error Occured') self.contacts = sql_operations.fetch_all_result() self.data = None self.on_home = True self.go_back() def go_back(self): if self.on_home: self.new_contact_frame.destroy() self.draw_all_contact_frame() self.update() else: self.new_contact_frame.destroy() self.show_contact()
def draw_display_contact_frame(self): self.search_term.set('') # frames self.new_contact_frame = tk.Frame(self, width=300, height=400, bg='gray') self.create_contact_frame = tk.Frame(self.new_contact_frame, width=300, height=350) self.bottom_menu = tk.Frame(self.new_contact_frame, width=300, height=50, bg='dodgerblue3') self.new_contact_frame.grid(row=1, column=0) self.create_contact_frame.grid(row=0, column=0) self.bottom_menu.grid(row=1, column=0) self.new_contact_frame.grid_propagate(False) self.create_contact_frame.grid_propagate(False) self.bottom_menu.grid_propagate(False) # widgets self.first = tk.LabelFrame(self.create_contact_frame, text='*First Name', bg='dodgerblue3', width=250, height=40, fg='white') self.last = tk.LabelFrame(self.create_contact_frame, text='Last Name', bg='dodgerblue3', width=250, height=40, fg='white') self.phone = tk.LabelFrame(self.create_contact_frame, text='*Phone', bg='dodgerblue3', width=250, height=40, fg='white') self.email = tk.LabelFrame(self.create_contact_frame, text='Email', bg='dodgerblue3', width=250, height=40, fg='white') self.first.grid(row=0, column=0, padx=25, pady=(30,0)) self.last.grid(row=1, column=0, padx=25, pady=(30,0)) self.phone.grid(row=2, column=0, padx=25, pady=(30,0)) self.email.grid(row=3, column=0, padx=25, pady=(30,0)) self.first.grid_propagate() self.last.grid_propagate() self.phone.grid_propagate() self.email.grid_propagate() if not self.edit_contacts: self.first_name = tk.Label(self.first, width=33, anchor='w') self.last_name = tk.Label(self.last, width=33, anchor='w') self.phone_number = tk.Label(self.phone, width=33, anchor='w') self.email_id = tk.Label(self.email, width=33, anchor='w') self.first_name.grid(row=0, column=0) self.last_name.grid(row=0, column=0) self.phone_number.grid(row=0, column=0) self.email_id.grid(row=0, column=0) self.back = tk.Button(self.bottom_menu, image=back_icon, bg='dodgerblue3', relief=tk.FLAT, command=self.go_back) self.copy = tk.Button(self.bottom_menu, image=copy_icon, bg='dodgerblue3', relief=tk.FLAT, command=self.copy_contact) self.edit = tk.Button(self.bottom_menu, image=edit_icon, bg='dodgerblue3', relief=tk.FLAT, command=self.edit_contact) self.delete = tk.Button(self.bottom_menu, image=delete_icon, bg='dodgerblue3', relief=tk.FLAT, command=self.delete_contact) self.back.grid(row=0, column=0, padx=(10,0), pady=4) self.copy.grid(row=0, column=1, padx=(40,0), pady=4) self.edit.grid(row=0, column=2, padx=(40,0), pady=4) self.delete.grid(row=0, column=3, padx=(40,0), pady=4) else: self.first_name = CustomEntry(self.first, width=39) self.last_name = tk.Entry(self.last, width=39) self.phone_number = CustomPhone(self.phone, width=39) self.email_id = tk.Entry(self.email, width=39) self.first_name.focus_set() self.first_name.grid(row=0, column=0) self.last_name.grid(row=0, column=0) self.phone_number.grid(row=0, column=0) self.email_id.grid(row=0, column=0) self.cancel = tk.Button(self.bottom_menu, image=cancel_icon, bg='dodgerblue3', relief=tk.FLAT, command=self.go_back) self.save_contact = tk.Button(self.bottom_menu, image=save_icon, bg='dodgerblue3', relief=tk.FLAT, activebackground='dodgerblue3', command=self.save_contact_in_db) self.cancel.grid(row=0, column=0, padx=5) self.save_contact.grid(row=0, column=1, padx=180)
class Application(tk.Frame): def __init__(self, master=None): super().__init__(master=master) self.master = master self.grid() self.draw_frames() self.draw_title_widgets() self.draw_body_widgets() self.itemCost = { 'Tea': 10, 'Coffee': 15, 'Pastery': 80, 'Pizza': 199, 'Fries': 60, 'Burger': 49, 'Pepsi': 14, 'Cookies': 4 } self.discount_dict = { 0: 0, 100: 1, 300: 2, 500: 3, 1000: 5, 2000: 8, 5000: 15 } self.itemList = [item for item in self.itemCost.keys()] self.entryValues = [tk.StringVar() for item in self.itemList] self.expression = '' self.draw_item_frame_widgets() self.draw_bill_frame_widgets() self.draw_controller_frame_widgets() self.draw_calculator_frame_widgets() def draw_frames(self): self.title_frame = tk.Frame(self, width=800, height=105, bg='white') self.main_frame = tk.LabelFrame(self, width=800, height=280, bg='white') self.title_frame.grid(row=0, column=0) self.main_frame.grid(row=1, column=0, pady=5) self.title_frame.grid_propagate(False) self.main_frame.grid_propagate(False) def draw_title_widgets(self): self.icon = tk.Label(self.title_frame, image=coffee_icon1, bg='white') self.icon.grid(row=0, column=0, rowspan=2, padx=(10, 3)) self.title = tk.Label(self.title_frame, width=24, height=2, text='Honest Bistro Cafe', font=('verdana', 22, 'bold'), fg="#248aa2", bg="white") self.title.grid(row=0, column=1, columnspan=3) self.l1 = tk.Label(self.title_frame, bg='#248aa2', width=25) self.l2 = tk.Label(self.title_frame, bg='#248aa2', width=5) self.l1.grid(row=1, column=1) self.l2.grid(row=1, column=3) self.date_time = tk.Label(self.title_frame, text=self.get_current_datetime(), fg='#fe4a49', font=('verdana', 12, 'bold')) self.date_time.after(1000, self.update_datetime_label) self.date_time.grid(row=1, column=2, padx=5) def draw_body_widgets(self): self.items_frame = CustomFrame(self.main_frame, text='Cafe Items', height=240, width=170) self.items_frame.grid(row=0, column=0, padx=(15, 10), pady=15, rowspan=2, sticky='NW') self.bill_frame = CustomFrame(self.main_frame, text='Items Bill', height=200, width=192) self.bill_frame.grid(row=0, column=1, padx=10, pady=(15, 0)) self.controller = CustomFrame(self.main_frame, width=192, height=30, borderwidth=0) self.controller.grid(row=1, column=1, padx=10, sticky='N') self.calculator = CustomFrame(self.main_frame, text='Calculator', height=200, width=168) self.calculator.grid(row=0, column=2, padx=5, pady=15, sticky='NW') self.contact = CustomFrame(self.main_frame, width=192, height=30, borderwidth=0) self.contact.grid(row=1, column=2, padx=10, sticky='N') l1 = tk.Label(self.contact, bg='#248aa2', width=25, height=2, text=' Opens 09:00 to 08:30 \n Contact : 876655444', font=('Verdana', 8, 'bold'), fg='white', anchor='w') l1.grid(row=0, column=0) self.items_frame.grid_propagate(False) self.bill_frame.grid_propagate(False) self.controller.grid_propagate(False) self.calculator.grid_propagate(False) def draw_item_frame_widgets(self): for row, item in enumerate(self.itemList): label = CustomLabel(self.items_frame, text=(' ' + item)) label.grid(row=row, column=0, pady=(4, 0)) entry = CustomEntry(self.items_frame, textvariable=self.entryValues[row]) entry.grid(row=row, column=1, pady=(4, 0)) def draw_bill_frame_widgets(self): self.chargeList = [ 'Items Cost', 'Service Charge', 'GST Charges', 'Discount', 'Total' ] self.chargeValues = [tk.StringVar() for item in self.chargeList] for row, item in enumerate(self.chargeList): label = CustomLabel(self.bill_frame, text=item, width=12) label.grid(row=row, column=0, pady=(3, 0)) entry = CustomEntry(self.bill_frame, textvariable=self.chargeValues[row]) entry.grid(row=row, column=1, pady=(3, 0)) self.name_frame = tk.LabelFrame(self.bill_frame) self.name_frame.grid(row=5, column=0, columnspan=2, pady=(15, 0), padx=3) self.customer_name = tk.StringVar() self.name = CustomLabel(self.name_frame, text='Name', width=7) self.name_entry = CustomEntry(self.name_frame, width=15, textvariable=self.customer_name) self.name.grid(row=0, column=0, pady=2, padx=1) self.name_entry.grid(row=0, column=1, pady=2, padx=1) def draw_controller_frame_widgets(self): self.total = CustomButton(self.controller, text='Calculate', command=self.calculate_payment) self.total.grid(row=0, column=0, padx=(2, 0)) self.clear = CustomButton(self.controller, text='Clear', command=self.clear_all) self.clear.grid(row=0, column=1, padx=(2, 0)) self.receipt = CustomButton(self.controller, text='Receipt', command=self.get_receipt) self.receipt.grid(row=0, column=2, padx=(2, 0)) def draw_calculator_frame_widgets(self): self.input_string = tk.StringVar() self.calc_entry = CustomEntry(self.calculator, width=23, textvariable=self.input_string) self.calc_entry.grid(row=0, column=0, columnspan=4, pady=(5, 3), padx=4) self.seven = self.create_button(self.calculator, '7', '#248aa2', "white", lambda: self.get('7'), 1, 0) self.eight = self.create_button(self.calculator, '8', '#248aa2', "white", lambda: self.get('8'), 1, 1) self.nine = self.create_button(self.calculator, '9', '#248aa2', "white", lambda: self.get('9'), 1, 2) self.plus = self.create_button(self.calculator, '+', 'white', "black", lambda: self.get('+'), 1, 3) self.four = self.create_button(self.calculator, '4', '#248aa2', "white", lambda: self.get('4'), 2, 0) self.five = self.create_button(self.calculator, '5', '#248aa2', "white", lambda: self.get('5'), 2, 1) self.six = self.create_button(self.calculator, '6', '#248aa2', "white", lambda: self.get('6'), 2, 2) self.minus = self.create_button(self.calculator, '-', 'white', "black", lambda: self.get('-'), 2, 3) self.one = self.create_button(self.calculator, '1', '#248aa2', "white", lambda: self.get('1'), 3, 0) self.two = self.create_button(self.calculator, '2', '#248aa2', "white", lambda: self.get('2'), 3, 1) self.three = self.create_button(self.calculator, '3', '#248aa2', "white", lambda: self.get('3'), 3, 2) self.mult = self.create_button(self.calculator, '*', 'white', "black", lambda: self.get('*'), 3, 3) self.clear = self.create_button(self.calculator, 'C', '#248aa2', "white", self.delete_calc_text, 4, 0) self.zero = self.create_button(self.calculator, '0', '#248aa2', "white", lambda: self.get('0'), 4, 1) self.equal = self.create_button(self.calculator, '=', '#248aa2', "white", self.evaluate_expression, 4, 2) self.div = self.create_button(self.calculator, '/', 'white', "black", lambda: self.get('/'), 4, 3) def create_button(self, parent, text, bg, fg, command, r, c): self.button = tk.Button(parent, bg=bg, fg=fg, font=('Arial', 10, 'bold')) self.button['text'] = text self.button['command'] = command self.button.config(height=1, width=3) self.button.grid(row=r, column=c, pady=4) return self.button def get(self, value): ops = ['+', '-', '*', '/'] if self.expression == 'error': self.expression = '' if len(self.expression) == 0: if value in ['+', '-']: self.expression += value elif value in ['*', '/']: pass else: self.expression += value elif len(self.expression) > 0: if value in ops and self.expression[-1] in ops: self.expression = self.expression[:-1] + value else: self.expression += value self.input_string.set(self.expression) def evaluate_expression(self): if len(self.expression) > 0: try: self.expression = str(round(eval(self.expression), 2)) except: self.expression = 'error' self.input_string.set(self.expression) def delete_calc_text(self): self.expression = '' self.calc_entry.delete(0, 'end') def get_current_datetime(self): dt = datetime.datetime.now() return dt.strftime('%b %d ,%Y %I:%M:%S %p') def update_datetime_label(self): dt = self.get_current_datetime() self.date_time['text'] = dt self.date_time.after(1000, self.update_datetime_label) def get_order_details(self): total_cost = 0 for index, item in enumerate(self.itemList): qty = self.entryValues[index].get() if qty: self.hasBought = True cost = int(qty) * self.itemCost[item] total_cost += cost if self.hasBought: service_charge = round(0.04 * total_cost, 2) gst = round(0.05 * total_cost, 2) total = total_cost + service_charge + gst disc = 0 for d in self.discount_dict.keys(): if total >= d: disc = d cut = self.discount_dict[disc] discount = round((cut / 100) * total_cost, 2) total = round(total - discount, 2) else: total_cost, service_charge, gst, discount, total = [ 0 for i in range(5) ] return total_cost, service_charge, gst, discount, total def calculate_payment(self): self.hasBought = False total_cost, service_charge, gst, discount, total = self.get_order_details( ) if self.hasBought: self.chargeValues[0].set(str(total_cost)) self.chargeValues[1].set(str(service_charge)) self.chargeValues[2].set(str(gst)) self.chargeValues[3].set(str(discount)) self.chargeValues[4].set(str(total)) def get_receipt(self): if self.chargeValues[4].get() != '': total_cost, service_charge, gst, discount, total = [ self.chargeValues[i].get() for i in range(5) ] name = self.customer_name.get() if name: current = self.make_entry() with open(f'receipts/order_receipt_{current}.txt', 'w') as file: file.write('Order Summary\n') file.write(f'Customer Name : {name}\n') file.write( f'Order date time : {self.get_current_datetime()}\n\n') i, q, p, t = 'item', 'quantity', 'price', 'total cost' file.write(f' {i:<12} | {q:<8} | {p:<8} | {t:<12}\n') for index, item in enumerate(self.itemList): qty = self.entryValues[index].get() if qty: self.hasBought = True c = self.itemCost[item] cost = int(qty) * c file.write( f' {item.lower():<12}| {qty:<8} | {c:<8} | {cost:<12}\n' ) file.write('\n') file.write(f'SubCost : {total_cost}\n') file.write(f'Service Charge : {service_charge}\n') file.write(f'GST : {gst}\n') file.write(f'Discount : {discount}\n') file.write(f'Total Cost : {total}\n') messagebox.showinfo('Honest Bistro', 'Receipt Generated') else: messagebox.showerror('Honest Bistro', 'Customer Name is required.') else: messagebox.showerror('Honest Bistro', 'No items Bought') def make_entry(self): with open('receipts/orders.txt') as file: current = file.readline() current = int(current[6:]) + 1 with open('receipts/orders.txt', 'w') as file: file.write(('OrderN' + str(current))) return current def clear_all(self): for index, item in enumerate(self.itemList): self.entryValues[index].set('') for index, item in enumerate(self.chargeList): self.chargeValues[index].set('') self.customer_name.set('') self.hasBought = False
class Application(tk.Frame): def __init__(self, master=None): super().__init__(master=master) self.master = master self.pack() self.db = Database() self.all_credentials = self.db.fetch_all_creds() self._attributes() self.draw_home_frame() def _attributes(self): self.on_home = True self.pwd_hidden = True self.username = tk.StringVar() self.email_id = tk.StringVar() self.password = tk.StringVar() self.captcha = None self.current_user = None def draw_home_frame(self): self.head = tk.Frame(self, width=250, height=150) self.body = tk.Frame(self, width=250, height=100) self.head.grid(row=0, column=0) self.body.grid(row=1, column=0) self.head.grid_propagate(False) self.body.grid_propagate(False) self.header = tk.Label(self.head, width=120, height=120, image=avtar, text='\nAuthenticator', compound=tk.TOP, fg='black', font=('Lucida Bright', 12, 'bold')) self.header.grid(row=0, column=0, pady=8, padx=60) self.signup_page = ttk.Button(self.body, text='New User ?\n SignUp', command=self.draw_signup_frame, width=30) self.login_page = ttk.Button(self.body, text='Login', command=self.draw_login_frame, width=30) self.signup_page.grid(row=0, column=0, padx=28, pady=5) self.login_page.grid(row=1, column=0, padx=28, pady=8) if len(self.all_credentials) == 0: self.login_page['state'] = 'disabled' else: self.login_page['state'] = 'enabled' def draw_signup_frame(self): self.head.destroy() self.body.destroy() self._attributes() # header self.head = tk.Frame(self, width=250, height=40) self.head.grid(row=0, column=0) self.head.grid_propagate(False) self.header = tk.Label(self.head, text='Create Your Account', fg='dodgerblue2', font=('Lucida Bright', 16)) self.header.grid(row=0, column=0, padx=15, pady=5) # body self.body = tk.Frame(self, width=250, height=210) self.body.grid(row=1, column=0) self.body.grid_propagate(False) self.account_frame = ttk.LabelFrame(self.body, text='Account', width=240, height=110) self.captcha_frame = ttk.LabelFrame(self.body, text='Captcha', width=240, height=55) self.back_btn = tk.Button(self.body, image=back, command=self.to_home_page, relief=tk.FLAT) self.submit_btn = ttk.Button(self.body, text='Submit', width=20, command=self.register_user) self.account_frame.grid(row=0, column=0, padx=5, pady=(5, 2), columnspan=2) self.captcha_frame.grid(row=1, column=0, padx=5, columnspan=2) self.back_btn.grid(row=2, column=0, pady=5) self.submit_btn.grid(row=2, column=1, pady=5) self.account_frame.grid_propagate(False) self.captcha_frame.grid_propagate(False) self.nameLabel = ttk.Label(self.account_frame, text='Username', anchor='w', width=9) self.nameEntry = ttk.Entry(self.account_frame, textvariable=self.username, width=26) self.emailLabel = ttk.Label(self.account_frame, text='Email', anchor='w', width=9) self.emailEntry = ttk.Entry(self.account_frame, textvariable=self.email_id, width=26) self.pwdLabel = ttk.Label(self.account_frame, text='Password', anchor='w', width=9) self.pwdEntry = ttk.Entry(self.account_frame, textvariable=self.password, width=23, show='*') self.pwdState = tk.Button(self.account_frame, image=show, relief=tk.FLAT, command=self.hide_unhide_pwd) self.nameLabel.grid(row=0, column=0, padx=3, pady=3) self.nameEntry.grid(row=0, column=1, padx=3, pady=3, columnspan=2, sticky='w') self.emailLabel.grid(row=1, column=0, padx=3, pady=3) self.emailEntry.grid(row=1, column=1, padx=3, pady=3, columnspan=2, sticky='w') self.pwdLabel.grid(row=2, column=0, padx=3, pady=3) self.pwdEntry.grid(row=2, column=1, padx=(3, 0), pady=3, columnspan=1, sticky='w') self.pwdState.grid(row=2, column=2) self.captcha = self.generate_captcha() self.captcha_label = tk.Label(self.captcha_frame, text=self.captcha, width=8, bg='coral2', fg='white') self.captcha_refresh = tk.Button(self.captcha_frame, image=recaptcha, relief=tk.FLAT, command=self.generate_captcha) self.captcha_entry = ttk.Entry(self.captcha_frame, width=10) self.captcha_label.grid(row=0, column=0, padx=(10, 5)) self.captcha_refresh.grid(row=0, column=1, padx=20) self.captcha_entry.grid(row=0, column=2, padx=5) def draw_login_frame(self): self.head.destroy() self.body.destroy() self.head = tk.Frame(self, width=250, height=150) self.body = tk.Frame(self, width=250, height=100) self.head.grid(row=0, column=0) self.body.grid(row=1, column=0) self.head.grid_propagate(False) self.body.grid_propagate(False) self.header = tk.Label(self.head, width=100, height=140, image=avtar, text='\nLogin', compound=tk.TOP, fg='black', font=('Arial', 12, 'bold'), cursor='hand2') self.header.bind("<Button-1>", self.to_home_page) self.header.grid(row=0, column=0, pady=8, padx=70) if self.on_home: self.email_ = CustomEntry(self.body, text='Email', width=26, textvariable=self.email_id) self.email_.grid(row=0, column=0, padx=(25, 12), pady=20) self.send = tk.Button(self.body, image=send, relief=tk.FLAT, command=self.fetch_email) self.send.grid(row=0, column=1, padx=10, pady=20) self.not_registered = tk.Label( self.body, text='Not a registered user, SignUp here', font=('Arial', 8, 'underline'), fg='blue', cursor='hand2') self.not_registered.bind("<Button-1>", self.to_signup) self.not_registered.grid(row=1, column=0, columnspan=2) else: self.header['text'] = '\nHello ' + self.username.get() self.pass_ = CustomEntry(self.body, text='Password', width=26, textvariable=self.password) self.pass_.grid(row=0, column=0, padx=20, pady=20) self.send = tk.Button(self.body, image=send, relief=tk.FLAT, command=self.verify_login) self.send.grid(row=0, column=1, padx=10, pady=20) # custom functions def hide_unhide_pwd(self): if self.pwd_hidden == True: self.pwd_hidden = False self.pwdEntry.config(show='') self.pwdState['image'] = hide else: self.pwd_hidden = True self.pwdEntry.config(show='*') self.pwdState['image'] = show def to_home_page(self, event=None): self._attributes() self.head.destroy() self.body.destroy() self.draw_home_frame() def register_user(self): username = self.username.get() email = self.email_id.get() password = self.password.get() captcha = self.captcha_entry.get() if not username: messagebox.showinfo('Authenticator', 'Username required') elif not email: messagebox.showinfo('Authenticator', 'Email required') elif email and not valid_email(email): messagebox.showerror('Authenticator', 'Invalid email') elif not password: messagebox.showinfo('Authenticator', 'Password required') elif not captcha: messagebox.showinfo('Authenticator', 'Fill captcha to continue') else: if str(eval(self.captcha)) == captcha: password = encode_password(password) self.db.insert_credential((username, email, password)) self.all_credentials = self.db.fetch_all_creds() messagebox.showinfo('Authenticator', 'Registration Successfull') self._attributes() self.current_user = (username, email, password) self.on_home = False self.draw_login_frame() self.header['text'] = f'Hello {username}' self.header.config(font=('Arial', 10, 'bold')) else: self.captcha_entry.delete(0, tk.END) self.generate_captcha() def generate_captcha(self): captcha = str(random.randint(1, 100)) + ' ' + random.choice( ['+', '-', '*']) + ' ' + str(random.randint(1, 100)) if self.captcha is not None: self.captcha_label['text'] = captcha self.captcha = captcha return captcha def to_home(self): self.on_home = True self.email_id.set('') self.body.destroy() self.draw_widgets() def to_signup(self, event): self.draw_signup_frame() def fetch_email(self): email = self.email_.get() if email not in (' ', 'Email'): data = self.db.fetch_credential(email) if data is not None: self.current_user = data self.on_home = False self.password.set('') self.draw_login_frame() self.header['text'] = f'\nHello {self.current_user[0]}' self.header.config(font=('Arial', 9, 'bold')) else: messagebox.showerror('Authenticator', 'Not registered') def verify_login(self): password = decode_password(self.current_user[2]) if self.pass_.get() == password: self.is_success() else: messagebox.showerror('Authenticator', 'Incorrect Password') self.pass_.delete(0, tk.END) def is_success(self): self.head.destroy() self.body.destroy() self.page = tk.Frame(self, width=250, height=250, bg='white') self.page.grid(row=0, column=0) self.page.grid_propagate(False) self.frame = 0 self.should_continue = True self.label = tk.Label(self.page, image=success_list[self.frame]) self.label.grid(row=0, column=0, pady=20, padx=50) self.after(0, self.update) def update(self): if self.frame <= len(success_list) - 2: self.frame += 1 else: self.should_continue = False l2 = tk.Label(self.page, text='LoggedIn Successfully', fg='dodgerblue2', font=('Lucida Bright', 12, 'bold')) l2.grid(row=1, column=0) self.write_current_user_record() self.after(2000, self.quit) if self.should_continue: self.label.configure(image=success_list[self.frame]) self.after(20, self.update) def write_current_user_record(self): dct = { 'username': self.current_user[0], 'email': self.current_user[1], 'password': str(self.current_user[2]) } with open('files/current_user.json', 'w') as file: json.dump(dct, file, indent=4)
class Application(tk.Frame): def __init__(self, master=None): super().__init__(master=master) self.master = master self.grid() self.focus() self.draw_frames() self.draw_header_widgets() self.draw_body_widgets() self.bind('<Return>', self.search_user) self.master.protocol("WM_DELETE_WINDOW", self.stop_process) def draw_frames(self): self.title_frame = tk.Frame(self, width=300, height=50, bg='dodgerblue3') self.insta_frame = tk.Frame(self, width=100, height=100, bg='white') self.search_frame = tk.Frame(self, width=300, height=50, bg='white') tk.Label(self, width=56, bg='dodgerblue3').grid(row=2, column=0, columnspan=4) self.profile_frame = tk.Frame(self, width=200, height=150, bg='#b3cde0') self.info_frame = tk.Frame(self, width=200, height=150, bg='#b3cde0') self.download_frame = tk.Frame(self, width=400, height=50, bg='dodgerblue3') self.title_frame.grid(row=0, column=1, columnspan=3) self.search_frame.grid(row=1, column=1, columnspan=3) self.insta_frame.grid(row=0, column=0, rowspan=2) self.profile_frame.grid(row=3, column=0, columnspan=2) self.info_frame.grid(row=3, column=2, columnspan=2) self.download_frame.grid(row=4, column=0, columnspan=4) self.title_frame.grid_propagate(False) self.search_frame.grid_propagate(False) self.insta_frame.grid_propagate(False) self.profile_frame.grid_propagate(False) self.info_frame.grid_propagate(False) self.download_frame.grid_propagate(False) def draw_header_widgets(self): self.header = tk.Label(self.title_frame, text='InstaLoader', font=('Copperplate Gothic Light', 25, 'bold'), bg='white') self.header.grid(row=0, column=0, pady=(10, 5), padx=15) self.username = tk.StringVar() self.entry = CustomEntry(self.search_frame, width=33, textvariable=self.username) self.entry.grid(row=0, column=0, pady=(8, 5), padx=(15, 2)) self.entry.bind('<Return>', self.search_user) self.search = tk.Button(self.search_frame, image=search_icon, bg='white', cursor='hand2', command=self.search_user, relief=tk.FLAT) self.search.grid(row=0, column=1, pady=(8, 5), padx=2) self.settings = tk.Button(self.search_frame, image=settings_icon, bg='white', cursor='hand2', command=self.ask_credentials, relief=tk.FLAT, height=45, width=30) self.settings.grid(row=0, column=2, pady=0, padx=0) self.left_label = tk.Label(self.insta_frame, width=1, height=8, bg='dodgerblue3') self.insta_label = tk.Label(self.insta_frame, width=90, height=97, image=insta_icon, bg='white') self.left_label.grid(row=0, column=0) self.insta_label.grid(row=0, column=1) def draw_body_widgets(self): self.profile_name = CustomLabel(self.profile_frame, width=25, height=2, anchor='c') self.profile_pic = tk.Button(self.profile_frame, text='click to Open', compound=tk.TOP, relief=tk.FLAT) self.profile_name.grid(row=0, column=0) self.profile_pic.grid(row=1, column=0, padx=6, pady=8) self.profile_pic.grid_forget() self.followers_label = CustomLabel(self.info_frame) self.following_label = CustomLabel(self.info_frame) self.post_label = CustomLabel(self.info_frame) self.followers_count = CountLabel(self.info_frame) self.following_count = CountLabel(self.info_frame) self.post_count = CountLabel(self.info_frame) self.followers_label.grid(row=0, column=0, padx=(15, 2), pady=(5, 2)) self.following_label.grid(row=1, column=0, padx=(15, 2), pady=2) self.post_label.grid(row=2, column=0, padx=2, pady=(15, 2)) self.followers_count.grid(row=0, column=1, padx=2, pady=(5, 2)) self.following_count.grid(row=1, column=1, padx=2, pady=2) self.post_count.grid(row=2, column=1, padx=2, pady=2) self.download_button = tk.Button(self.download_frame, bg='green', text='Download all posts', fg='white', width=20) self.download_button.grid(row=0, column=0, pady=10, padx=30) self.download_button.grid_forget() self.stop_button = tk.Button(self.download_frame, bg='green', text='Quit Application', fg='white', width=20, command=self.stop_process) self.stop_button.grid(row=0, column=1, pady=10, padx=40) self.stop_button.grid_forget() def search_user(self, event=None): self.search.config(state=tk.DISABLED) thread = threading.Thread(target=self.search_user_profile) thread.start() self.poll_thread(thread) def poll_thread(self, thread): if thread.is_alive(): self.after(100, lambda: self.poll_thread(thread)) else: self.search.config(state=tk.NORMAL) def search_user_profile(self): self.user = self.username.get() if self.user and self.user != 'Enter Instagram Username': account_details = read_credentials() if account_details: username, password = account_details else: self.ask_credentials() account_details = read_credentials() if account_details: username, password = account_details else: username, password = None, None if username and password: try: self.loader = instaloader.Instaloader() self.loader.login(username, password) self.profile = Profile.from_username( self.loader.context, self.user) img_url = self.profile.get_profile_pic_url() name = self.profile.full_name following = self.profile.get_followees().count followers = self.profile.get_followers().count posts = self.profile.get_posts().count followers = self.update_numbers(followers) following = self.update_numbers(following) posts = self.update_numbers(posts) self.profile_name['text'] = name self.followers_label['text'] = 'Followers' self.following_label['text'] = 'Following' self.post_label['text'] = 'Posts' self.followers_count['text'] = followers self.following_count['text'] = following self.post_count['text'] = posts self.update_idletasks() r = requests.get(img_url) with open('profile.jpg', 'wb') as file: file.write(r.content) self.profile_pic.grid(row=1, column=0, padx=6, pady=2) self.profile_pic.config(width=180, height=105) self.profile_image = ImageTk.PhotoImage( Image.open('profile.jpg').resize((160, 95))) self.profile_pic['image'] = self.profile_image self.profile_pic['command'] = self.display_profile_pic self.download_button.grid(row=0, column=0, pady=10, padx=30) self.stop_button.grid(row=0, column=1, pady=10, padx=40) self.download_button['command'] = self.download_all_posts except instaloader.exceptions.InstaloaderException: messagebox.showerror('Instaloader', 'No internet connection') except instaloader.exceptions.ProfileNotExistsException: messagebox.showerror('Instaloader', 'Profile does not exist') except instaloader.exceptions.InstaloaderException: messagebox.showerror('Instaloader', 'Incorrect username or password') else: print('Enter Insta Username') def display_profile_pic(self): self.after(100, lambda: subprocess.run(['explorer', 'profile.jpg'])) def ask_credentials(self): CredentialWindow() def download_all_posts(self): print('starting') self.stop_button['text'] = 'Stop Download & Quit' self.search.config(state=tk.DISABLED) thread1 = threading.Thread(target=self.download_all) thread1.start() self.poll_thread(thread1) def download_all(self): self.loader.download_profile(self.user, download_stories=False, download_tagged=False) def stop_process(self): PROCNAME = "python.exe" for proc in psutil.process_iter(): # check whether the process name matches if proc.name() == PROCNAME: proc.kill() def update_numbers(self, num): if num >= 1000000: num /= 1000000 num = f'{num:.1f}M' elif num >= 1000: num /= 1000 num = f'{num:.1f}K' else: num = str(num) return num