Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove border of tkinter button created with an image

Tags:

python

tkinter

I've created a couple of buttons using my program and I've made them include images. However, I wish to now remove the border that remains (see http://i.imgur.com/XRlmq39.png for screenshot).

The code for the "back" button as an example:

backbutton = ttk.Button(mainframe, command=homereturn)
backbuttonimage = PhotoImage(file="back.gif")
backbutton.config(image=backbuttonimage)
backbutton.pack()
backbutton.grid(column=0, row=1)

Any help would be greatly appreciated.

like image 649
Jarrod Jones Avatar asked Aug 17 '15 22:08

Jarrod Jones


People also ask

What does FG mean in tkinter?

foreground − Foreground color for the widget. This can also be represented as fg. highlightbackground − Background color of the highlight region when the widget has focus. highlightcolor − Foreground color of the highlight region when the widget has focus.

Can tkinter display images?

Tkinter's label widget can be used to display either images or text. To display an image requires the use of Image and ImageTk imported from the Python Pillow (aka PIL) package.

How do you create a button on a tkinter canvas?

To create a button on a Tkinter Canvas, simply pass the parent as the canvas in place of a parent in the Button constructor.


2 Answers

backbutton = tk.Button(...,  highlightthickness = 0, bd = 0)

This works for Python 3.x, I tried...

enter image description here

 icon = PhotoImage(file="lock.png")
 self.company_lock_button = Button(self.control_lock_frame, image = icon, highlightthickness = 0, bd = 0)
 self.day_lock_button = Button(self.control_lock_frame, image = icon)
 self.hour_lock_button = Button(self.control_lock_frame, image = icon)
like image 187
Pakium Avatar answered Oct 05 '22 23:10

Pakium


If you are using images to define a custom button, use the standard button class rather than the ttk button class. This will allow you to set the borderwidth attribute to zero:

import tkinter as tk
...
backbutton = tk.Button(..., borderwidth=0)

(unrelated: there's no point in calling backbutton.pack() and immediately follow it with backbutton.grid(...) - you can only use one for a particular widget, and the last one you call is the one that has any effect. In this case the call to pack is completely useless)

like image 42
Bryan Oakley Avatar answered Oct 06 '22 00:10

Bryan Oakley