def change_name(self, event): """Change category name.""" def ok(event): cats = [l.cget('text').lower() for l in self.cat_labels.values()] cat = name.get().strip().lower() if cat and cat not in cats: label.configure(text=cat.capitalize()) if old_cat == self.default_category.get(): self.default_category.set(cat.capitalize()) self.update_def_cat_menu() name.destroy() label = event.widget old_cat = label.cget('text') name = Entry(self, justify='center') name.insert(0, label.cget('text')) name.place(in_=label, relx=0, rely=0, anchor='nw', relwidth=1, relheight=1) name.bind('<FocusOut>', lambda e: name.destroy()) name.bind('<Escape>', lambda e: name.destroy()) name.bind('<Return>', ok) name.selection_range(0, 'end') name.focus_set()
class UserTab: def __init__(self, main_window, tab_control, username, user_controller): """Initializes the user tab with all visual elements""" self.main_window = main_window self.username = username self.user_controller = user_controller self.user_info = self.user_controller.get_user(self.username) self.user_tab = ttk.Frame(tab_control) tab_control.add(self.user_tab, text='User') self.new_password = StringVar() self.new_password_repeat = StringVar() self.new_firstname = StringVar(value=self.user_info['firstname']) self.new_lastname = StringVar(value=self.user_info['lastname']) self.new_email = StringVar(value=self.user_info['email']) # USER INFORMATION Label(self.user_tab, text="Your information:", font='Helvetica 16 bold').grid(row=0, column=0, padx=15, pady=15, sticky='w') Label(self.user_tab, text="Username:"******"First name:").grid(row=2, column=0, sticky='w', padx=15, pady=5) Label(self.user_tab, textvariable=self.new_firstname).grid(row=2, column=1, sticky='w', pady=5) Label(self.user_tab, text="Last name:").grid(row=3, column=0, sticky='w', padx=15, pady=5) Label(self.user_tab, textvariable=self.new_lastname).grid(row=3, column=1, sticky='w', pady=5) Label(self.user_tab, text="Email:").grid(row=4, column=0, sticky='w', padx=15, pady=5) Label(self.user_tab, textvariable=self.new_email).grid(row=4, column=1, sticky='w', pady=5) # CHANGE USER INFORMATION BUTTON Button(self.user_tab, text='Modify information', command=self.change_information).grid(row=5, column=1, pady=10, sticky='w') # CHANGE PASSWORT FIELDS AND BUTTON Label(self.user_tab, text="Change password:"******"New password:"******"Repeat password:"******"""Creates fields where new user information can be typed and button to save it""" self.nameentry = Entry(self.user_tab, textvariable=self.new_firstname) self.nameentry.grid(row=2, column=1, sticky='w') self.lnameentry = Entry(self.user_tab, textvariable=self.new_lastname) self.lnameentry.grid(row=3, column=1, sticky='w') self.mailentry = Entry(self.user_tab, textvariable=self.new_email) self.mailentry.grid(row=4, column=1, sticky='w') self.btn = Button(self.user_tab, text='Submit changes', command=self.submit_changes) self.btn.grid(row=5, column=1, pady=10, sticky='ew') def submit_changes(self): """ Passes the new user information to user controller. If successful, fields are destroyed. If not, a messagebox is shown and the user can continue editing the information in the fields """ success, message = self.user_controller.edit_user( self.username, self.new_firstname.get(), self.new_lastname.get(), self.new_email.get()) if success: self.user_info = self.user_controller.get_user(self.username) self.new_firstname.set(self.user_info['firstname']) self.new_lastname.set(self.user_info['lastname']) self.new_email.set(self.user_info['email']) print(self.new_firstname.get(), self.new_lastname.get(), self.new_email.get()) self.nameentry.destroy() self.lnameentry.destroy() self.mailentry.destroy() self.btn.destroy() return else: messagebox.showerror("Input error", message) def change_password(self): """ Verifies basic conditions, such that password is repeated correctly and then passes the password to the user controller to change it in the DB. Shows messagebox for success and failure. """ if self.new_password.get() != self.new_password_repeat.get(): messagebox.showerror( "Error", "Password wasn't repeated correctly. Please check your password entry again." ) return password_changed, message = self.user_controller.edit_password( self.username, self.new_password.get()) if password_changed: self.new_password.set('') self.new_password_repeat.set('') messagebox._show("Success", message) else: messagebox.showerror("Input error", message) def logout(self): """Function that is called when the logout button is clicked. Triggers the closing of the window""" self.main_window.close_main_window()
def _edit(self, event, item): """Edit feed title.""" column = self.tree.identify_column(event.x) if column in ['#1', '#2']: bbox = self.tree.bbox(item, column) entry = Entry(self.tree) entry.place(x=bbox[0], y=bbox[1], width=bbox[2], height=bbox[3], anchor='nw') entry.bind('<Escape>', lambda e: entry.destroy()) entry.bind('<FocusOut>', lambda e: entry.destroy()) if column == '#1': entry.insert(0, self.tree.item(item, 'values')[0]) entry.configure(style='manager.TEntry') def ok(event): name = entry.get() if name: name = self.master.feed_rename( self.tree.set(item, 'Title'), name) self.tree.set(item, 'Title', name) entry.destroy() entry.bind('<Return>', ok) else: entry.insert(0, self.tree.item(item, 'values')[1]) entry.configure(style='no_edit.TEntry', validate='key', validatecommand=self._no_edit_entry) entry.selection_range(0, 'end') entry.focus_set() elif column == '#3': def focus_out(event): x, y = self.tree.winfo_pointerxy() x0 = combo.winfo_rootx() x1 = x0 + combo.winfo_width() y0 = combo.winfo_rooty() y1 = y0 + combo.winfo_height() if not (x0 <= x <= x1 and y0 <= y <= y1): combo.destroy() def ok(event): category = combo.get().strip() self.categories.add(category) self.master.feed_change_cat(self.tree.set(item, 'Title'), self.tree.set(item, 'Category'), category) self.tree.set(item, 'Category', category) combo.destroy() bbox = self.tree.bbox(item, column) cat = list(self.categories) combo = AutoCompleteCombobox(self.tree, values=cat, allow_other_values=True) combo.place(x=bbox[0], y=bbox[1], width=bbox[2], height=bbox[3], anchor='nw') combo.bind('<Escape>', lambda e: combo.destroy()) combo.bind('<FocusOut>', focus_out) combo.bind('<Return>', ok) combo.bind('<<ComboboxSelected>>', ok) combo.current(cat.index(self.tree.set(item, '#3')))