我(几乎)是一个可怜的程序员处女,所以请对我宽容点。
这是我第二次尝试制作程序,恐怕这超出了我的能力范围。经过很长时间的尝试解决这个问题,我请求您的帮助。
我基本上是在制作一个待办事项列表,但希望它有更多的功能,而不仅仅是一个无聊的列表。
我在脑海中的想象是,用户将任务添加到条目小部件中,然后该任务将显示在列表框中。列表框中的每个字符串都会有一个与之关联的值(我需要对我希望程序具有的功能进行一些计算)。 所以我想我想要的是列表框中的每个字符串都成为一个变量,然后与我想要一个值的该变量相关联。
我会尽力向您展示:
这里我添加我想要成为新变量的字符串
然后我从下拉菜单中指定一个数字。我希望这个数字是上一步中变量/字符串的值。
我真的希望你们中的一个人能够以一种(最好)不需要我做太多改变的方式引导我走向正确的方向。对我来说,事情仍然很棘手,浏览代码对我来说已经相当困难了。 目的很简单,我想对与每个任务相关的(希望)即将出现的值进行一些计算。 如果你们敢的话,先谢谢了!
相关代码在这里:
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()
,因为您可以使用 tags
中每条记录的 text
或 Treeview
选项将影响值与任务关联起来。
将 tkinter 导入为 tk
def add_task(): 任务=入口.get() 如果任务: listbox.insert(tk.END, 任务) 条目.删除(0,tk.END)
defdelete_task(): 尝试: selected_task_index = listbox.curselection()[0] listbox.delete(selected_task_index) 除了索引错误: 通过
root = tk.Tk() root.title("我的名字")
entry = tk.Entry(根, 宽度=50) 入口.pack(pady=10)
add_button = tk.Button(root, text="??????", command=add_task) add_button.pack(pady=5)
delete_button = tk.Button(root, text="ÍÍف هмÉ", command=delete_task) 删除按钮.pack(pady=5)
listbox = tk.Listbox(根, 宽度=50, 高度=10) listbox.pack(pady=10)
root.mainloop()