114
114

Reputation: 926

Troubleshooting A GUI in Tkinter for a Function

Consider:

from Tkinter import *

class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.entryLabel = Label(self, text="Please enter a list of numbers (no commas):")
        self.entryLabel.grid(row=0, column=0, columnspan=2)

        self.listEntry = Entry(self)
        self.listEntry.grid(row=0, column=2, sticky=E)

        self.entryLabel = Label(self, text="Please enter an index value:")
        self.entryLabel.grid(row=1, column=0, columnspan=2, sticky=E)

        self.indexEntry = Entry(self)
        self.indexEntry.grid(row=1, column=2)

        self.runBttn = Button(self, text="Run Function", command=self.psiFunction)
        self.runBttn.grid(row=2, column=0, sticky=W)

        self.answerLabel = Label(self, text="Output List:")
        self.answerLabel.grid(row=2, column=1, sticky=W)

This code builds a small user interface which is then used for the following function:

    def psiFunction(self):
        j = int(self.indexEntry.get())
        valueList = list(self.listEntry.get())
        x = map(int, valueList)
        if x[0] != 0:
            x.insert(0, 0)
        rtn = []
        for n2 in range(0, len(x) * j - 2):
            n = n2 / j
            r = n2 - n * j
            if  r == 0:
                rtn.append(j * x[n])
            else: 
                rtn.append(j * x[n] + r * (x[n + 1] - x[n]))
        self.answer = Label(self, text=rtn)
        self.answer.grid(row=2, column=2, sticky=W)


if __name__ == "__main__":
    root = Tk()
    app = App(root)
    root.mainloop()

For example if we take j = 3 and our list x = [0,1,2,3,4,5] we get [0,1,2,3.....,13,14,15] as our output list (though the program outputs it as a string). This is done by calculating values of our output list of length len(x) * j - 2 using terms from the input list for a fixed parameter j. Now, this example works fine with the above code. However, if I try to take, say, j=4 x = [0,1,2,3,4,5] nothing comes up - I get no output. I'm not given any error messages either so I'm not sure why exactly this issue arises. At first I thought it might have something to do with the list length being smaller than the j value (just taking a wild guess) but this example shows my idea was wrong. Does anyone know why this might be happening?

Note: The title refers to the fact that this does not occur when using the function from python, removed from the tkinter code.

EDIT: Changed code for definition

def psiFunction(self):
        j = int(self.indexEntry.get())
        valueList = list(self.listEntry.get())
        x = map(int, valueList)
        length = len(x)
        if x[0] != 0:
            x.insert(0, 0)
        rtn = []
        for n2 in range(0, length * j):
            n = n2 / j
            r = n2 - n * j
            if  r == 0:
                rtn.append(j * x[n])
            else: 
                rtn.append(j * x[n] + r * (x[n + 1] - x[n]))
        self.answer = Label(self, text=rtn)
        self.answer.grid(row=2, column=2, sticky=W)

Upvotes: 0

Views: 52

Answers (1)

zhangyangyu
zhangyangyu

Reputation: 8610

I don't know why there is no error in your computer but this part of your code do gives an error with x=[0,1,2,3,4,5] and j=4.

for n2 in range(0, len(x) * j - 2):
            n = n2 / j
            r = n2 - n * j
            if  r == 0:
                rtn.append(j * x[n])
            else: 
                rtn.append(j * x[n] + r * (x[n + 1] - x[n]))

Let's see, range(0, len(x) * j - 2) equals range(0, 22), so when n2 comes to 21, n = 5, j = 1. It will go into the else and there will be x[6], this will throw an indexerror, and you can not get the desired output.

Upvotes: 1

Related Questions