程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Python Tkinter:为列表框中的每个字符串创建一个变量?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

开发过程中遇到Python Tkinter:为列表框中的每个字符串创建一个变量?的问题如何解决?下面主要结合日常开发的经验,给出你关于Python Tkinter:为列表框中的每个字符串创建一个变量?的解决方法建议,希望对你解决Python Tkinter:为列表框中的每个字符串创建一个变量?有所启发或帮助;

我(几乎)是一个糟糕的程序员处女,所以请对我放轻松。@H_616_3@

这是我第二次尝试制作程序,但结果证明这有点超出我的能力范围,恐怕。在尝试解决这个问题很长时间后,我请求您的帮助。@H_616_3@

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

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

我会尽力向您展示:@H_616_3@

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

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

然后我从下拉菜单中指定一个数字。我希望这个数字是上一步中变量/字符串的值。@H_616_3@

@L_419_1@@H_616_3@

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

相关代码在这里:@H_616_3@

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,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',command=new_task)
button_new_task.pack()


button_delete_task = tkinter.button(root,text='delete task',command=delete_task)
button_delete_task.pack()


button_save_tasks = tkinter.button(root,text='Save tasks',command=save_tasks)
button_save_tasks.pack()

button_prioritize_tasks = tkinter.button(root,text='Prioritize',command=prioritize_tasks)
button_prioritize_tasks.pack()

root.mainloop() 

解决方法

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

以下是修改后代码:@H_616_3@

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,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,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',command=new_task)
button_new_task.pack()

button_delete_task = tk.button(root,text='delete task',command=delete_task)
button_delete_task.pack()

button_save_tasks = tk.button(root,text='Save tasks',command=save_tasks)
button_save_tasks.pack()

button_prioritize_tasks = tk.button(root,text='Prioritize',command=prioritize_tasks)
button_prioritize_tasks.pack()

load_tasks()

root.mainloop() 

请注意,我有:@H_616_3@

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

更好的建议是使用 ttk.Treeview() 而不是 tk.Listbox(),因为您可以使用 tagstext 选项将影响值与任务相关联{{ 1}}。@H_616_3@

大佬总结

以上是大佬教程为你收集整理的Python Tkinter:为列表框中的每个字符串创建一个变量?全部内容,希望文章能够帮你解决Python Tkinter:为列表框中的每个字符串创建一个变量?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。