【发布时间】:2021-04-27 18:17:13
【问题描述】:
我(几乎)是一个可怜的程序员处女,所以请放轻松。
这是我第二次尝试制作程序,但结果证明这有点超出我的导航能力,恐怕。经过长时间的尝试解决这个问题,我请求您的帮助。
我基本上是在制作一个待办事项列表,但希望它具有更多的功能,而不仅仅是一个无聊的列表。
我在脑海中的想象是用户将任务添加到条目小部件中,然后将显示在列表框中。然后,列表框中的每个字符串都会有一个与之关联的值(我需要对我希望程序具有的功能进行一些计算)。 所以我想我想要的是列表框中的每个字符串都成为一个变量,然后与我想要一个值的那个变量相关联。
我会试着给你看:
这里我添加了我想成为新变量的字符串
然后我从下拉菜单中指定一个数字。我希望这个数字是上一步中变量/字符串的值。
我真的希望你们中的某个人能够以一种(最好)不需要我做太多改变的方式带领我朝着正确的方向前进。事情对我来说仍然很棘手,我已经很难浏览代码。 目的很简单,我想对与每个任务相关的(希望)即将成为的值进行一些计算。 如果有人敢,请提前感谢!
相关代码在这里:
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()
【问题讨论】:
-
标题不错 ;)
-
你没有提到问题。
-
我相信我做到了。问题是我不知道如何使列表框中的字符串成为具有各自值的变量 - 基于下拉列表中的字符串/整数的值。
-
@KarimLoberg 我有点困惑,但您可以尝试使用字典。所以基本上字典中的字符串是键,值是键的值
标签: python-3.x string variables tkinter listbox