gray_farz
Posts: 34
Joined: Mon Mar 20, 2017 6:13 am

how to set button background with window background

Thu Oct 12, 2017 7:59 am

hello freinds
i have put an image (self.image) on my window background
now,
i need to put another image(mi_but1) on this window..
my problem:
image mi_but1 is put on window with a gray square behind it(a gray square between image mi_but1 and window's background)
the final gui doesnt look nice
i don't want this gray background...
what should I do? how can i remove this gray square behind the button(button background)? OR how can i put the image of window background(self.image) in behind of the button (button background)
my code:

Code: Select all

from tkinter import *
from PIL import Image, ImageTk

root = Tk()
root.title("Title")
root.geometry("600x600")
root.configure(background="black")

class Example(Frame):
    def __init__(self, master, *pargs):
        Frame.__init__(self, master, *pargs)

        self.image = Image.open("/home/pi/Desktop/images/background/image.png")
        self.img_copy= self.image.copy()


        self.background_image = ImageTk.PhotoImage(self.image)

        self.background = Label(self, image=self.background_image)
        self.background.pack(fill=BOTH, expand=YES)
        self.background.bind('<Configure>', self._resize_image)

        mi_but1=PhotoImage(file="/home/pi/Downloads/images/buttons/starlarg.png")
        button2 = Button(self.background, text='button2',image=mi_but1)
        button2.image=mi_but1
        button2.pack(side='top')

    def _resize_image(self,event):

        new_width = event.width
        new_height = event.height

        self.image = self.img_copy.resize((new_width, new_height))

        self.background_image = ImageTk.PhotoImage(self.image)
        self.background.configure(image =  self.background_image)

e = Example(root)
e.pack(fill=BOTH, expand=YES)
root.mainloop()

scotty101
Posts: 2333
Joined: Fri Jun 08, 2012 6:03 pm

Re: how to set button background with window background

Thu Oct 12, 2017 8:59 am

Two things you could try

Set the button's border width to zero

Code: Select all

button2 = Button(..., borderwidth=0)
Or use a GIF image with transparency
https://stackoverflow.com/questions/443 ... background
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

gray_farz
Posts: 34
Joined: Mon Mar 20, 2017 6:13 am

Re: how to set button background with window background

Wed Oct 18, 2017 10:15 am

scotty101 wrote:
Thu Oct 12, 2017 8:59 am
Two things you could try

Set the button's border width to zero

Code: Select all

button2 = Button(..., borderwidth=0)
Or use a GIF image with transparency
https://stackoverflow.com/questions/443 ... background
i tried 'borderwidth' before...its not usable..
my image doesn't have any background itself...the button creats(has) a background itself?????

Return to “Python”

Who is online

Users browsing this forum: No registered users and 22 guests