0

ERORR:

Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Users\DuckSide\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
        return self.func(*args)
      File "F:\importantfiles\files\PythonTask\Test\exercise_2_Test2\exercise_2_Test2.py", line 84, in Test
        if words[1] != '':
    IndexError: list index out of range

MyCode

from tkinter import *
from random import randint
from tkinter import ttk
import tkinter as tk
import sqlite3

conn = sqlite3.connect('database.db')
cur = conn.cursor()
root = tk.Tk()
root.title('exercise_2')
root.geometry('245x300')

v = StringVar()
b = StringVar()
v.set('')
b.set('')

info = cur.execute('SELECT * FROM data WHERE name=?', ('Ivanov',))
if info.fetchone() is None:
    cur.execute("INSERT INTO data VALUES (?, ?);", (70121903, 'Ivanov'))
    conn.commit()
    v.set('Ivanov')
    b.set('70121903')
else:
    v.set('Ivanov')
    b.set('70121903')




tree= ttk.Treeview( root,column=("column0" ,"column1"))

tree.heading("#0" , text="Сумма")
tree.column("#0",minwidth=0,width=160)
tree.heading("#1" , text="Имя")
tree.column("#0",minwidth=0,width=80)
tree.place(x = 0, y = 83)
tree.place(width = 245,height = 220)

def Test():
    text = str(Entry1.get())
    words = text.split()
    if words[0] == 'DEPOSIT':
        conn = sqlite3.connect('database.db')
        cur = conn.cursor()

        info = cur.execute('SELECT * FROM data WHERE name=?', (words[1],))
        if info.fetchone() is None:
            cur.execute("INSERT INTO data VALUES (?, ?);", (words[2], words[1]))
            conn.commit()
            v.set(words[1])
            b.set(words[2])
        else:
            cur.execute("SELECT id FROM data WHERE name = ?", (words[1],))
            result = cur.fetchone()
            cur.execute("UPDATE data SET id = ? WHERE name = ?", (int(result[0]) + int(words[2]), words[1]))
            conn.commit()
            cur.execute("SELECT id FROM data WHERE name = ?", (words[1],))
            result = cur.fetchone()
            v.set(words[1])
            b.set(result)

    elif words[0] == 'WITHDRAW':
        conn = sqlite3.connect('database.db')
        cur = conn.cursor()

        info = cur.execute('SELECT * FROM data WHERE name=?', (words[1],))
        if info.fetchone() is None:
            cur.execute("INSERT INTO data VALUES (?, ?);", (int(words[2]) * -1, words[1]))
            conn.commit()
            v.set(words[1])
            b.set(words[2])
        else:
            cur.execute("SELECT id FROM data WHERE name = ?", (words[1],))
            result = cur.fetchone()
            cur.execute("UPDATE data SET id = ? WHERE name = ?", (int(result[0]) - int(words[2]), words[1]))
            conn.commit()
            cur.execute("SELECT id FROM data WHERE name = ?", (words[1],))
            result = cur.fetchone()
            v.set(words[1])
            b.set(result)
    elif words[0] == 'CLEAR':
        tree.delete(*tree.get_children())

    elif words[0] == 'BALANCE':
        if words[1] == '':
            conn = sqlite3.connect("database.db")
            cur = conn.cursor()
            cur.execute("SELECT * FROM data")
            rows = cur.fetchall()
            for row in rows:
                tree.insert("", tk.END, text=row[0], values=row[1:])
            conn.close()
        else:
            conn = sqlite3.connect("database.db")
            cur = conn.cursor()
            cur.execute("SELECT * FROM data")
            rows = cur.fetchall()
            for row in rows:
                if words[1] == row[1]:
                    tree.insert("", tk.END, text=row[0], values=row[1:])
                conn.close()









Label1 = Label(textvariable=v).place(x=0, y=0)
Label2 = Label(textvariable=b).place(x=0, y=18)

Entry1 = Entry(width = 40)
Entry1.place(x=0, y=40)
Entry1.insert(0, '')

button1 = Button(bd = 1,width = 34 ,text="Calculate",command=Test).place(x=0, y=60)
root.mainloop()

The first condition is processed and works, and on the second it gives an error

furas
  • 134,197
  • 12
  • 106
  • 148
DuckSide
  • 1
  • 1

0 Answers0