ハンバーガーショップ

ハンバーガーショップ(高1女子/作)
データベースとPythonを連携した作品第1号ですね。Python単体でプログラミングした作品は保存ができません。保存するためには変数をはじめとする各パラメータの値をデータベースに書き込み、必要に応じて読み込む必要があります。この作品では売上合計の値がデータベースから読み込んでいます。

データベースを作成するファイルを本体のプログラムと分けて書いているところがいいと思います。売上金額の計算も持てる知識を最大限に利用して作っているなと感じます。

各レコードの数量×単価を積み上げて計算している点が工夫の産物だと思います。しかし、このやり方だと項目が増えると苦しくなってきます。データの正規化を学んですっきりとした形でデータ管理する方法を学んでいきましょう。

まだまだ、伸びしろだらけです。




Pythonで記述したコード(データベース)
import sqlite3

#売上管理DBの作成
接続=sqlite3.connect("db\売上管理.db")

#テーブル作成用のSQL作成
sql="create table if not exists 当月売上(uriage integer,h_count integer,p_count integer,d_count integer)"

#SQLの実行
接続.execute(sql)

#データを登録する
sql ="insert into 当月売上 values(?,?,?,?)"

データ=[]

接続.executemany(sql,データ)

#コミット
接続.commit()

#データベースを閉じる
接続.close()

#メッセージ表示
input("売上管理DBが作成されました。\nEnterキーで終了する")



Pythonで記述したコード
import tkinter,datetime,sqlite3,pygame

#売上管理DBへの接続
接続=sqlite3.connect("db\売上管理.db") #売上管理DBへの接続

#変数定義
合計金額=0
h_カウント=0 #ハンバーガーの数量カウント用
p_カウント=0 #ポテトの数量カウント用
d_カウント=0 #ドリンクの数量カウント用
売上=0

#ウィンドウ作成
root= tkinter.Tk()
root.title("ハンバーガーショップ")
root.geometry("800x600")
root.resizable(False,False)

#画像ファイルアップロード
ハンバーガー=tkinter.PhotoImage(file="image\ハンバーガー.png")
ポテト=tkinter.PhotoImage(file="image\ポテト.png")
ドリンク=tkinter.PhotoImage(file="image\ドリンク.png")
背景=tkinter.PhotoImage(file="image\背景.png")
ロゴ=tkinter.PhotoImage(file="image\ロゴ.png")

#キャンバス作成
canvas=tkinter.Canvas(root,width=800,height=600)
canvas.pack()

#ハンバーガーの注文時の処理
def h計算():
    global 合計金額,h_カウント
    注文音.play()
    注文音.set_volume(0.1)
    合計金額+=500
    h_カウント+=1
    sql="insert into 当月売上 values(?,?,?,?)"#レコード登録
    データ=[(500,1,0,0)]#レコード登録(単価とハンバーガーの数量)
    接続.executemany(sql,データ)#SQLの実行
    接続.commit()#コミット処理
    canvas.delete("金額")
    canvas.create_text(280,390,text="注文合計",fill="black",font=("メイリオ",24))
    canvas.create_text(285,280,text=str(合計金額)+"円",fill="black",font=("メイリオ",24),tag="金額")
    canvas.create_text(680,390,text="売上金額",fill="black",font=("メイリオ",24))

#ポテトの注文時の処理
def p計算():
    global 合計金額,p_カウント
    注文音.play()
    注文音.set_volume(0.1)
    合計金額+=300
    p_カウント+=1
    sql="insert into 当月売上 values(?,?,?,?)"#レコード登録
    データ=[(300,0,1,0)]#レコード登録(単価とポテトの数量)
    接続.executemany(sql,データ)#SQLの実行
    接続.commit()#コミット処理
    canvas.delete("金額")
    canvas.create_text(280,390,text="注文合計",fill="black",font=("メイリオ",24))
    canvas.create_text(285,280,text=str(合計金額)+"円",fill="black",font=("メイリオ",24),tag="金額")
    canvas.create_text(680,390,text="売上金額",fill="black",font=("メイリオ",24))

#ドリンク注文時の処理
def d計算():
    global 合計金額,d_カウント
    注文音.play()
    注文音.set_volume(0.1)
    合計金額+=250
    d_カウント+=1
    sql="insert into 当月売上 values(?,?,?,?)"#レコード登録
    データ=[(250,0,0,1)]#レコード登録(単価とドリンクの数量)
    接続.executemany(sql,データ)#SQLの実行
    接続.commit()#コミット処理
    canvas.delete("金額")
    canvas.create_text(280,390,text="注文合計",fill="black",font=("メイリオ",24))
    canvas.create_text(285,280,text=str(合計金額)+"円",fill="black",font=("メイリオ",24),tag="金額")
    canvas.create_text(680,390,text="売上金額",fill="black",font=("メイリオ",24))

#売上計算処理
def 計算結果():
    global h_カウント,p_カウント,d_カウント,売上
    売上計算音.play()
    売上計算音.set_volume(0.1)
    売上=0
    現在日時=datetime.datetime.now()
    canvas.delete("ロゴ")
    canvas.delete("個数")
    canvas.create_text(0,470,text="【売れたもの("+str(現在日時.month)+"月"+str(現在日時.day)+"日)】",fill="black",font=("メイリオ",20),tag="個数",anchor='w')
    canvas.create_text(10,500,text="ハンバーガー:"+str(h_カウント)+"個、"+str(h_カウント*500)+"円",fill="black",tag="個数",font=("メイリオ",20),anchor='w')
    canvas.create_text(10,540,text="ポテト:"+str(p_カウント)+"個、"+str(p_カウント*300)+"円",fill="black",tag="個数",font=("メイリオ",20),anchor='w')
    canvas.create_text(10,580,text="ドリンク:"+str(d_カウント)+"個、"+str(d_カウント*250)+"円",fill="black",tag="個数",font=("メイリオ",20),anchor='w')
    canvas.create_image(680,280,image=ロゴ,tag="ロゴ")
    canvas.create_text(500,470,text="売上合計",fill="red",font=("メイリオ",20,"bold"),tag="個数",anchor='w')
    #売上金額計算処理
    カーソル=接続.cursor()
    sql2="select * from 当月売上"
    カーソル.execute(sql2)
    for row in カーソル:
        売上+=row[0]*row[1]+row[0]*row[2]+row[0]*row[3]#レコード単位の積を合計
    canvas.create_text(500,500,text=str(売上)+"円",fill="red",font=("メイリオ",20,"bold"),tag="個数",anchor='w')

#背景画像表示
canvas.create_image(400,225,image=背景)

#ハンバーガー用
canvas.create_text(195,75,text="注文\nボタン",font=("メイリオ",24,"bold"),fill="green")
#ポテト用
canvas.create_text(460,75,text="注文\nボタン",font=("メイリオ",24,"bold"),fill="green")
#ドリンク用
canvas.create_text(720,75,text="注文\nボタン",font=("メイリオ",24,"bold"),fill="green")


#ハンバーガーボタン作成
hボタン=tkinter.Button(root,image=ハンバーガー,bd=5,command=h計算)
hボタン.place(x=30,y=10)
#ポテトボタン作成
pボタン=tkinter.Button(root,image=ポテト,bd=5,command=p計算)
pボタン.place(x=290,y=1)
#ドリンクボタン作成
dボタン=tkinter.Button(root,image=ドリンク,bd=5,command=d計算)
dボタン.place(x=550,y=5)
#売上計算ボタン作成
売上計算=tkinter.Button(root,text="売上計算",font=("メイリオ",13,"bold"),bg="orange",command=計算結果)
売上計算.place(x=520,y=365)

#ハンバーガーの値段_テキスト作成
canvas.create_text(80,180,text="500円",fill="orange",font=("メイリオ",24))
#ポテトの値段_テキスト作成
canvas.create_text(350,180,text="300円",fill="orange",font=("メイリオ",24))
#ドリンクの値段_テキスト作成
canvas.create_text(600,180,text="250円",fill="orange",font=("メイリオ",24))

#音声
pygame.init()
BGM=pygame.mixer.Sound("sound\BGM.wav")
注文音=pygame.mixer.Sound("sound\注文ボタン.mp3")
売上計算音=pygame.mixer.Sound("sound\売上計算ボタン.mp3")

#BGM
BGM.play(-1)
BGM.set_volume(0.05)

root.mainloop()