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_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()
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.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