1

I am trying to make this p = [0,25,43.3013,50,43.3013,25,0,0,0,0,0] assigning only for the position 1,2,3,4,5 and making others zero. And I got this error:

Exception in Tkinter callback

Traceback (most recent call last):

File "C:\Users\sel\Anaconda3\lib\tkinter__init__.py", line 1705, in call return self.func(*args)

File "", line 28, in open_window1

p[1]=25

IndexError: list assignment index out of range

How can this be fixed?

import numpy as np
import xlsxwriter
import tkinter as tk
from tkinter import *

import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure


window=Tk()


def open_window1():
    y = 1/2
    b = 1/4

    result = []
    u = []
    v = []

    t = []
    #p = [0,25,43.3013,50,43.3013,25,0,0,0,0,0]

    p = []

    p[1]=25
    p[2]=43.3013
    p[3]=50
    p[4] = 43.3013
    p[5] = 25

    a = []
    pn = []
    pn.append(0)

    m = 0.4559/100000
    k = 18
    c = 0.2865
    u.append(0)
    v.append(0)
    a.append((p[0]-c*v[0]-k*u[0])/m)

    dt = 0.1
    Tn = 1 

    st = Tn/dt + 1

    step = int(st)

    x = 0.0
    for i in range(step):
        z = x
        t.append("%.2f"% z)
        x = x + dt


    a1 =(m/(b*dt*dt)+y*c/(b*dt))
    a2 = (m/(b*dt)+(y/b-1)*c)
    a3 = (((1/(2*b))-1)*m + dt*((y/(2*b))-1)*c)
    kn = k + a1


    for i in range(step):
        while(i<1 and i>5):
            p[i] = 0


    for i in range(step-1):
        pn.append(np.around(p[i+1]+ a1*u[i] + a2*v[i] + a3*a[i], decimals=4))
        u.append(np.around(pn[i+1]/kn, decimals=4))
        v.append(np.around(y*(u[i+1]-u[i])/(b*dt) + (1-y/b)*v[i] + dt* (1-y/(2*b))*a[i], decimals=4))
        a.append(np.around((u[i+1]-u[i])/(b*dt*dt) - v[i]/(b*dt)-(1/(2*b)-1)*a[i], decimals=4))

    top = tk.Toplevel()
    top.wm_geometry("1200x540")
    top.title('Result table')

    canvas = tk.Canvas(top,bg="white", width=1195, height=540)
    canvas.grid(row=0,column=180)

    for i in range(0,len(t)):

        b6 = tk.Label(canvas, text=t[i], font= "calibri 12", bg="white")
        canvas.create_window(3,40*(i+1), window=b6, anchor=tk.NW)

    for i in range(0,len(t)):

        b6 = tk.Label(canvas, text=p[i], font= "calibri 12", bg="white")
        canvas.create_window(100,40*(i+1), window=b6, anchor=tk.NW)            

    for i in range(0,len(t)):

        b6 = tk.Label(canvas, text=pn[i], font= "calibri 12", bg="white")
        canvas.create_window(200,40*(i+1), window=b6, anchor=tk.NW)         

    for i in range(0,len(t)):

        b6 = tk.Label(canvas, text=a[i], font= "calibri 12", bg="white")
        canvas.create_window(300,40*(i+1), window=b6, anchor=tk.NW)     


    for i in range(0,len(t)):

        b6 = tk.Label(canvas, text=v[i], font= "calibri 12", bg="white")
        canvas.create_window(400,40*(i+1), window=b6, anchor=tk.NW)   

    for i in range(0,len(t)):

        b6 = tk.Label(canvas, text=u[i], font= "calibri 12", bg="white")
        canvas.create_window(500,40*(i+1), window=b6, anchor=tk.NW)   


b1=Button(window, text="Cons. Avg.", width=12, command=open_window1)
b1.grid(row=6,column=0)

window.title("Fill in the blanks")
window.geometry("350x133")
window.mainloop()
1
  • 1
    Just because you are using Tkinter doesn't mean your problem has anything to do with Tkinter. Also, you should make sure you understand these kinds of things - and much more - before you try to work with Tkinter; learn to walk before running. Commented Oct 6, 2019 at 6:29

1 Answer 1

2
p = []

p[1]=25
p[2]=43.3013
p[3]=50
p[4] = 43.3013
p[5] = 25

This is not allowed in Python; you cannot assign to list indices that aren't already present. (Also, the first index would be 0, not 1.) You made p an empty list, and thus none of these assignments are allowed.

To append a value to the end of a list, regardless of how many elements it already contains, use the .append() method.

But it would have been better to just use the commented-out version in the first place: p = [0,25,43.3013,50,43.3013,25,0,0,0,0,0].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.