Python Tkinter:为列表框中的每个字符串创建一个变量?

Python Tkinter: Create a variable for each string inside a listbox?

我(几乎)是一个可怜的程序员处女所以请放轻松。

这是我第二次尝试制作一个程序,结果有点超出我的理解范围,恐怕。经过很长时间的努力,我请求你的帮助。

我基本上是在制作一个待办事项列表,但希望它具有更多功能,而不仅仅是一个无聊的列表。

我在脑海中想象的是,用户将任务添加到条目小部件中,然后该小部件将显示在列表框中。列表框中的每个字符串都会有一个与之关联的值(我需要对我希望程序具有的功能进行一些计算)。 所以我想我想要的是列表框中的每个字符串都成为一个变量,然后与我想要一个值的变量相关联。

我会尽力向您展示:

这里我添加了我想成为新变量的字符串

然后我从下拉菜单中指定一个数字。我希望这个数字是上一步中 variable/string 的值。

我真的希望你们中的一个人能够以一种(最好)不需要我做太多改变的方式引导我朝着正确的方向前进。事情对我来说仍然很棘手,我已经很难浏览代码了。 目的很简单,我想对与每个任务关联的(希望)即将成为的值进行一些计算。 如果你们敢,请提前致谢!

相关代码在这里:

import tkinter.messagebox # Import the messagebox module
import pickle # Module to save to .dat
import tkinter as tk

root = tk.Tk() #
root.title('SmaToDo') # Name of the program/window


def new_task():
    global entry_task
    global task_window
    task_window = Toplevel(root)
    task_window.title('Add a new task')
    task_label = tk.Label(task_window, text = 'Title your task concisely:', justify='center')
    task_label.pack()
    # Entry for tasks in new window
    entry_task = tkinter.Entry(task_window, width=50, justify='center')
    entry_task.pack()
    # Add task button in new window
    button_add_task = tkinter.Button(task_window, text='Add task', width=42, command=lambda: [add_task(), impact()])
    button_add_task.pack()

def add_task():
    global task
    global impact_window
    task = entry_task.get() # we get the task from entry_task and we get the input from the entry_task type-field with .get()
    if task != '': # If textbox inputfield is NOT empty do this:
        listbox_tasks.insert(tkinter.END, task)
        entry_task.delete(0, tkinter.END) # Slet hvad der står i inputfeltet fra første bogstav til sidste (0, tkinter.END)
        task_window.destroy()
    else:
        tkinter.messagebox.showwarning(title='Whoops', message='You must enter a task')
        task_window.destroy()


def delete_task():
    try:
        task_index = listbox_tasks.curselection()[0]
        listbox_tasks.delete(task_index)
    except:
        tkinter.messagebox.showwarning(title='Oops', message='You must select a task to delete')

def save_tasks():
    tasks = listbox_tasks.get(0, listbox_tasks.size())
    pickle.dump(tasks, open('tasks.dat', 'wb'))

def prioritize_tasks():
    pass



# Create UI
frame_tasks = tkinter.Frame(root)
frame_tasks.pack()

scrollbar_tasks = tkinter.Scrollbar(frame_tasks)
scrollbar_tasks.pack(side=tkinter.RIGHT, fill=tkinter.Y)

listbox_tasks = tkinter.Listbox(frame_tasks, height=10, width=50, justify='center') # tkinter.Listbox(where it should go, height=x, width=xx)
listbox_tasks.pack()

listbox_tasks.config(yscrollcommand=scrollbar_tasks.set)
scrollbar_tasks.config(command=listbox_tasks.yview)


try:
    tasks = pickle.load(open('tasks.dat', 'rb'))
    listbox_tasks.delete(0, tkinter.END)
    for task in tasks:
        listbox_tasks.insert(tkinter.END, task)
except:
    tkinter.messagebox.showwarning(title='Phew', message='You have no tasks')

# Add task button
button_new_task = tkinter.Button(root, text='New task', width=42, command=new_task)
button_new_task.pack()


button_delete_task = tkinter.Button(root, text='Delete task', width=42, command=delete_task)
button_delete_task.pack()


button_save_tasks = tkinter.Button(root, text='Save tasks', width=42, command=save_tasks)
button_save_tasks.pack()

button_prioritize_tasks = tkinter.Button(root, text='Prioritize', width=42, command=prioritize_tasks)
button_prioritize_tasks.pack()

root.mainloop() 

简单的方法是添加另一个list来存储影响值。您需要同步任务列表和影响列表。

修改后的代码如下:

import pickle # Module to save to .dat
import tkinter as tk
from tkinter import messagebox
import random

TASKS_FILE = "tasks.dat"
task_impacts = []  # store impact of tasks

root = tk.Tk() #
root.title('SmaToDo') # Name of the program/window

def impact():
    # assign random impact to new task
    task_impacts.append(random.randint(1, 11))    

def new_task():
    def add_task():
        task = entry_task.get().strip()
        if task:
            listbox_tasks.insert(tk.END, task)
            impact() # get the impact of the task
        else:
            messagebox.showwarning(title='Whoops', message='You must enter a task')
        task_window.destroy()
    
    task_window = tk.Toplevel(root)
    task_window.title('Add a new task')
    task_label = tk.Label(task_window, text = 'Title your task concisely:', justify='center')
    task_label.pack()
    # Entry for tasks in new window
    entry_task = tk.Entry(task_window, width=50, justify='center')
    entry_task.pack()
    # Add task button in new window
    button_add_task = tk.Button(task_window, text='Add task', width=42, command=add_task)
    button_add_task.pack()

def delete_task():
    try:
        task_index = listbox_tasks.curselection()[0]
        listbox_tasks.delete(task_index)
        task_impacts.pop(task_index) # remove corresponding impact value as well
    except:
        messagebox.showwarning(title='Oops', message='You must select a task to delete')

def load_tasks():
    try:
        with open(TASKS_FILE, 'rb') as f:
            tasks = pickle.load(f)
        listbox_tasks.delete(0, tk.END)
        task_impacts.clear()
        for task, impact in tasks:
            listbox_tasks.insert(tk.END, task)
            task_impacts.append(impact)
    except:
        messagebox.showwarning(title='Phew', message='You have no tasks')

def save_tasks():
    tasks = zip(listbox_tasks.get(0, tk.END), task_impacts)
    with open(TASKS_FILE, "wb") as f:
        pickle.dump(tasks, f)

def prioritize_tasks():
    print(list(zip(listbox_tasks.get(0, tk.END), task_impacts)))


# Create UI
frame_tasks = tk.Frame(root)
frame_tasks.pack()

scrollbar_tasks = tk.Scrollbar(frame_tasks)
scrollbar_tasks.pack(side=tk.RIGHT, fill=tk.Y)

listbox_tasks = tk.Listbox(frame_tasks, height=10, width=50, justify='center') # tkinter.Listbox(where it should go, height=x, width=xx)
listbox_tasks.pack()

listbox_tasks.config(yscrollcommand=scrollbar_tasks.set)
scrollbar_tasks.config(command=listbox_tasks.yview)

# Add task button
button_new_task = tk.Button(root, text='New task', width=42, command=new_task)
button_new_task.pack()

button_delete_task = tk.Button(root, text='Delete task', width=42, command=delete_task)
button_delete_task.pack()

button_save_tasks = tk.Button(root, text='Save tasks', width=42, command=save_tasks)
button_save_tasks.pack()

button_prioritize_tasks = tk.Button(root, text='Prioritize', width=42, command=prioritize_tasks)
button_prioritize_tasks.pack()

load_tasks()

root.mainloop() 

注意我有:

  • add_task() 函数移到 new_task() 函数中,以便不使用全局变量
  • 创建了缺失的 impact() 函数来为新任务分配随机影响值
  • 在函数中移动了已保存任务的加载 load_tasks()

更好的建议是使用 ttk.Treeview() 而不是 tk.Listbox(),因为您可以使用每个记录中的 tagstext 选项将影响值关联到任务Treeview.