簡體   English   中英

從列表打印到列表框,pysimplegui

[英]Printing from a list to a listbox, pysimplegui

我正在嘗試使用 pysimplegui 將列表中的元素打印到列表框中。 我面臨的問題是元素每行打印一個字符,而不是每行整個元素。

import string
import subprocess
import winapps
import PySimpleGUI as sg
import os.path
import ctypes

file_list_column = [
    [
        sg.Text("File Path"),
        sg.In(size=(25, 1), enable_events=True, key="-File Path-"),
        sg.Button(size=(10, 1), enable_events=True, key="-AddButton-", button_text="Add"),
    ],
    [
        sg.Listbox(
            values=[], enable_events=True, size=(40, 20), key="-File List-"
        )
    ],
    [
        sg.Button(size=(15, 1), enable_events=True, key="-create-", button_text="Create")
    ],
]

layout = [
    [
        sg.Column(file_list_column),
    ]
]

window = sg.Window("Startup Script", layout)

while True:
    event, values = window.read()
    if event == "Exit" or event == sg.WIN_CLOSED:
        break
    for x in values:
        if event == "-AddButton-":
            filename = values["-File Path-"]
            values[x] = filename
            window["-File List-"].update(filename)
window.close()

在此處輸入圖像描述

語句window["-File List-"].update(filename)表示window["-File List-"].update(values=filename) ,不是插入一個新的文件名,而是替換整個Listbox元素具有一個新的列表filename ,現在是一個字符串,然后您將filename中的每個字符作為Listbox元素中的一個項目。

事件循環修改,

file_list = []

while True:

    event, values = window.read()

    if event in ("Exit", sg.WIN_CLOSED):
        break

    elif event == "-AddButton-":
        new_filename = values["-File Path-"].strip()
        if new_filename not in file_list:
            file_list = sorted(file_list + [new_filename])
            window["-File List-"].update(values=file_list)

window.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM