英文:
How do we turn an image into a background for a window using PySimpleGUI?
问题
我想将以下图像设置为我和朋友正在尝试制作的应用程序的背景。然而,我们尝试了多种方法,但都没有成功。
我们尝试了各种方法,查看了YouTube和Stack Overflow上的内容。
这是我们的代码:
import PySimpleGUI as sg
window = sg.Window(title="Cobalt", layout=[[]], margins=(487, 245)).read()
英文:
I want to make the following image the background for an app me and my friend are trying to make. However, we tried multiple methods and none succeeded.
We tried various methods, looking on youtube and Stack Overflow.
Here's our code:
import PySimpleGUI as sg
window = sg.Window(title="Cobalt", layout=[[]], margins=(487, 245)).read()
答案1
得分: 1
以下是您提供的代码的翻译部分:
这里使用了tkinter代码,但未对所有元素进行测试。
此刻可能需要不同的布局代码。
所有的框架和列都将填充背景图像。
需要更多的tkinter编程经验。
from io import BytesIO
from PIL import Image
import PySimpleGUI as sg
def image_to_data(im):
with BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
return data
def make_background(window, file, main_frame):
global images
def find_frames(widget):
widgets = list(widget.children.values())
if isinstance(widget, (sg.tk.Frame, sg.tk.LabelFrame)):
widget.update()
x, y = widget.winfo_rootx() - x0, widget.winfo_rooty() - y0
width, height = widget.winfo_width(), widget.winfo_height()
new_im = im_.crop((x, y, x+width, y+height))
image = sg.tk.PhotoImage(data=image_to_data(new_im))
images.append(image)
label = sg.tk.Label(widget, image=image, padx=0, pady=0, bd=0, bg=bg)
label.place(x=0, y=0)
label.lower()
for widget in widgets:
find_frames(widget)
size = window.size
im_ = Image.open(file).resize(size)
root = window.TKroot
widgets = list(root.children.values())
x0, y0 = root.winfo_rootx(), root.winfo_rooty()
frame = sg.tk.Frame(root, padx=0, pady=0, bd=0, bg=bg)
frame.place(x=0, y=0)
images = []
image = sg.tk.PhotoImage(data=image_to_data(im_))
images.append(image)
label = sg.tk.Label(frame, image=image, padx=0, pady=0, bd=0, bg=bg)
label.pack()
main_frame.Widget.master.place(in_=frame, anchor='center', relx=.5, rely=.5)
frame.lower()
frame.update()
for widget in widgets:
find_frames(widget)
bg = sg.theme_background_color()
background_image_file = 'd:/background.png'
w, h = size = 640, 480 # 背景图像的尺寸
sg.set_options(dpi_awareness=True)
frame = [
[sg.Input(size=(30, 1), expand_x=True),
sg.Button('浏览')],
[sg.Multiline('', expand_x=True, expand_y=True)],
[sg.Button('退出')],
]
# 只需一个框架,以将其移至窗口中央
layout = [[sg.Frame('', frame, size=(350, 200), border_width=0, key='FRAME', background_color=bg)]]
location = sg.Window.get_screen_size()
window = sg.Window('背景演示', layout, margins=(0, 0), grab_anywhere=True,
size=size, keep_on_top=True, finalize=True,
no_titlebar=True,
transparent_color=bg,
)
images = []
make_background(window, background_image_file, window['FRAME'])
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Cancel', '退出'):
break
print(event)
window.close()
请注意,代码中的注释和字符串内容并未被翻译,保持原文不变。
英文:
Here, tkinter code used for it, not tested for all elements.
It may need different code for different layout at this moment.
All Frames and Columns will be filled background image too here.
Need more tkinter programming experience for it.
from io import BytesIO
from PIL import Image
import PySimpleGUI as sg
def image_to_data(im):
with BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
return data
def make_background(window, file, main_frame):
global images
def find_frames(widget):
widgets = list(widget.children.values())
if isinstance(widget, (sg.tk.Frame, sg.tk.LabelFrame)):
widget.update()
x, y = widget.winfo_rootx() - x0, widget.winfo_rooty() - y0
width, height = widget.winfo_width(), widget.winfo_height()
new_im = im_.crop((x, y, x+width, y+height))
image = sg.tk.PhotoImage(data=image_to_data(new_im))
images.append(image)
label = sg.tk.Label(widget, image=image, padx=0, pady=0, bd=0, bg=bg)
label.place(x=0, y=0)
label.lower()
for widget in widgets:
find_frames(widget)
size = window.size
im_ = Image.open(file).resize(size)
root = window.TKroot
widgets = list(root.children.values())
x0, y0 = root.winfo_rootx(), root.winfo_rooty()
frame = sg.tk.Frame(root, padx=0, pady=0, bd=0, bg=bg)
frame.place(x=0, y=0)
images = []
image = sg.tk.PhotoImage(data=image_to_data(im_))
images.append(image)
label = sg.tk.Label(frame, image=image, padx=0, pady=0, bd=0, bg=bg)
label.pack()
main_frame.Widget.master.place(in_=frame, anchor='center', relx=.5, rely=.5)
frame.lower()
frame.update()
for widget in widgets:
find_frames(widget)
bg = sg.theme_background_color()
background_image_file = 'd:/background.png'
w, h = size = 640, 480 # size of background image
sg.set_options(dpi_awareness=True)
frame = [
[sg.Input(size=(30, 1), expand_x=True),
sg.Button('Browse')],
[sg.Multiline('', expand_x=True, expand_y=True)],
[sg.Button('Exit')],
]
# Need only one frame here to move it to center of window
layout = [[sg.Frame('', frame, size=(350, 200), border_width=0, key='FRAME', background_color=bg)]]
location = sg.Window.get_screen_size()
window = sg.Window('Background Demo', layout, margins=(0, 0), grab_anywhere=True,
size=size, keep_on_top=True, finalize=True,
no_titlebar=True,
transparent_color=bg,
)
images = []
make_background(window, background_image_file, window['FRAME'])
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Cancel', 'Exit'):
break
print(event)
window.close()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论