python-3.x (PySimpleGUI)如何将图像按钮移动到给定坐标

slmsl1lt  于 2023-02-06  发布在  Python
关注(0)|答案(2)|浏览(235)

这几乎是肯定的,非常微不足道,但我哑巴,所以帮助。

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()

这将按钮放置在topleft(0,0)上,如何将其放置在其他位置?

axzmvihb

axzmvihb1#

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()
ozxc1zmp

ozxc1zmp2#

无法在PySimpleGUI中移动按钮。
如果项目是关于国际象棋的,则可以使用Graph元素,然后可以在Graph中移动图形。
示例代码

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()

相关问题