python でCSVファイルを読み込んで 画面上に表示したいが 最後の単語だけしか表示されない

python

1import tkinter as tk 2from tkinter import filedialog 3import tkinter.font 4import csv 5import openpyxl 6import tkinter.font 7import time 8 9class Flash:10 def __init__(self, parent, wpm, lst ,lbl61, lbl71):11 # variable storing time12 self.count = 013 # label displaying words14 self.wpm = wpm 15 self.lst = lst 16 17 self.wpm_interval = int(60/self.wpm * 1000)18 self.lbl61 = lbl61 19 self.lbl71 = lbl71 20 21 # print(self.count)22 # print(self.wpm)23 # print(self.wpm_interval)24 # print(self.lst)25 26 def display_next_word(self):27 if self.count > 5:28 return29 30 self.lbl61.configure(text="%s" % self.lst[self.count][3])31 self.lbl71.configure(text="%s" % self.lst[self.count][6])32 self.count += 133 print("af: " + str(self.count) + " : " + str(self.wpm_interval))34 root.after(self.wpm_interval, self.display_next_word())35 36 37# py ファイルを実行したら 以下のif文を実行する.モジュールとしてimportしたら実行されない。38if __name__ == "__main__": 39 40 #Root設定41 root = tk.Tk()42 root.geometry("700x600+700+300")43 root.title("速習英単語")44 45 #フォント設定46 font = tkinter.font.Font(47 root,48 family="Times",49 size=2050 )51 52 #CSV読込関数53 # 格納するための配列を準備54 apps = []55 def openFile():56 apps.clear()57 58 filename = filedialog.askopenfilename(initialdir="/",filetypes=(("exexutables", "*csv"), ("all files", "*.*")))59 apps.append(filename)60 for fn in apps:61 applabel = tk.Label(frame4, text= fn, width="20", font=font)62 applabel.pack(side=tk.LEFT)63 64 #CSV読込65 csv_path = apps[0]66 wb = openpyxl.Workbook()67 ws = wb.active 68 with open(csv_path) as f:69 lst = list(csv.reader(f))70 row = len(lst)71 72 #入力値取得73 s1 = int( ent1.get() )74 75 #入力値取得(フレームレートスピード)76 frs = int( ent2.get() )77 78 e1 = s1 + 9979 if e1>= row:80 e1 = row 81 82 sc = s1 83 open_btn4 = tk.Button(frame4, text='Start11', command=lambda:td(sc, e1, wpm_interval, lst))84 open_btn4.pack(side=tk.LEFT)85 86 lbl41 = tk.Label(frame4,text="表示対象:"+ str(sc) + "~" + str(e1), width="20", font=font)87 lbl41.pack(side=tk.LEFT)88 wpm_interval = int(60/frs * 3000)89 90 # lbl61["text"]=lst[sc][3]91 # lbl71["text"]=sc92 # print("be: " + str(sc) + " : " + str(wpm_interval))93 94 def td(sc, e1, wpm_interval, lst):95 lbl61["text"]=lst[sc][3]96 lbl71["text"]=sc 97 98 print("be: " + str(sc) + " : " + str(wpm_interval))99 100 # start flashing101 flash = Flash(root, wpm = wpm_interval, lst=lst , lbl61=lbl61 , lbl71=lbl71)102 flash.display_next_word()103# flash.display_next_word(sc, e1, lst)104 105 106 #Frame を作ってその中にLabel と ENTRY を格納1 #263D42107 frame1 = tk.Frame(root, width="690", height="40", bg="#ff0000")108 frame1.pack(anchor=tk.W)109 frame1.pack_propagate(0)110 lbl11 = tk.Label(frame1,text="スタート番号", width="20", font=font)111 lbl11.pack(side=tk.LEFT)112 113 ent1 = tk.Entry(frame1, width="3", font=font)114 ent1.pack(side=tk.LEFT)115 ent1.insert(0, "1")116 lbl12 = tk.Label(frame1,text="(0~999の半角数字)", width="20", font=font)117 lbl12.pack(side=tk.LEFT)118 119 #Frame を作ってその中にLabel と ENTRY を格納2120 frame2 = tk.Frame(root, width="690", height="40", bg="#00ff00")121 frame2.pack(anchor=tk.W)122 frame2.pack_propagate(0)123 lbl21 = tk.Label(frame2,text="表示速度", width="20", font=font)124 lbl21.pack(side=tk.LEFT)125 ent2 = tk.Entry(frame2, width="3", font=font)126 ent2.pack(side=tk.LEFT)127 ent2.insert(0, "300")128 lbl22 = tk.Label(frame2,text="(0~999の半角数字)", width="20", font=font)129 lbl22.pack(side=tk.LEFT)130 131 #Frame を作ってその中にLabel と Button を格納3132 frame3 = tk.Frame(root, width="690", height="40", bg="#0000ff")133 frame3.pack(anchor=tk.W)134 frame3.pack_propagate(0)135 lbl31 = tk.Label(frame3,text="単語ファイル選択(CSV)", width="20", font=font)136 lbl31.pack(side=tk.LEFT)137 open_btn = tk.Button(frame3, text='Select', command=openFile)138 open_btn.pack(side=tk.LEFT)139 140 #Frame を作ってその中にLabel と Button を格納4141 frame4 = tk.Frame(root, width="690", height="40", bg="#ffff00")142 frame4.pack(anchor=tk.W)143 frame4.pack_propagate(0)144 145 #Frame を作ってその中にLabel と Button を格納5146 frame5 = tk.Frame(root, width="690", height="120", bg="#00ffff")147 frame5.pack(anchor=tk.W)148 frame5.pack_propagate(0)149 150 #Frame を作ってその中にLabel を格納6151 frame6 = tk.Frame(root, width="690", height="40", bg="#0000ff")152 frame6.pack(anchor=tk.W)153 frame6.pack_propagate(0)154 lbl61 = tk.Label(frame6,text="61", width="20", font=font, anchor=tk.W)155 lbl61.place(x=35, y=0)156 157 #Frame を作ってその中にLabel を格納6158 frame7 = tk.Frame(root, width="690", height="40", bg="#0000ff")159 frame7.pack(anchor=tk.W)160 frame7.pack_propagate(0)161 lbl71 = tk.Label(frame7,text="71", width="20", font=font, anchor=tk.W)162 lbl71.place(x=30, y=0)163 164 root.mainloop()165

コメントを投稿

0 コメント