Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 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
  How Can I Do Refresh Window? satrancali 0 73 Feb-03-2018, 07:50 AM
Last Post: satrancali
  [Tkinter] problem with button events Lubik_ 4 333 Dec-01-2017, 08:47 PM
Last Post: Windspar
  [Tkinter] No menu bar in window bde69 5 293 Nov-25-2017, 06:35 PM
Last Post: heiner55
  Button not defined, why? gb_johnny 6 602 Nov-13-2017, 12:19 AM
Last Post: Windspar
  [Tkinter] Window unresponsive when executed. fawazcode 2 214 Sep-11-2017, 12:29 AM
Last Post: Larz60+
  tkinter button commands Lux 1 671 Aug-01-2017, 03:02 AM
Last Post: Larz60+
  Need help sizing a window Larz60+ 0 300 Jul-05-2017, 07:06 PM
Last Post: Larz60+
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 923 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE
  Closing Modal Window in QT nieselfriem 0 469 Apr-19-2017, 03:32 PM
Last Post: nieselfriem
  which button should be shown gray 1 519 Mar-30-2017, 11:57 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)