Home > OS >  (PySimpleGUI) How to move an image button to given coords
(PySimpleGUI) How to move an image button to given coords

Time:02-05

This is almost certainly, very trivial however im dumb so help.

import PySimpleGUI as sg
ChessMenuOption= sg.Button('',image_filename = r'C:\Users\benja\Documents\Python\Chess project\Chess_Selection_Image.png')
sg.Window(title = 'Chess', layout = [[ChessMenuOption]] , size = (800,800)).read()

This places the button at the topleft (0,0), how do I place it somewhere else?

CodePudding user response:

import PySimpleGUI as sg

dummy_element = sg.Text('', size=(100, 100))
ChessMenuOption = sg.Button('', image_filename=r'C:\Users\benja\Documents\Python\Chess project\Chess_Selection_Image.png', button_color=('white', 'white'), pad=(0, 0), size=(800, 800))
layout = [[dummy_element, ChessMenuOption]]
window = sg.Window(title='Chess', layout=layout, size=(900, 900))

window.read()

CodePudding user response:

Button cannot be moved in PySimpleGUI.

If your project is about the chess, you can use the Graph element, then you can move figures in the Graph.

Example Code

import base64
import io
from PIL import Image
import PySimpleGUI as sg

def rotate(data):
    file = io.BytesIO(base64.b64decode(data))
    im = Image.open(file)
    im = im.transpose(Image.ROTATE_180)
    with io.BytesIO() as output:
        im.save(output, format="PNG")
        data = output.getvalue()
    return data

width, height = size = (640, 640)
dx, dy = width//8, height//8
ddx, ddy = (dx-56)//2, (dy-56)//2

layout = [
    [sg.Graph(size, (0, 0), size, pad=(0, 0), background_color='green', enable_events=True, key='Graph')],
]
window = sg.Window('Chess', layout, margins=(0, 0), finalize=True)
graph = window['Graph']

for i in range(8):
    for j in range(8):
        graph.draw_rectangle(
            ((i*dx), (j 1)*dy),
            ((i 1)*dx, j*dy),
            fill_color='white' if (i j) % 2 else 'black')

figures = {}

for i, image in enumerate(sg.EMOJI_BASE64_HAPPY_LIST[:8]):
    figure = graph.draw_image(data=image, location=(i*dx ddx, dy-ddy))
    figures[(i, 0)] = figure
    figure = graph.draw_image(data=sg.EMOJI_BASE64_WIZARD, location=(i*dx ddx, 2*dy-ddy))
    figures[(i, 1)] = figure

for i, image in enumerate(sg.EMOJI_BASE64_SAD_LIST[:8]):
    figure = graph.draw_image(data=rotate(image), location=(i*dx ddx, height-ddy))
    figures[(i, 7)] = figure
    figure = graph.draw_image(data=rotate(sg.EMOJI_BASE64_JEDI), location=(i*dx ddx, height-dy-ddy))
    figures[(i, 6)] = figure

selected, rectangle = False, None

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'Graph':
        x, y = values[event]
        i, j = x//dx, y//dy
        if selected:
            if (i, j) not in figures:
                figure = figures[(i0, j0)]
                graph.relocate_figure(figure, i*dx ddx, (j 1)*dy-ddy)
                del figures[(i0, j0)]
                i0, j0 = i, j
                figures[(i0, j0)] = figure
            else:
                i0, j0 = i, j
            graph.delete_figure(rectangle)
            rectangle = graph.draw_rectangle((i0*dx, (j0 1)*dy), ((i0 1)*dx, j0*dy), line_color='yellow', line_width=5)
        else:
            if (i, j) in figures:
                i0, j0 = i, j
                rectangle = graph.draw_rectangle((i0*dx, (j0 1)*dy), ((i0 1)*dx, j0*dy), line_color='yellow', line_width=5)
                selected = True
            else:
                if rectangle:
                    graph.delete_figure(rectangle)

window.close()

enter image description here

  • Related