0

The following code is my first attempt at a Tkinter program- please go easy. Upon running the code it is meant to be like a Reminders app. I'm having trouble with it however, upon running the program a windows is generated with the option to load from the accompanying TXT file (I've used them before).

After clicking on the button, more buttons are meant to be loaded. These buttons do load but they instead each contain the same value (despite the item loop assigning them each different values)- and they pass this value to the described function. They are meant to have their own value, corresponding to the amount of 'tasks' or rather spaces in the 'cache' that exist eg; each task has it's own unique value.

Button command is surrounded by asterix.

`

import tkinter
from tkinter import *
from tkinter import ttk

exit_command = False
version = 0.1
taskstats = []
lineread = 'a'
line = []
cache = []
buttonlist = []
taskpadtop = 50
windowwidth = 200
windowheight = 400
window3 = 10
window4 = 10
counter_ID = 0
save_loaded = False


#
# def add_at_end():
#     with open('tasker.txt', 'w') as file:
#         file.write('TEST')
#         file.close()

def tick_task(number):
    button_name = buttonlist[number]
    button_name.configure(text='clicked')


def load_file():
    global counter_ID
    if len(cache) == 0:
        with open('tasker.txt', 'r') as file:
            save_loaded = True
            alllines = file.readlines()
            for item in alllines:
                evtit, evdisc, evstat = item.split()
                finalline = [evtit, evdisc, evstat]
                cache.append(finalline)

            file.close()

    else:

        print('Error, already loaded savedata')
        save_loaded = False

    if save_loaded:
        # Each 'item' will contain a task, of course.

        for item in range(0, len(cache)):
            counter_ID = counter_ID + 1
            # So yes, stick a label for each one!
            txt = Label(text=cache[item][0])
            # Place that label in X=0, and then space them by 100 pixels
            txt.place(x=0, y=(item * 50) + taskpadtop)
**
confirm = Button(window, text='Task {} Complete'.format(item + 1), padx=30, command=lambda: tick_task(item))****
            confirm.grid(column=3)
            buttonlist.append(confirm)
            print(buttonlist)
            # tickvalue=IntVar()
            # cache[2].append(tickvalue)
            # taskstatus = Checkbutton(window, name=str(item),variable=tickvalue)
            # print(tickvalue)
            # # command = lambda: task_change(counter_ID)
            # taskstatus.place(x=30, y=(item * 50) + taskpadtop)


window = Tk()
# window.grid()
# window.columnconfigure(3)

btn = Radiobutton(window, text="Load from internal TXT!", fg='blue', command=load_file)
btn.grid(column=0)
btn.place(x=0, y=0)

window.title('Tasker {}'.format(version))
window.geometry("{}x{}+{}+{}".format(windowwidth, windowheight, window3, window4))

window.mainloop()

`

I was expecting that the buttons would each individually pass their own unique value (numeric from 0 upwards) to the function in tkinters command argument.

I have looked at tutorials, using tkinters own button naming scheme and appending to a list etc. None have worked...

06Days
  • 1

0 Answers0