文本列表到列表范围的tkinter问题

问题描述 投票:-1回答:1

我试图使用文本文件中的数据并将其放在列表中,并从列表中生成标签,当我运行代码时,我从文本中获取它我变成了这个错误我知道问题是因为我没有从原始文本文件中获得足够的值,例如我的文本文件中有30或20项,但我需要36个lebel

import os
import subprocess
import fileinput
import tkinter
from tkinter import *
import sys
import tkinter as tk


# Search an id and change it with a name and port
 master = Tk()
 master.geometry("300x300")




def change():
    SN = e1.get()
    NA = e2.get()

     with fileinput.FileInput("read.txt", inplace=True, backup='.bak') as 
file:
        for line in file:
            print(line.replace(str(SN), str(NA)), end='')

def creatswitch():
    BS = e3.get()
    with open("read.txt",'r+') as f:
        array = []
        te = str(BS)
        for line in f:
            if line.startswith(te):
                array.append(line.split("#")[0])


                for line in f:  # Continue iterating f for additional 
lines to keep
                if not line.rstrip():
                    break  # We hit an empty line, return to looking for 
switch
                    array.append(line.split("#")[0])




    Switch = tkinter.Tk()
    w = Label(Switch, text=str(array[1][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)

w.grid(row=0, column=1)
w = Label(Switch, text=str(array[3][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=2)
w = Label(Switch, text=str(array[5][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=3)
w = Label(Switch, text=str(array[7][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=4)
w = Label(Switch, text=str(array[9][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=5)
w = Label(Switch, text=str(array[11][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=6)
w = Label(Switch, text=str(array[13][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=7)
w = Label(Switch, text=str(array[15][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=8)
w = Label(Switch, text=str(array[17][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=9)
w = Label(Switch, text=str(array[19][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=10)
w = Label(Switch, text=str(array[21][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=11)
w = Label(Switch, text=str(array[23][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=12)
w = Label(Switch, text=str(array[25][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=13)
w = Label(Switch, text=str(array[27][7::]), bg="blue", fg="white")
w.config(height=3, width=30)
w.grid(row=0, column=14)
   w = Label(Switch, text=str(array[29][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=0, column=15)

   w = Label(Switch, text=str(array[31][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=0, column=16)

   w = Label(Switch, text=str(array[33][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=0, column=17)

   w = Label(Switch, text=str(array[35][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=0, column=18)
   w = Label(Switch, text=str(array[2][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=1)

   w = Label(Switch, text=str(array[4][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=2)

    w = Label(Switch, text=str(array[4][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=3)

   w = Label(Switch, text=str(array[8][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=4)

   w = Label(Switch, text=str(array[10][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=5)

   w = Label(Switch, text=str(array[12][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=6)

   w = Label(Switch, text=str(array[14][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=7)

   w = Label(Switch, text=str(array[16][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=8)

   w = Label(Switch, text=str(array[18][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
    w.grid(row=1, column=9)

   w = Label(Switch, text=str(array[20][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=10)

   w = Label(Switch, text=str(array[22][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=11)

   w = Label(Switch, text=str(array[24][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=12)

   w = Label(Switch, text=str(array[26][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=13)

   w = Label(Switch, text=str(array[28][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=14)

   w = Label(Switch, text=str(array[30][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=15)

   w = Label(Switch, text=str(array[32][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=16)

   w = Label(Switch, text=str(array[34][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=17)

   w = Label(Switch, text=str(array[36][7::]), bg="blue", fg="white")
   w.config(height=3, width=30)
   w.grid(row=1, column=18)





    # bb = Button(Switch, text=str(sa), command=addtolist, height=5, 
  width=10)
    # bb.pack(side=LEFT)

   Switch.mainloop()


















 Label(master, text="Find").grid(row=0)
 Label(master, text="Replace").grid(row=1)
 Label(master, text="Switch").grid(row=2)
 e1 = Entry(master)
 e2 = Entry(master)
 e3 = Entry(master)

 e1.grid(row=0, column=1)
 e2.grid(row=1, column=1)
 e3.grid(row=2, column=1)

 Button(master, text='Quit', command=master.quit).grid(row=5, column=0, 
 sticky=W, pady=5)
 Button(master, text='Replace', command=change).grid(row=5, column=1, 
 sticky=W, pady=5)

 Button(master, text='Build ', command=creatswitch).grid(row=5, column=2, 
 sticky=W, pady=5)











 mainloop()


enter code hereFile "G:/infiband/infiband.py", line 
117, in creatswitch
w = Label(Switch, text=str(array[33][7::]), bg="blue", 
fg="white")
IndexError: list index out of range
python-3.x string list tkinter
1个回答
1
投票

如果您需要36个标签,但您的数据只有20个值,那么这将是最简单的方法:

创建一个包含36个元素的列表。用你的20个元素填充列表,并用一些随机值完成剩下的值,用string =“None”来表示

#like your data from file would be
file_data = ["a", "b", "c", "d", "e"] #this list is not filled with 20 elem. for simplification
length_of_data = len(file_data)
array = []
for i in range(36):
    if i <= length_of_data:
        array[i] = file_data[i]
    else:
        array[i] = "None"

所以现在你的数组长36个元素。 array = [“a”,“b”,“c”,“d”,“e”,“None”,“None”,“None”,...]

为什么要对标签进行硬编码?更好的方法是在循环中创建标签,也许你会发现你不需要36个标签。如果您的数据长度为20个元素,则只创建20个标签。

list_of_labels = [] #for later needs
for i in range(len(array)):
    w = Label(Switch, text=str(array[i]), 
                bg="blue", fg="white", height=3, width=30)
    w.grid(row=0, column=i)
    list_of_labels.append(w)

现在您有36个标签,分为6行,而不是108行!

顺便说一下python列表索引从0开始而不是1。

© www.soinside.com 2019 - 2024. All rights reserved.