从pysimplegui获取输入并将其传递给函数(python)

2024-09-28 21:11:08 发布

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

我希望允许用户使用将文件夹地址复制并粘贴到pysimplegui中,然后使用该文本获取文件夹中文件的列表

import PySimpleGUI as psg
import pathlib as pl

#set the theme for the screen/window
psg.theme("LightPurple")

#define layout
layout=[[psg.Text("Folder Address",size=(15, 1), font='Lucida',justification='right'),psg.Input()],
    [psg.Button("SAVE", font=("Times New Roman",12)),psg.Button("CANCEL", font=("Times New Roman",12))]]

#Define Window
win =psg.Window("Data Entry",layout)
#Read  values entered by user and pass that input to a windows path
v=win.read()
d = pl.WindowsPath(v)# I'm pretty sure this is where I'm going wrong. 

#close first window
win.close()

#define layout for second windows to display data entered by user in first window
layout1=[[psg.Text("The data you entered is  :", size=(20,1), font='Lucida', text_color='Magenta')],
        [psg.Text(str([e for e in dir_path.iterdir()]), font='Lucida', text_color='Blue')]]

#Define Window and display the layout to print output        
win1=psg.Window("Output Screen",layout1)

e,v=win1.read()
#close second window
win1.close()

我很乐意使用格式等,但如果有人有任何见解(或pysimplegui的良好教程),我将不胜感激


Tags: thetotext文件夹forclosewindowwin
1条回答
网友
1楼 · 发布于 2024-09-28 21:11:08

创建窗口布局后,在事件循环中等待键盘或鼠标输入的事件。一次又一次,直到窗口关闭或脚本结束

您可以在一个窗口中完成所有操作,修改代码如下。对于长输出,请在此处使用sg.Multiline

import PySimpleGUI as psg
import pathlib as pl

#set the theme for the screen/window
psg.theme("LightPurple")

font1 = ('Lucida', 12)
font2 = ("Times New Roman",12)
#define layout
layout=[
    [psg.Text("Folder Address",size=(15, 1), font=font1, justification='right'),
     psg.Input(key='-INPUT-')],
    [psg.Button("SAVE", font=font2),
     psg.Button("CANCEL", font=font2)],
    [psg.Text("The data you entered is  :", size=(20,1), font=font1, text_color='Magenta')],
    [psg.Multiline('', size=(80, 25), font=font1, text_color='Blue', key='-OUTPUT-')],
]

#Define Window
win = psg.Window("Data Entry",layout)

# Event loop
while True:

    event, values = win.read()
    if event in (psg.WINDOW_CLOSED, 'CANCEL'):
        break
    elif event == 'SAVE':
        v = values['-INPUT-']
        d = pl.WindowsPath(v)
        text = str([e for e in d.iterdir()])
        win['-OUTPUT-'].update(value=text)

#close window
win.close()

相关问题 更多 >