【问题标题】:How to display the output from a function in a text wdiget with PYSIMPLEGUI?如何使用 PYSIMPLEGUI 在文本小部件中显示函数的输出?
【发布时间】:2021-03-30 09:22:33
【问题描述】:

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

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)

【问题讨论】:

    标签: python python-3.x pysimplegui


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 2015-04-18
      相关资源
      最近更新 更多