如何使用PYSIMPLEGUI在文本wdiget中显示函数的输出?

2024-09-28 22:23:13 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在弹出文本输出中显示函数的结果

import PySimpleGUI as sg

FILMS = [["There will be blood", "Paul Thomas Anderson", 2007],
      ["Lion King","Rob Minkoff", 1994],
      ["Toy Story","Josh Cooley",1995],
      ["Monty Python's Life of Brian","Terry Jones",1979],
      ["Die Hard","John McTiernan",1988],
      ["Rocky","John G. Avildsen",1976]]


def listprint():
    for i in range (len(FILMS)):
        print(FILMS[i][0])

event, values = sg.Window('Find a film', [[sg.Text('Are you ready:')],[sg.B("OK",key="-OK-"), sg.B("Return",key="-Return-")] ]).read(close=True)

if event == '-OK-':
    sg.popup('Film names will be printed in the python output:',command = listprint)

Tags: key函数in文本importeventreturnok
1条回答
网友
1楼 · 发布于 2024-09-28 22:23:13

只需将结果传递给sg.popup,但使用*解压列表/元组。有时,它看起来不太好,您可以使用用户定义的弹出窗口来显示它们

enter image description here

import PySimpleGUI as sg

def film_name():
    return [item[0] for item in FILMS]

FILMS = [
    ["There will be blood", "Paul Thomas Anderson", 2007],
    ["Lion King","Rob Minkoff", 1994],
    ["Toy Story","Josh Cooley",1995],
    ["Monty Python's Life of Brian","Terry Jones",1979],
    ["Die Hard","John McTiernan",1988],
    ["Rocky","John G. Avildsen",1976],
]

layout = [
    [sg.Text('Are you ready:')],
    [sg.B("OK"), sg.B("Return")],
]

window = sg.Window('Find a film', layout, finalize=True)

while True:
    event, values = window.read()
    if event in (sg.WINDOW_CLOSED, 'Return'):
        break
    elif event == 'OK':
        sg.popup('Film names will be printed in the python output:', *film_name())
    print(event, values)

window.close()

相关问题 更多 >