Issue
I get error like this in the title when I run this code. I want to make TODO list but I'm getting many errors on my way :(
. I can't call array (tab
) in other function and I can't change column in checkbox grid
.
from tkinter import *
from tkinter import messagebox as msb
win = Tk()
win.title("Checkbutton")
win.geometry("300x600")
win.resizable("False","False")
task_name = StringVar()
tab = []
tab.append("")
tab.append("")
r = 2
c = 1
cc = IntVar
def checking():
tab[r].grid(row=r, column=1, padx=(10, 10), pady=(5, 5))
def addcheck():
global r
global c
global cc
if r < 16:
tab.append(Checkbutton(variable = cc, text = task_name.get(), command = checking))
tab[r].grid(row=r, column=0, padx=(10, 10), pady=(5, 5))
r = r + 1
task_entry.delete(0, 20)
else:
msb.showwarning(title="DANGER", message="TOO MUCH TASKS, TAKE A REST")
text1 = Label(win, text = "TO DO:").grid(row = 1, column = 0, padx = (10, 10), pady = (30, 10))
text2 = Label(win, text = "DONE: ").grid(row = 1, column = 1, padx = (10, 10), pady = (30, 10))
task_entry = Entry(width = "20", textvariable = task_name)
task_entry.grid(row = 0, column = 0, padx = (30, 10))
add = Button(win, text = "add task", command = addcheck).grid(row = 0, column = 1, padx = (10, 10))
mainloop()
Full error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\matib\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "D:\pythonprojekty\pythonProject\main.py", line 18, in checking
tab[r].grid(row=r, column=1, padx=(10, 10), pady=(5, 5))
IndexError: list index out of range
Solution
You're getting the IndexError
because of trying to use the global variable r
in the checking()
function — and the way to fix it is by explicitly passing the row
value as an argument when calling it. In the code below this is being done by using a lambda
expression to define the callback command=
function.
There were a number of other mistakes in your code, and I have attempted to correct them all and reformat the code to follow the PEP 8 - Style Guide for Python Code recommendations for readability.
import tkinter as tk
from tkinter import messagebox as msb
from tkinter.constants import *
win = tk.Tk()
win.title("Checkbutton")
win.geometry("300x600")
win.resizable(FALSE, FALSE)
task_name = tk.StringVar()
tab = [None, None]
r = len(tab)
def checking(row):
tab[row].grid(row=row, column=1, padx=(10, 10), pady=(5, 5))
def addcheck():
global r
if r > 16:
msb.showwarning(title="DANGER", message="TOO MANY TASKS, TAKE A REST")
else:
cb_var = tk.IntVar()
tab.append(tk.Checkbutton(variable=cb_var, text=task_name.get(),
command=lambda row=r: checking(row)))
tab[r].grid(row=r, column=0, padx=(10, 10), pady=(5, 5))
r += 1
task_entry.delete(0, END)
tk.Label(win, text="TO DO:").grid(row=1, column=0, padx=(10, 10), pady=(30, 10))
tk.Label(win, text="DONE: ").grid(row=1, column=1, padx=(10, 10), pady=(30, 10))
task_entry = tk.Entry(width="20", textvariable=task_name)
task_entry.grid(row=0, column=0, padx=(30, 10))
tk.Button(win, text="add task", command=addcheck).grid(row=0, column=1, padx=(10, 10))
task_entry.focus_set()
win.mainloop()
Here's a screenshot of it running on my system:
Answered By - martineau
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.