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
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 966 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  Transparent window background, but not text on it muzicman0 7 3,339 Feb-02-2024, 01:28 AM
Last Post: Joically
  Centering and adding a push button to a grid window, TKinter Edward_ 15 6,021 May-25-2023, 07:37 PM
Last Post: deanhystad
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 5,123 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 3,134 Apr-16-2022, 04:04 PM
Last Post: DBox
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,355 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  [Tkinter] Not able to get image as background in a Toplevel window finndude 4 4,139 Jan-07-2022, 10:10 PM
Last Post: finndude
  [Tkinter] Why does the image for the button not appear? finndude 4 2,251 Oct-21-2021, 06:41 PM
Last Post: deanhystad
  [Tkinter] image inside button rwahdan 4 7,494 Jul-12-2021, 08:49 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,856 Jun-16-2021, 06:08 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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