O'Reilly logo

Tkinter GUI Programming by Example by David Love

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Saving the user's choices

To ensure the user is able to save their choices, we need a Save button:

self.save_button = ttk.Button(self, text="Save", command=self.save)

Now we can finish off our __init__ method by packing all of our widgets:

self.save_button.pack(side=tk.BOTTOM, fill=tk.X, expand=1, padx=40)self.font_list.pack(side=tk.LEFT, fill=tk.Y, expand=1)self.size_input.pack(side=tk.BOTTOM, fill=tk.X, expand=1)

Since our Save button calls a method called save, let's define this now.

def save(self):    font_family = self.font_list.get(self.font_list.curselection()[0])    font_size = self.size_input.get()    yaml_file_contents = f"family: {font_family}\n" \                           + f"size: {font_size}"    with open('schemes/font.yaml', 'w') as file: file.write(yaml_file_contents) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required