Bottom Page

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()
Quote
#2
what GUI framework you use? maybe show some code...
Quote
#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.
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Restoring Tkinter widget background to original color pythonprogrammer 1 273 Dec-16-2019, 04:59 AM
Last Post: woooee
  Make Label Text background (default color) transparent using tkinter in python barry76 1 3,255 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  Unable to put background image on Tkinter Frame jenkins43 2 1,347 Nov-27-2019, 11:38 AM
Last Post: jenkins43
  tkinter window and turtle window error 1885 3 590 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] changing background color of a button ieee488 2 577 Sep-06-2019, 01:24 PM
Last Post: ieee488
  change background color of button ieee488 3 2,535 Jul-30-2019, 09:49 PM
Last Post: ieee488
  Button with Image Icon Friend 2 530 Jul-25-2019, 09:39 AM
Last Post: Friend
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 664 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] How to run animations in background with tkinter? Pythenx 1 817 May-20-2019, 07:10 PM
Last Post: Yoriz
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 896 May-12-2019, 08:17 PM
Last Post: joe_momma

Forum Jump:


Users browsing this thread: 1 Guest(s)