我试图使用PySimpleGui在一个简单的GUI中显示一个图像,但它可能比我想象的要复杂一些。
这就是我目前正在尝试的:
import io
import os
import PySimpleGUI as sg
from PIL import Image
import sys
# GUI #
layout = [
[sg.Image('2C.png')]
[sg.Submit(), sg.Cancel()]
]
window = sg.Window('My Program', layout)
button,values = window.read()
这就是崇高抛出的错误:
C:(Directory)file.py:12: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
[sg.Image('2C.png')]
Traceback (most recent call last):
File "file.py", line 12, in <module>
[sg.Image('2C.png')]
TypeError: list indices must be integers or slices, not tuple
真的不知道谷歌该做些什么,因为PySimpleGUI的资源如此之少。
谢谢你的阅读
卡兰
发布于 2022-01-27 04:00:18
在[sg.Image('2C.png')]
后面缺少一个逗号
发布于 2022-02-01 06:32:46
你漏掉了逗号。请确保图像位于当前工作目录中,或者提供图像的完整可访问路径,如下面的示例。
image="/storage/emulated/0/Pycode2/folder.png"
或在此文件夹中放置图片并通过此代码更改当前目录。这不是一个好的编程实践。
#choose directory to work use
os.chdir('/storage/emulated/0/Pycode2/')
你的正确代码是
layout = [
[sg.Image('2C.png'),],
[sg.Submit(), sg.Cancel(),],
]
请注意,这些额外的逗号不会导致错误,并非常有助于添加新的布局项目。缺少逗号会引发此错误。
发布于 2022-09-12 03:10:38
我按照下面的代码创建了一个GUI。我有两个不同的问题:
)。
# ----------- Importing Libraries ---------------
import PySimpleGUI as sg
from datetime import datetime
import base64
sg.theme('DarkTeal9')
#------------------------------Create single layouts----------------------------------
flower_base64 = "image code is here"
layout_img = [[sg.Button('ciccio', image_data=flower_base64, button_color=(sg.theme_background_color(),sg.theme_background_color()),border_width=0, key='-GRAPH-')]]
layout_1 = [[sg.InputText("", key="-IT2-", font='Arial 9', size=(10,1)),
sg.Combo(["Item1", "Item2", "Item3"],size=(20,1), key='-TEST2-', font='Arial 9'),
sg.CalendarButton("", close_when_date_chosen=True, target='-IN2-', font='Arial 9', no_titlebar=False, format='%d-%b-%Y'),
sg.InputText("", key='-IN2-', size=(20,1), font='Arial 9')]]
layout_a = [[sg.Button("row 2")]]
layout_2 = [[sg.InputText("", key="-IT3-", font='Arial 9', size=(10,1)),
sg.Combo(["Item1", "Item2", "Item3"],size=(20,1), key='-TEST3-', font='Arial 9'),
sg.CalendarButton("", close_when_date_chosen=True, target='-IN3-', font='Arial 9', no_titlebar=False, format='%d-%b-%Y'),
sg.InputText("", key='-IN3-', size=(20,1), font='Arial 9')]]
layout_b =[[sg.Button("row 3")]]
layout_3 = [[sg.InputText("", key="-IT4-", font='Arial 9', size=(10,1), visible=True),
sg.Combo(["Item1", "Item2", "Item3"],size=(20,1), key='-TEST4-', font='Arial 9'),
sg.CalendarButton("", close_when_date_chosen=True, target='-IN4-', font='Arial 9', no_titlebar=False, format='%d-%b-%Y'),
sg.InputText("", key='-IN4-', size=(20,1), font='Arial 9', justification="c")]]
#------------------------------Create master layout----------------------------------
layout = [[sg.Column(layout_img, key="-AZZ-")],
[sg.Column(layout_1, key='-LAY1-'), sg.Column(layout_a, visible=True, key="-LAYA-")],
[sg.Column(layout_2, visible=False, key='-LAY2-'), sg.Column(layout_b, visible=False, key='-LAYB-')],
[sg.Column(layout_3, visible=False, key='-LAY3-')],
[sg.Button ("Save"), sg.Button ("Load"), sg.Button("Upload"), sg.Button('Exit')]]
window = sg.Window("", layout, no_titlebar=True)
while True:
event, values = window.read()
if event == 'Save':
filename = sg.popup_get_file("Save", save_as=True, no_window=True)
window.SaveToDisk(filename)
if event == 'Load':
filename = sg.popup_get_file('Load', no_window=True)
window.LoadFromDisk(filename)
if "-IT2-":
window[f'-LAY2-'].update(visible=True)
window[f'-LAYA-'].update(visible=False)
if "-IT3-":
window[f'-LAY3-'].update(visible=True)
if event == sg.WIN_CLOSED or event == 'Exit':
break
window.close()
if event == 'row 2':
window[f'-LAY2-'].update(visible=True)
window[f'-LAYA-'].update(visible=False)
window[f'-LAYB-'].update(visible=True)
layout = str(event)
if event == 'row 3':
window[f'-LAY3-'].update(visible=True)
window[f'-LAYB-'].update(visible=False)
layout = str(event)
window.close()
https://stackoverflow.com/questions/70877668
复制