【发布时间】:2016-05-09 15:23:16
【问题描述】:
谁能告诉我我做错了什么?!我在 Python 3 中使用 tkinter 制作了一个 GUI。我正在尝试创建数组并在窗口打开时将其显示到列表框。您可以在下面看到我使用的代码。
错误:
Population.X[i] = float(random.random()) * self.XMin
IndexError: list assignment index out of range
代码:
class Population:
X = []
Y = []
class Application(Frame):
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.widgets()
self.create_array()
def widgets(self):
self.first_listbox = Listbox(self)
self.first_listbox.grid()
def create_array(self):
i = 0
while i < 20:
Population.X[i] = float(random.random()) * self.XMin
# x = random.random() - Random float x | 0.0 <= x < 1.0 |
if Population.X[i] == 0:
Population.X[i] = -0.1
Population.Y[i] = 1 / Population.X[i]
i += 1
while i < 20:
self.first_listbox.insert(i, Population.X[i])
i += 1
root = Tk()
root.geometry("600x400")
app = Application(root)
root.mainloop()
【问题讨论】:
标签: python arrays python-3.x tkinter listbox