正在获取错误:IndexError:列表索引超出范围'

时间:2019-04-16 10:29:59

标签: python tkinter tkinter-canvas

我正在尝试为tkinter编码。这是代码。这是可验证的。您可以输入任何数字来验证这一点。

import tkinter as tk


rectangle_values = []
rectangle_number = []
temp_recta = []
temp_rectn = []
rcta = []
rctn = []
area_var = 0
minw = 0
maxw = 0
minl = 0
maxl = 0
are_a = 0
comp = 0
h = 0

def open_window(rect, mnw,mxw,mnl,mxl):
    window.title('Rectangles')
    global minw
    global maxw
    global minl
    global maxl
    global are_a
    global comp
    global h
    global rectangle_values
    global rectangle_number



    minw = mnw
    maxw = mxw
    minl = mnl
    maxl = mxl
    comp = rect
    x, y, z, j = 400, 405, 405, 1

    l9 = tk.Label(canvas, text="각 Component 입력 정보", font= "calibri 14 bold", bg="white")
    canvas.create_window(30,345, window=l9, anchor=tk.NW)

    l10 = tk.Label(canvas, text="CW", font= "calibri 14", bg="white")
    canvas.create_window(200,370, window=l10, anchor=tk.NW)

    l11 = tk.Label(canvas, text="CH", font= "calibri 14", bg="white")
    canvas.create_window(335,370, window=l11, anchor=tk.NW)    

    l12 = tk.Label(canvas, text="공급사 모재 면적 정보", font= "calibri 14 bold", bg="white")
    canvas.create_window(460,350, window=l12, anchor=tk.NW) 

    l12 = tk.Label(canvas, text="Area", font= "calibri 14", bg="white")
    canvas.create_window(470,380, window=l12, anchor=tk.NW)

    poscoimg3 = tk.PhotoImage(file="C:/Users/sel/Desktop/poscoimg3.png")
    canvas.create_image(700,360, image=poscoimg3, anchor=tk.NW)
    canvas.poscoimg3 = poscoimg3

    area_var = tk.DoubleVar()
    entry_area_number = tk.Entry(canvas, textvariable=area_var)
    canvas.create_window(590,395, window=entry_area_number)





    for i in range(0, rect * 2):         
        if (i % 2) == 0: 
            l1 = tk.Label(canvas, text="Component %d" %(j), font= "calibri 13", bg="white")
            canvas.create_window(30, x, window=l1, anchor=tk.NW)
            rectangle_values.append(tk.StringVar())
            j +=1
            x +=30

            en = tk.Entry(canvas, textvariable=rectangle_values[i])
            canvas.create_window(150, y, window=en, anchor=tk.NW)
            y +=30

        if (i % 2) != 0:
            rectangle_values.append(tk.StringVar())            
            en = tk.Entry(canvas, textvariable=rectangle_values[i])
            canvas.create_window(285, z, window=en, anchor=tk.NW)
            z +=30
    h=y       

    #for j in range(0, rect):
        #rectangle_number.append(tk.StringVar())            
        #en = tk.Entry(canvas, textvariable=rectangle_number[j])
        #canvas.create_window(400, f, window=en, anchor=tk.NW)
        #f +=20

    proceed_button = tk.Button(canvas, text="Run",font= "calibri 13", command=lambda: first_step(area_var.get()))
    canvas.create_window(470, 420, window=proceed_button, anchor=tk.NW)
    canvas.configure(scrollregion=canvas.bbox("all"))


def first_step(e):
    global temp_recta
    global temp_rectn
    global rcta
    global rctn
    global comp
    global are_a

    are_a = e

    for r in range(0, comp * 2):
        temp_recta.append(rectangle_values[r].get())
    for i in range(len(temp_recta)):
        t = int(temp_recta[i])
        rcta.append(t)
    for r in range(0, comp):
        temp_rectn.append(rectangle_number[r].get())
    for i in range(len(temp_rectn)):
        t = int(temp_rectn[i])
        rctn.append(t)  
    cal_culate()


window = tk.Tk()
window.configure(background='white')



ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
w = 980  # width for the Tk root
h = 600  # height for the Tk root
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)



window.geometry('%dx%d+%d+%d' % (w, h, x, y))
canvas = tk.Canvas(window, bg="white", width=980, height=580, highlightthickness=0)
canvas.pack()
canvas_scroll = tk.Scrollbar(canvas, command=canvas.yview)
canvas_scroll.place(relx=1, rely=0, relheight=1, anchor=tk.NE)
canvas.configure(yscrollcommand=canvas_scroll.set, scrollregion=())



l0 = tk.Label(canvas, text="강재 NESTING 최적화 프로그램", font= "calibri 32 bold",fg="royalblue4",bg="white" )
canvas.create_window(200,70, window=l0, anchor=tk.NW)

l00 = tk.Label(canvas, text="제조사 시트 입력 정보", font= "calibri 14 bold", bg="white" )
canvas.create_window(30,155, window=l00, anchor=tk.NW)

photo = tk.PhotoImage(file="C:/Users/sel/Desktop/posco.png")
canvas.create_image(800,10, image=photo, anchor=tk.NW)


line = tk.PhotoImage(file="C:/Users/sel/Desktop/line.png")
canvas.create_image(0,130, image=line, anchor=tk.NW)


poscoimg1 = tk.PhotoImage(file="C:/Users/sel/Desktop/poscoimg1.png")
canvas.create_image(500,180, image=poscoimg1, anchor=tk.NW)

poscoimg2 = tk.PhotoImage(file="C:/Users/sel/Desktop/poscoimg2.png")
canvas.create_image(0,340, image=poscoimg2, anchor=tk.NW)

l1 = tk.Label(canvas, text="MIN", font= "calibri 14", bg="white")
canvas.create_window(210,180, window=l1, anchor=tk.NW)


l2 = tk.Label(canvas, text="MAX", font= "calibri 14", bg="white")
canvas.create_window(340,180, window=l2, anchor=tk.NW)


l3 = tk.Label(canvas, text="Sheet 폭(SW)", font= "calibri 13", bg="white")
canvas.create_window(33,200, window=l3, anchor=tk.NW)

minw_var = tk.DoubleVar()
entry_minw_number = tk.Entry(canvas, textvariable=minw_var)
canvas.create_window(220,215, window=entry_minw_number)

maxw_var = tk.DoubleVar()
entry_maxw_number = tk.Entry(canvas, textvariable=maxw_var)
canvas.create_window(355,215, window=entry_maxw_number)

l4 = tk.Label(canvas, text="Sheet 높이(SH)", font= "calibri 13", bg="white")
canvas.create_window(33,230, window=l4, anchor=tk.NW)

minl_var = tk.DoubleVar()
entry_minl_number = tk.Entry(canvas, textvariable=minl_var)
canvas.create_window(220,240, window=entry_minl_number)

maxl_var = tk.DoubleVar()
entry_maxl_number = tk.Entry(canvas, textvariable=maxl_var)
canvas.create_window(355,240, window=entry_maxl_number)

l5 = tk.Label(canvas, text="소요 Component 개수", font= "calibri 13", bg="white")
canvas.create_window(33,260, window=l5, anchor=tk.NW)

rect_var = tk.IntVar()
entry_rect_number = tk.Entry(canvas, textvariable=rect_var)
canvas.create_window(290,270, window=entry_rect_number)

submit_button = tk.Button(canvas, text="Submit",font= "calibri 13", command=lambda: open_window(rect_var.get(),\
                minw_var.get(), maxw_var.get(),minl_var.get(),maxl_var.get()))
canvas.create_window(200, 290, window=submit_button, anchor=tk.NW)
canvas.bind_all('<MouseWheel>', lambda event: canvas.yview_scroll(int(-1*(event.delta/120)), "units"))

def cal_culate():
    print(minw,maxw,minl,maxl,comp,are_a)

window.title("Rectangle Configuration")
window.mainloop()

我只是尝试打印输入的数字,以检查代码是否运行良好。但是我遇到了这个错误。

错误

  

Tkinter回调中的异常

     

回溯(最近通话最近一次):

     

> strong>呼叫

中的文件“ C:\ Users \ sel \ Anaconda3 \ lib \ tkinter__init __。py”,行1705      

返回self.func(* args)

     

文件“”,第92行,在     前进按钮= tk.Button(画布,文字=“运行”,字体=“ calibri 13”,>命令= lambda:first_step(area_var.get()))

     

文件“”的第113行,在first_step中      temp_rectn.append(rectangle_number [r] .get())

     

IndexError:列表索引超出范围”

我不知道我哪里出了错。

1 个答案:

答案 0 :(得分:2)

int()无法将空字符串转换为整数,您想要的是检查temp_recta [i]实际上是否为非空字符串,然后尝试对其进行解析。如果它是一个空字符串,则可以将其解析为0(这部分很大程度上取决于您的业务逻辑)

>>> s = ''
>>> i = int(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>> i = int(s) if s else 0

在上面的示例中,如果s为'30',我将为30,如果它为'(空白字符串),我将为0