class AddCharacterWindow(Frame): """ Handles the window for adding a character. Gets the characters name from the user, and opens the character edit window. """ def __init__(self, parent): self.parent = parent self.window = Toplevel(self.parent) self.window.resizable(width=False, height=False) self.character_library = CharacterLibrary() # Call pertinent methods for this window. self.entrybox_name() def entrybox_name(self): """ Gets name from the user with an entrybox, adds Okay and Cancel buttons. """ self.character_name = StringVar() lbl_enter_name = Label(self.window, text='Enter Character Name') ent_character_name = Entry(self.window, textvariable=self.character_name, bg='white') btn_cancel = Button(self.window, text='Cancel', command=self.cancel) btn_okay = Button(self.window, text='Okay', command=self.okay) # Grid management lbl_enter_name.grid(column=0, row=0, columnspan=2, sticky=W) ent_character_name.grid(column=0, row=1, columnspan=2, sticky=EW) btn_cancel.grid(column=0, row=2, sticky=E) btn_okay.grid(column=1, row=2, sticky=E) def cancel(self, *args): self.window.destroy() def okay(self, *args): """ Saves the blank character, opens the CharacterEditWindow, closes itself. """ new_character = Character(self.character_name.get()) self.character_library.save_character(new_character) character_edit_window = CharacterEditWindow(self.parent, self.character_name.get()) self.window.destroy()
class CharacterEditWindow(Frame): """ This handles the window for editing a character. """ def __init__(self, parent, character_name): # CharacterEdit Window initialization self.parent = parent self.window = Toplevel(self.parent) self.window.resizable(width=False, height=False) self.character_library = CharacterLibrary() self.character = self.character_library.get_character(character_name) # Call pertinent methods for this window. self.combobox_character() self.tree_skills() self.select_skills() #self.menu_skills() def combobox_character(self): """ Displays and manages the character selection for the CharacterEditWindow. """ # Get known characters. API calls or data retrieval here. character_names = self.character_library.get_character_list() # Creates the Combobox which has all known characters and automatically # selects the first character. Also other widgets. frm_character = Frame(self.window) lbl_character = Label(frm_character, text='Character:') self.cbx_character = ttk.Combobox(frm_character, values=character_names, width=14) self.cbx_character.set(self.character.name) # Grid management. frm_character.grid(column=0, row=0) lbl_character.grid(column=0, row=0, sticky=NW, padx=3, pady=3) self.cbx_character.grid(column=1, row=0, columnspan=2, sticky=NW, padx=3, pady=3) # Binding. self.cbx_character.bind('<<ComboboxSelected>>', self.change_character) def tree_skills(self): """ Shows available skills a character can use. """ frm_skills = Frame(self.window) self.tre_skills = ttk.Treeview(frm_skills, height=14, columns=('level')) scb_skills = Scrollbar(frm_skills, orient=VERTICAL, command=self.tre_skills.yview) self.tre_skills.column('#0', width=250, minwidth=150) self.tre_skills.column('level', width=30, minwidth=30) self.tre_skills.heading('level', text='Lvl') for parent in self.character.get_parent_skills(): self.tre_skills.insert('', 'end', parent, text=parent, tag='ttk') for child in self.character.get_children_skills(parent): self.character.skill_level self.tre_skills.insert(parent, 'end', child, text=child, tag='ttk') self.tre_skills.set(child, 'level', self.character.get_skill_level(child)) # Grid management. frm_skills.grid(column=0, row=1) self.tre_skills.grid(column=0, row=0, sticky=NW, padx=3, pady=3) scb_skills.grid(column=1, row=0, sticky=NE+S, pady=4) self.tre_skills.configure(yscrollcommand=scb_skills.set) # Bindings self.tre_skills.bind('<<TreeviewSelect>>', self.current_skill_changed) def select_skills(self): """ Shows the modules which allow you select a skill level, and approve the changes to a character. """ # Get the skill that selected. # Add widgets needed for this window frm_widgets = Frame(self.window) self.cbx_levels = ttk.Combobox(frm_widgets, values=(0, 1, 2, 3, 4, 5), width=8) btn_done = Button(frm_widgets, text='Done', command=self.done) # Grid management. frm_widgets.grid(column=0, row=2) self.cbx_levels.grid(column=0, row=1) btn_done.grid(column=1, row=1) # Bindings self.cbx_levels.bind('<<ComboboxSelected>>', self.change_skill) def current_skill_changed(self, *args): """ Called when an item in the listbox is selected. Updates the combobox with the selected items level and the lbl_current_skill.""" # Find the selected items level. current_skill = self.tre_skills.selection()[0] if current_skill not in self.character.get_all_skills(): pass else: # self.cbx_levels.set(self.character.get_skill_level(current_skill)) def change_skill(self, *args): """ Opens the dialog to select a skill level and changes it in the character class. """ # Use the lbl_current_skill HACK to find the skill and use the combobox # to determine how to change the characters skill. skill = self.tre_skills.selection()[0] level = self.cbx_levels.get() # Change the skill in the character. self.character.set_skill(skill, int(level)) # Saves changes to the character. self.character_library.save_character(self.character) # Update the listbox by calling its function self.tre_skills.set(skill, 'level', level) def change_character(self, *args): """ Changes the character. """ # Get the name of the character to change to. name = self.cbx_character.get() # Change the character self.character = self.character_library.get_character(name) # Display the change. self.tree_skills() def done(self, *args): """ Close window, call a function from the main UI to pass the modified character class. """ self.parent.update_character(self.character) self.window.destroy()