目次
品番検索プログラムを作る順序
1.品番検索プログラム①から⑪まで
2.品番を読み込みエクセルに品番ロケがあるかを調べるためのコード
3.tkinterに、リセットボタンと終了ボタンを作る
完成図
コード
import tkinter as tk
import openpyxl
# ①メインウィンドウを作成
root = tk.Tk()
# ②ウィンドウのサイズを設定
root.geometry("1000x500")
# ③画面タイトル
root.title('品番検索')
#④ラベル
label1 = tk.Label(text='品番入力',font=("Meiryo", "20", "normal"))
label1.place(x=1,y=25)
#⑤品番入力ボックス
textbox1 = tk.Entry(font=("Meiryo", "20", "normal"))
textbox1.place(x=120, y =25, width=520,height=40)
#⑥開いた時にカーソルをBOX内に表示する
textbox1.focus_set()
#⑦品番表示ボックスラベル
label1 = tk.Label(text='品番',font=("Meiryo", "20", "normal"))
label1.place(x=1,y=150)
#⑧品番表示ボックス
textbox2 = tk.Entry(font=("Meiryo", "20", "normal"))
textbox2.place(x=120, y =150, width=520,height=40)
#⑨ロケ表示ボックスラベル
label1 = tk.Label(text='ロケ',font=("Meiryo", "20", "normal"))
label1.place(x=1,y=200)
#⑩ロケ表示ボックス
textbox3 = tk.Entry(font=("Meiryo", "20", "normal"))
textbox3.place(x=120, y =200, width=520,height=40)
#⑪ロケなし表示ボックス
textbox4 = tk.Entry(font=("Meiryo", "20", "normal"))
textbox4.place(x=120, y =310, width=520,height=40)
#次に品番を検索します
#検索ボタンを押し記入した品番を読み込む
hinban_list = []
roke_list = []
def kensaku():
#品番入力ボックスから品番をゲット
hinban = textbox1.get()
#エクセルと照合する
wb = openpyxl.load_workbook('roke.xlsx')
ws = wb['Sheet1']
#エクセルの最大行を取得(rokeは11行)
maxrow = ws.max_row
#hinbanをrokeエクセルの上から最大行まで順番に照合していく
for i in range(maxrow-1):
name = ws.cell(i+2, 1).value
#一致したらhinban_lisit,roke_listに入れる
if hinban == name:
hinban_list.append(hinban)
#hinbanとエクセル合致したロケのセル
roke = ws.cell(i+2, 2).value
roke_list.append(roke)
#listが空の場合はロケなし表示、空でなければ品番とロケをボックスに表示
if not hinban_list:
textbox4.insert(tk.END,hinban+' のロケはありません')
else:
textbox2.insert(tk.END,hinban_list[0])
textbox3.insert(tk.END,roke_list[0])
#検索ボタン
button = tk.Button(text='検索', font=("Meiryo", "12", "normal"), command = kensaku)
button.place(x=100, y =70, width=100, height=50 )
#ボックス内をクリアしてリセットして再度検索可能にする
def clear():
textbox1.delete(0,tk.END)
textbox2.delete(0,tk.END)
textbox3.delete(0,tk.END)
textbox4.delete(0,tk.END)
#リストクリア
hinban_list.clear()
roke_list.clear()
#カーソル位置をボックス内に戻す
textbox1.focus_set()
#終了
def end():
root.destroy()
#クリアボタン
button = tk.Button(text='クリア', font=("Meiryo", "12", "normal"), command = clear)
button.place(x=100, y =250, width=100, height=50 )
#閉じるボタン
button = tk.Button(text='終了', font=("Meiryo", "12", "normal"), command = end)
button.place(x=100, y =370, width=100, height=50 )
root.mainloop()
上記コードを個別に見ていきます。
まずは①から⑪まで
>>>品番検索プログラム①から⑪まで


