python-3.x 如何枚举列表中所有传入的值

esyap4oy  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(117)

我正在学习python,目前正在完成我的todo应用程序,但我最不想改变的是,所有传入的值都将自动与它们的索引一起出现。我希望用户看到的任务前面的任务数。我试着使用枚举函数,但据我所知,你只能在列表中已经有一个值之后使用它,我想做的是添加一个已经包含索引号的值。
因此,如果我想添加一个新的todo '做作业',我希望结果是'4.做作业'
gui.py

import time

import functions
import PySimpleGUI as sg

sg.theme('DarkBrown1')

clock = sg.Text('', key='clock')
label = sg.Text('Type in a to-do')
input_box = sg.InputText(tooltip='Enter todo', key='todo')
add_button = sg.Button('Add')
list_box = sg.Listbox(values=functions.get_todos(), key='todos',
                      enable_events=True, size=[45, 10])
edit_button = sg.Button('Edit')
complete_button = sg.Button('Complete')
exit_button = sg.Button('Exit')

window = sg.Window('To-Do App',
                   layout=[[clock],
                           [label],
                           [input_box, add_button],
                           [list_box,edit_button, complete_button],
                           [exit_button]],
                   font=('Helvetica', 20))
while True:
    event, values = window.read(timeout=10)
    window['clock'].update(value=time.strftime("%b %d, %Y %H:%M:%S"))
    match event:
        case 'Add':
            todos = functions.get_todos()
            new_todo =  values['todo'] + '\n'
            todos.append(new_todo)
            functions.write_todos(todos)
            window['todos'].update(values=todos)
        case 'Edit':
            try:
                todo_to_edit = values['todos'][0]
                new_todo = values['todo']
                todo = functions.get_todos()
                index = todos.index(todo_to_edit)
                todos[index] = new_todo
                functions.write_todos(todos)
                window['todos'].update(values=todos)
            except IndexError:
                sg.popup('Pls select an item first', font=('Helvetica', 20))
        case 'Complete':
            try:
                todo_to_complete = values['todos'][0]
                todos = functions.get_todos()
                todos.remove(todo_to_complete)
                functions.write_todos(todos)
                window['todos'].update(values=todos)
                window['todo'].update(value='')
            except IndexError:
                sg.popup('Pls select an item first', font=('Helvetica', 20))
        case 'Exit':
            break
        case 'todos':
            window['todo'].update(value=values['todos'][0])
        case sg.WIN_CLOSED:
            break
window.close()

字符串
functions.py

FILEPATH = 'todos'
def get_todos(filepath=FILEPATH):
    with open(filepath, 'r') as file_local:
        todos_local = file_local.readlines()
    return todos_local

def write_todos(todos_arg, filepath=FILEPATH):
    with open(filepath, 'w') as file:
        file.writelines(todos_arg)

if __name__ == '__main__':
    print('Hello')
    print(get_todos())

vfhzx4xs

vfhzx4xs1#

在更新Listbox元素之前,将项目列表转换为以索引号开始,或者在将其添加到项目列表中进行更新之前,将其转换为以索引号开始。

import PySimpleGUI as sg

def add_index(items):
    return [f'{i+1}. {item}' for i, item in enumerate(items)]

items = []

layout = [
    [sg.Text("Type in a to-do")],
    [sg.Input(key='INPUT'), sg.Button("Add", bind_return_key=True)],
    [sg.Listbox(items, size=(45, 10), expand_x=True, key='LISTBOX')],
]
window = sg.Window('Demo', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == "Add":
        new_item = values["INPUT"].strip()
        if new_item:
            items.append(new_item)
            index = len(items) - 1
            window['LISTBOX'].update(add_index(items), set_to_index=[index], scroll_to_index=index)
            window['INPUT'].update(select=True)

window.close()

字符串


的数据

相关问题