0

This is one my very first python programs, and my very first Tkinter program, so it is just a test.

What I am hoping to accomplish is getting an array (a[]) to store a number from dataA through the variable g at the location a[f].

Then I want to be able to choose which two locations (a[b] and a[c]) that get added up to the variable d, which will be displayed on the widget "prompt".

from tkinter import *
from tkcalendar import *
from array import *
root = Tk()
i = 0



def getDateA():
    a = array('b',[]*10000)
    global i
    f = i + 1
    c = int(dataC.get())
    b = int(dataB.get())
    g = dataA.get()
    a.append(g[f])
    d = int(a[b]) + int(a[c])
    
    prompt.configure(text = d)
    
    
dataA = Entry(root, text="Enter a", width = 20)
dataA.grid(column = 2)

dataB = Entry(root, text="Enter b", width = 20)
dataB.grid(column = 3)

dataC = Entry(root, text="Enter c", width = 20)
dataC.grid(column = 4)

my_button = Button(root, text="get date", command=getDateA)
my_button.grid(column = 2)

prompt = Label(root, text="Ingenting")
prompt.grid(column=1)
root.mainloop()
 

At first, the array wasn't long enough, so I added append, but I still got the error message (at the bottom of this post).

Then I initialized and - hopefully - added 10000 empty indexes. Still, I get the following error message:

Exception in Tkinter callback Traceback (most recent call last):
File "C:\Python38\lib\tkinter_init_.py", line 1883, in call return self.func(*args) File "Linear algebra.py", line 16, in getDateA a.append(g[f]) IndexError: string index out of range

Why is that?

2
  • What have you done to debug this? Have you examined g immediately before the error to see if it is what you expect it is? Same thing for f: is it what you're assuming it is? Commented Sep 26, 2020 at 22:30
  • []*10000 gives you 10000 repetitions of an empty list, which is still an empty list. You could try [0]*10000 or (0 for x in range(10000)) or itertools.repeat(0, 10000). The latter two use less memory but the range one may be slower. See here for something similar: stackoverflow.com/a/35481867/3830997 Commented Sep 27, 2020 at 3:29

1 Answer 1

0

I'm not sure what the purpose of this script is, but here are the changes to have it do something:

  • Make the array global so the array state is retained between updates
  • Initialize the array using [0], not []. The empty list does nothing.
  • g needs to be an int, not a string
  • Use a[i] to set the value instead of append
  • Increment i on each click to set the next value in the array

Here's the updated code:

from tkinter import *
from tkcalendar import *
from array import *
root = Tk()
i = 0
a = array('b',[0]*1000)  # global array, initialize all 0

def getDateA():
    global i,a
    f = i + 1
    c = int(dataC.get())  # convert all to ints
    b = int(dataB.get())
    g = int(dataA.get())
    a[i] = g  # set array element
    d = int(a[b]) + int(a[c])
    
    prompt.configure(text = d)
    i += 1  # update next spot in array
    
    
dataA = Entry(root, text="Enter a", width = 20)
dataA.grid(column = 2)

dataB = Entry(root, text="Enter b", width = 20)
dataB.grid(column = 3)

dataC = Entry(root, text="Enter c", width = 20)
dataC.grid(column = 4)

my_button = Button(root, text="get date", command=getDateA)
my_button.grid(column = 2)

prompt = Label(root, text="Ingenting")
prompt.grid(column=1)
root.mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the help Mike, it did indeed work. But I have a question, why should I not use append? I would like to be able to have an index-printing prompt, to help keep track of all the values. But if I initialize 10000 empty spaces, this prompt would be infinitely long.
You can use append if you prefer, just start with a zero length array. This may help with the index prompt also.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.