Title: Build a toolbar in Python and tkinter
This program creates a toolbar that holds three tools: a button that lets you choose a color, an OptionMenu that lets you select a line thickness, and a check button. It uses techniques described in the following examples.
See those posts for details about how to display a color chooser and how to make an OptionMenu display images. This example adds three main techniques: creating a toolbar, making a button display a color swatch created at runtime, and creating a check button.
Making a Toolbar
The following code shows how the program creates its toolbar.
def build_toolbar(self):
'''Build the toolbar.'''
# Create a toolbar
self.toolbar = tk.Frame(self.window, relief=tk.RIDGE, borderwidth=2)
# Add buttons and other tools to the toolbar.
self.tool_padx = self.tool_pady = 3
# Color button.
self.selected_color = 'red'
self.make_color_button(self.selected_color)
# Line thickness OptionMenu.
self.make_thickness_option_menu()
# Checkbutton.
self.make_checkbox()
# Place the toolbar
self.toolbar.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
This code creates a Frame to use as the toolbar. It sets tool_padx and tool_pady values so the methods that create the tools can use consistent padding. It then calls the make_color_button and make_thickness_option_menu to create the button and OptionMenu.
Making a Button Display a Color
The following code shows how the program creates its color button.
def make_color_button(self, color):
self.tool_hgt = 16
self.tool_wid = 16
self.color_button_image = tk.PhotoImage(width=self.tool_wid, height=self.tool_hgt)
self.color_button_image.put(color, to=(0,0,self.tool_wid-1,self.tool_wid-1))
self.color_button = tk.Button(self.toolbar, command=self.choose_color, image=self.color_button_image)
self.color_button.pack(side=tk.LEFT, padx=self.tool_padx, pady=self.tool_pady)
This code sets the tool_width and tool_height values so other tools can use those same values if desired. It then creates a PhotoImage with those dimensions, saving the image in the variable color_button_image. It then calls the image's put method to fill the image with the button's initial color.
Next, the code creates a button, setting its image property to the newly created PhotoImage. The method finishes by packing the new button.
When you click the button, the program executes the following code.
def choose_color(self):
color = colorchooser.askcolor(title='Choose Color',
initialcolor=self.selected_color)
if color[1] is not None:
# Save the selected color.
self.selected_color = color[1]
# Fill the color button with the selected color.
self.color_button_image.put(color[1],
to=(0,0,self.tool_wid-1,self.tool_wid-1))
This method displays a color chooser as describe in the post Display a color picker in Python and tkinter. If you pick a color, the method saves it in the variable selected_color in case you want to use it later. It then uses the button image's put method to fill the image with the new color. The button automatically updates itself to display the modified image.
Creating a Check Button
The last really interesting new piece of the program is the following make_checkbox method.
def make_checkbox(self):
self.checkbutton = tk.Checkbutton(self.toolbar, text='Do Cool Stuff?')
self.checkbutton.pack(side=tk.LEFT, padx=self.tool_padx, pady=self.tool_pady)
This method simply creates a check button and packs it. There's nothing really tricky here.
Download the example to see all of the details.
|