前提・実現したいこと
Python3.6で時差を計算するシステムを作っています。
コンボボックスからエントリーボックスへ値を渡す為の機能を実装しているのですが、
insertを使って、別のクラスの関数に入っているエントリーボックスへ値を出力したいと考えています。
python
1#coding: utf-8 2 3import tkinter as tk 4from tkinter import ttk 5from datetime import datetime 6import csv 7 8 9class Data: 10 11 def data_input(self): 12 # リスト型 13 t = [] 14 v = [] 15 key = ['Country', 'UTC', 'Summer'] 16 country_info = {} 17 18 # CSVファイルを開く 19 with open("data/csv/utc_original.csv", encoding="UTF-8") as f: 20 reader = csv.reader(f) 21 header = next(reader) 22 # 1行ごとにリスト型に格納 23 for row in reader: 24 row[2] = (float(row[2])) 25 if row[3] == 'True': 26 row[3] = True 27 elif row[3] == 'False': 28 row[3] = False 29 t.append(row) 30 31 # 首都のみリストに移動 32 for i in range(len(t)): 33 v.append(t[i][0]) 34 t[i].remove(t[i][0]) 35 36 # keyリストからキーを取得しながら、情報を格納して、首都をキーに指定 37 for i in range(len(t)): 38 dic = dict(zip(key, t[i])) 39 country_info[v[i]] = dic 40 41 return country_info 42 43 def data_day(self): 44 a = datetime.now().strftime("%H") 45 var = tk.StringVar(root) 46 var.set(a) 47 48 return var 49 50 51 52class Cmb(ttk.Combobox): 53 # --- クラス Cmb コマンドボックスの値 --- 54 def __init__(self, master, dictionary, *args, **kw): 55 ttk.Combobox.__init__(self, master, values = sorted(list(dictionary.keys())), state = 'readonly',width=30, *args, **kw) 56 self.dictionary = dictionary 57 self.bind('<<ComboboxSelected>>', self.selected) 58 59 def value(self): 60 return self.dictionary[self.get()] 61 62 def selected(self, event): 63 print(self.value()['UTC']) 64 a = self.value()['UTC'] 65 66 ここから a の値をClassのApplication内の、関数create_entry内のety0 へ渡したい 67 68 69 70 71 72 73 74class Application(tk.Frame): 75 76 def __init__(self, master=None): 77 super().__init__(master) 78 master.title("時差計算アプリ") 79 master.resizable(0, 0) 80 master.geometry("480x200") 81 master.grid_rowconfigure(1, weight=1) 82 master.grid_columnconfigure(0, weight=1) 83 self.pack() 84 85 self.header = tk.Frame(self.master, bg='#999999', width=500, height=10, pady=3) 86 self.center = tk.Frame(self.master, bg='#dddddd', width=500, height=10, pady=3) 87 self.footer0 = tk.Frame(self.master, bg='#dddddd', width=500, height=10, pady=3) 88 self.footer1 = tk.Frame(self.master, bg='#dddddd', width=500, height=10, pady=3) 89 self.create_layout() 90 91 self.head_left = tk.Frame(self.header, bg='#999999', width=100, height=10, padx=3, pady=3) 92 self.head_mid0 = tk.Frame(self.header, bg='#999999', width=200, height=10, padx=3, pady=3) 93 self.head_mid1 = tk.Frame(self.header, bg='#999999', width=100, height=10, padx=3, pady=3) 94 self.head_right = tk.Frame(self.header, bg='#999999', width=100, height=10, padx=3, pady=3) 95 self.create_header() 96 97 self.cnt_left = tk.Frame(self.center, bg='#dddddd', width=100, height=10, padx=3, pady=3) 98 self.cnt_mid0 = tk.Frame(self.center, bg='#dddddd', width=200, height=10, padx=3, pady=3) 99 self.cnt_mid1 = tk.Frame(self.center, bg='#dddddd', width=50, height=10, padx=0, pady=0) 100 self.cnt_mid2 = tk.Frame(self.center, bg='#dddddd', width=50, height=10, padx=3, pady=3) 101 self.cnt_right = tk.Frame(self.center, bg='#dddddd', width=100, height=10, padx=3, pady=3) 102 self.create_center() 103 104 self.ftr_left = tk.Frame(self.footer0, bg='#dddddd', width=70, height=10, padx=3, pady=3) 105 self.ftr_mid0 = tk.Frame(self.footer0, bg='#dddddd', width=70, height=10, padx=3, pady=3) 106 self.ftr_mid1 = tk.Frame(self.footer0, bg='#dddddd', width=70, height=10, padx=3, pady=3) 107 self.ftr_mid2 = tk.Frame(self.footer0, bg='#dddddd', width=70, height=10, padx=3, pady=3) 108 self.ftr_mid3 = tk.Frame(self.footer0, bg='#dddddd', width=70, height=10, padx=3, pady=3) 109 self.ftr_right = tk.Frame(self.footer0, bg='#dddddd', width=10, height=10, padx=3, pady=3) 110 self.create_footer() 111 112 self.create_label() 113 self.create_enter() 114 self.create_checkbox() 115 self.create_button() 116 self.create_spin() 117 self.create_cmb() 118 self.create_pack() 119 120 121 def create_layout(self): 122 # --- コンテナ設計 --- 123 self.header.grid(row=0, sticky="ew") 124 self.center.grid(row=1, sticky="nsew") 125 self.footer0.grid(row=2, sticky="ew") 126 self.footer1.grid(row=3, sticky="ew") 127 128 def create_header(self): 129 # --- headerの詳細設計 --- 130 self.header.grid_rowconfigure(0, weight=1) 131 self.header.grid_columnconfigure(1, weight=1) 132 133 self.head_left.grid(row=0, column=0, sticky="ns") 134 self.head_mid0.grid(row=0, column=1, sticky="nsew") 135 self.head_mid1.grid(row=0, column=2, sticky="nsew") 136 self.head_right.grid(row=0, column=3, sticky="ns") 137 138 def create_center(self): 139 # --- centerの詳細設計 --- 140 self.center.grid_rowconfigure(0, weight=1) 141 self.center.grid_columnconfigure(1, weight=1) 142 143 self.cnt_left.grid(row=0, column=0, sticky="ns") 144 self.cnt_mid0.grid(row=0, column=1, sticky="nsew") 145 self.cnt_mid1.grid(row=0, column=2, sticky="nsew") 146 self.cnt_mid2.grid(row=0, column=3, sticky="nsew") 147 self.cnt_right.grid(row=0, column=4, sticky="ns") 148 149 150 def create_footer(self): 151 # -- footer0の詳細 --- 152 self.footer0.grid_rowconfigure(0, weight=1) 153 self.footer0.grid_columnconfigure(1, weight=1) 154 155 self.ftr_left.grid(row=0, column=0, sticky="ns") 156 self.ftr_mid0.grid(row=0, column=1, sticky="nsew") 157 self.ftr_mid1.grid(row=0, column=2, sticky="nsew") 158 self.ftr_mid2.grid(row=0, column=3, sticky="nsew") 159 self.ftr_mid3.grid(row=0, column=4, sticky="nsew") 160 self.ftr_right.grid(row=0, column=5, sticky="ns") 161 162 def create_pack(self): 163 164 self.header.pack() 165 self.center.pack() 166 self.footer0.pack() 167 self.footer1.pack() 168 169 def create_label(self): 170 # --- テキストラベル --- 171 lbl0 = tk.Label(self.head_left, text="CSV version", fg='blue', bg='#999999', padx=5, pady=5) 172 lbl1 = tk.Label(self.cnt_left, text="City1:", bg='#dddddd', padx=5, pady=1) 173 lbl2 = tk.Label(self.cnt_left, text="City2:", bg='#dddddd', padx=5, pady=1) 174 lbl3 = tk.Label(self.cnt_mid2, text="UTC:", bg='#dddddd', padx=5, pady=1) 175 lbl4 = tk.Label(self.cnt_mid2, text="UTC:", bg='#dddddd', padx=5, pady=1) 176 lbl5 = tk.Label(self.ftr_left, text="現在時刻:", bg='#dddddd', padx=5, pady=1) 177 lbl6 = tk.Label(self.ftr_mid0, text="〇", bg='#dddddd', padx=5, pady=1) 178 lbl7 = tk.Label(self.ftr_mid1, text="C1:", bg='#dddddd', padx=5, pady=1) 179 lbl8 = tk.Label(self.ftr_mid2, text="C2:", bg='#dddddd', padx=5, pady=1) 180 181 lbl0.grid(row=0, column=0) 182 lbl1.grid(row=0) 183 lbl2.grid(row=1) 184 lbl3.grid(row=0, column=0) 185 lbl4.grid(row=1, column=0) 186 lbl5.pack() 187 lbl6.pack() 188 lbl7.pack(side='top') 189 lbl8.pack(side='top') 190 191 return lbl0, lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8 192 193 def create_enter(self): 194 # --- テキストボックス --- 195 ety0 = tk.Entry(self.cnt_right, width=10) 196 ety1 = tk.Entry(self.cnt_right, width=10) 197 ety2 = tk.Entry(self.ftr_left, width=10) 198 ety3 = tk.Entry(self.ftr_mid0, width=10) 199 ety4 = tk.Entry(self.ftr_mid2, width=10) 200 201 ety0.pack() 202 ety1.pack() 203 ety2.pack() 204 ety3.pack() 205 ety4.pack(side='top') 206 207 return ety0, ety1, ety2, ety3, ety4 208 209 def create_checkbox(self): 210 # --- チェックボックス --- 211 chk2 = tk.Checkbutton(self.cnt_mid1, text='DST', bg='#dddddd').pack() 212 chk3 = tk.Checkbutton(self.cnt_mid1, text='DST', bg='#dddddd').pack() 213 214 return chk2, chk3 215 216 def create_button(self): 217 btn0 = tk.Button(self.head_right, text='計算') 218 btn1 = tk.Button(self.head_right, text='クリア', width=10, bg='#dddddd') 219 btn2 = tk.Button(self.footer1, text='Yesterday', bg='#dddddd', width=10) 220 btn3 = tk.Button(self.footer1, text='Today', bg='#dddddd', width=10) 221 btn4 = tk.Button(self.footer1, text='Tomorrow', bg='#dddddd', width=10) 222 223 btn0.pack(side='right') 224 btn1.pack(side='right') 225 btn2.pack(side='right') 226 btn3.pack(side='right') 227 btn4.pack(side='right') 228 229 return btn0, btn1, btn2, btn3, btn4 230 231 def create_spin(self): 232 i = Data() 233 spn1 = tk.Spinbox(self.ftr_mid1, from_=1, to=24, textvariable=i.data_day(), width=10) 234 235 spn1.pack() 236 237 return spn1 238 239 def create_cmb(self): 240 i = Data() 241 cmb1 = Cmb(self.cnt_mid0,i.data_input()) 242 cmb2 = ttk.Combobox(self.cnt_mid0) 243 cmb1.set('都市名を選択') 244 cmb2.set('都市名を選択') 245 246 cmb1.pack() 247 cmb2.pack() 248 249 return cmb1, cmb2 250 251 252 253if __name__ == '__main__': 254 root = tk.Tk() 255 app = Application(master=root) 256 app.mainloop() 257 258 259 260 261 262 263 264
CSV
1国,都市,UTC,DST 2ハワイ,アメリカ,-10,False 3ロサンゼルス,アメリカ,-8,True 4デンバー,アメリカ,-7,True 5シカゴ,アメリカ,-6,True 6ニューヨーク,アメリカ,-5,True 7ブエノスアイレス,アルゼンチン,-3,True 8キト,エクアドル,-5,False 9バンクーバー ,カナダ,-8,True 10バンフ,カナダ,-7, True 11
宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/01 02:43
2018/02/01 03:18
2018/02/23 00:41