Search code examples
pythonpython-3.xpysimplegui

PySimpleGUI how to place a button on top of image


here is example with image and "image as button" but i want to place a litte button on the image, it is possible ? with normal python i can do that with image.place(40,40....) method and how to do that with "PySimpleGUI"

import PySimpleGUI as sg

# Define the layout with an image and a button on top of it
layout = [
    [sg.Image(filename='image.png', background_color='white')],
    [sg.Button('Click Me', image_filename='image.png', button_color=('white', 'white'))]
]

# Create the window
window = sg.Window('Button on Image', layout)

# Event loop
while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break

# Close the window
window.close()

enter image description here


Solution

  • tkinter code required here, so no comment about it.

    import PySimpleGUI as sg
    
    layout = [
        [sg.Image(filename='image1.png', background_color='white', key='IMAGE')],
        [sg.Button('Click Me', image_filename='image2.png', button_color=('white', 'white'), pad=(0, 0))]
    ]
    
    window = sg.Window('Button on Image', layout, finalize=True)
    
    window.refresh()
    click = window["Click Me"].widget
    w1, h1 = window['IMAGE'].get_size()
    w2, h2 = window['Click Me'].get_size()
    master = click.master
    master.place(x=(w1-w2)//2, y=(h1-h2)//2, bordermode=sg.tk.INSIDE)
    
    window.move_to_center()
    window.read(close=True)
    

    enter image description here