[英]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). 例如,如果我们取j = 3并将列表x = [0,1,2,3,4,5]设为[0,1,2,3 .....,13,14,15]输出列表(尽管程序将其输出为字符串)。 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.
这是通过使用固定参数j的输入列表中的项计算长度为len(x)* j-2的输出列表的值来完成的。 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.
但是,如果我尝试取j = 4 x = [0,1,2,3,4,5],则什么都不会出现-我没有输出。 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.
起初,我认为这可能与列表长度小于j值有关(只是大胆地猜测),但此示例表明我的想法是错误的。 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. 注意:标题是指从tkinter代码中删除时,使用python中的函数时不会发生这种情况。
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)
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
. 我不知道为什么您的计算机没有错误,但是您的代码的这一部分确实给出了
x=[0,1,2,3,4,5]
和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
. 我们来看一下,
range(0, len(x) * j - 2)
等于range(0, 22)
,所以当n2
等于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. 它将进入else并存在
x[6]
,这将引发indexerror,并且您将无法获得所需的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.