Python Forum
set button background with image of window background
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
set button background with image of window background
#1
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) 
see the attached file...it 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:
from tkinter import *
from PIL import Image, ImageTk

root = Tk()
root.title("Title")
root.geometry("400x400")
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()
Reply
#2
what GUI framework you use? maybe show some code...
Reply
#3
Don't set a geometry, let it expand with whatever is placed inside.
I use grid, not pack but this is my choice, you should be able to make place work

a label can work, but a frame is better
add a frame on top of your root window, use relative width and height as follows:
self.background = Frame(root, relwidth=1.0, relheight=1.0, bd=0, padx=0, pady=0, image=self.background_image)
self.background .grid(row=0, column=0, sticky='nsew')
I'm not sure I have the syntax right as I haven't tested, but it's close if not correct.

If you switch to grid, please be aware that all widgets in the same container (the frame) must use grid as well.
Reply
#4
(Oct-12-2017, 10:41 AM)Larz60+ Wrote: Don't set a geometry, let it expand with whatever is placed inside.
I use grid, not pack but this is my choice, you should be able to make place work

a label can work, but a frame is better
add a frame on top of your root window, use relative width and height as follows:
self.background = Frame(root, relwidth=1.0, relheight=1.0, bd=0, padx=0, pady=0, image=self.background_image)
self.background .grid(row=0, column=0, sticky='nsew')
I'm not sure I have the syntax right as I haven't tested, but it's close if not correct.

If you switch to grid, please be aware that all widgets in the same container (the frame) must use grid as well.
Frame doesn't have 'image' option
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter button image Nick_tkinter 4 68 Yesterday, 11:33 PM
Last Post: deanhystad
  Having trouble using photo for background blakefindlay 2 166 Jan-27-2021, 11:31 PM
Last Post: blakefindlay
Question closing a "nested" window with a button in PySimpleGUI and repeating this process Robby_PY 9 780 Jan-18-2021, 10:21 PM
Last Post: Serafim
  Trying to make random image loop in Tk window using python Jediguy18 1 245 Dec-30-2020, 04:56 AM
Last Post: deanhystad
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 480 Dec-10-2020, 07:48 PM
Last Post: Larz60+
Music [PyGUI] I can't run Scirpt Blender in background - Blender + Python Piter_PL 0 200 Nov-21-2020, 02:42 PM
Last Post: Piter_PL
Photo Tkinter TEXT background image _ShevaKadu 5 1,406 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 1,523 Oct-18-2020, 10:58 AM
Last Post: TurboC
  Scrollable big image in a window (TKinter) Prospekteur 3 452 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  Adding an image to a tkinter window djwilson0495 2 1,655 Aug-23-2020, 11:07 AM
Last Post: ebolisa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020