首页 > 数据库 >【项目实战】基于Python+Tkinter+MySQL的GUI简易酒店管理系统(附完整源码)

【项目实战】基于Python+Tkinter+MySQL的GUI简易酒店管理系统(附完整源码)

时间:2023-03-12 18:58:05浏览次数:54  
标签:execute Tkinter consume Python cur number 源码 tab room

1、项目说明

基于python+Tkinter+MySQL的简易酒店管理系统项目实战

项目需要安装pycharm社区版或专业版都可,MySQL以及项目所需的所有模块

项目需要安装 tkinter、pymysql及其他一些模块
安装命令如下:

项目打包为exe文件需要安装pyinstaller

  • 执行命令:pyinstaller -F -w main.py -i logo图片的地址

 

2、项目主要技术

  • python编程技术

  • tkinter编程

  • mysql数据库技术

 

3、项目结构

  • db.py 数据库链接与操作功能

  • UI.py 界面设计与功能实现

  • main.py 项目入口(运行这个py文件运行项目)

 

4、项目主要功能

  • 店主登录

  • 顾客入住功能

  • 顾客增值服务

  • 顾客消费计算

  • 顾客退房结算

 

5、部分源码

db.py

"""
项目名称:Python简易酒店管理系统
作者:bhml
时间:2023/02/21
代码功能:数据库操作
"""

from tkinter import *
from pymysql import *
import UI
import datetime

# 连接数据库
db = connect(
    host='localhost', port=3306, user='root', password='123456', database='db_hotel'
)
# 实例化数据库对象
cur = db.cursor()

# 此刻正在操作的用户
opration = {
    'id': '',
    'name': '',
}

# 列表
commodity = ['果汁', '毛巾', '烟']
prices = [10, 15, 50]
room_type = ['单人', '双人', '大床', '总统套房']
temp_commodity_list = []


# 记录此时进行操作的用户信息
def working(staff_id):
    cur.execute('select sno,sname from tab_admin where sno={};'.format(staff_id))
    (sno, sname) = cur.fetchone()
    opration['id'] = sno
    opration['name'] = sname


# 登陆
def login(id, password, textvar_id, textvar_pass, Tkinter):
    try:
        cur.execute('select sno from tab_admin where sno={} and spassword={};'.format(id, password))
        sno = cur.fetchone()[0]
        if sno is not None:
            working(id)
            UI.staff_int_windows(Tkinter)
        else:
            UI.popup('error_login')
            UI.input_clear(textvar_id, textvar_pass)
    except:
        UI.popup('error_login')
        UI.input_clear(textvar_id, textvar_pass)


# 登记入住的客户
def direct_register(name, room_type, number, sex, ID):
    InTime = str(datetime.datetime.now().year) + '-' \
             + str(datetime.datetime.now().month) + '-' \
             + str(datetime.datetime.now().day)

    if name != '' and number != '' and sex != '' and ID != '':
        # 有空闲房间时
        try:
            print(room_type)
            cur.execute('select room from tab_room where rtype ="{}" and rstate = 0;'.format(room_type))
            room_number = cur.fetchone()[0]

            # 将信息添加到入住表中
            cur.execute('insert into tab_customers (cname ,csex ,cID ,cnumber ,cInTime,room)'
                        'values("{}" ,"{}" ,"{}" ,"{}" ,"{}" ,"{}")'
                        .format(name, sex, ID, number, InTime, room_number))
            # 房间状态归零
            cur.execute('update tab_room set rstate="1" where room="{}";'.format(room_number))
            # 初始化房间对应的额外消费表
            cur.execute('insert into tab_extra_consume(room ,juice ,towel ,smoke)'
                        'values("{}" ,{} ,{} ,{})'
                        .format(room_number, 0, 0, 0))
            cur.connection.commit()
            message = '登记成功!房间号为' + room_number
            UI.popup(message)
        # 无空余房间
        except Exception as e:
            print(e)
            UI.popup('error_not_null')
    else:
        UI.popup('error_input')


# 按名字查询客户信息
def cheak_customer_info(msg_text, entry, name=''):
    # try:
    if name == '':
        cur.execute('select * from tab_customers order by room')
    else:
        cur.execute(
            'select * from tab_customers where cname like "%{}%" order by room'
            .format(name))

    customers_tuple = cur.fetchall()
    print(len(customers_tuple))
    if len(customers_tuple) != 0:  # 确认存在信息以后执行
        UI.display(customers_tuple, msg_text)
        UI.input_clear(entry)


# 查询收入信息
def cheak_income_info():
    try:
        cur.execute('select * from tab_income order by past_customer')
        result = cur.fetchall()
        customers = []
        in_times = []
        for r in result:
            ok = cur.execute('select cname,cInTime from tab_customers where cID = ' + r[1])
            data = cur.fetchone()
            if ok:
                customers.append(data[0])
                in_times.append(data[1].strftime("%Y-%m-%d"))

        print(result)
        print(customers)
        print(in_times)

        return result, customers, in_times

    except Exception as e:
        print(e)
        return None


# 按照房间号查询客户消费清单
def cheak_customer_consume(textvar_entry, control_text, room_number):
    print(room_number)
    consume_sum = 0
    if room_number == '':
        UI.popup('error_input')
        return
    else:
        try:
            # 获取该房间的房价
            cur.execute('select rprice from tab_room where room = "{}"'.format(room_number))
            consume_sum = cur.fetchone()[0]
            print(consume_sum)
            cur.execute('select rtype from tab_room where room = "{}"'.format(room_number))
            type = cur.fetchone()[0]
            # 该房间消费的物品数量
            cur.execute('select * from tab_extra_consume where room = "{}"'.format(room_number))
            consume_tuple = cur.fetchone()
            print(consume_tuple)
            if consume_tuple is None:
                UI.popup('房号不存在!')
                return

            message = ''
            # 总价格
            sum = consume_tuple[1] * prices[0] + consume_tuple[2] * prices[1] + consume_tuple[3] * prices[2]
            print(sum)

            consume = consume_tuple[1:]
            for i, j in enumerate(consume):
                if j != 0:
                    message += '\t    ' + commodity[i] + \
                               '\t\t X' + str(consume[i]) \
                               + '   \t' + str(prices[i]) \
                               + '   \t' + str(consume[i] * prices[i]) + '\n'

            message += '\n\n\t    ' + type + \
                       '间\t\t X1' \
                       + '   \t' + str(consume_sum) \
                       + '   \t' + str(consume_sum) + '\n'

            message += '\n\n\n\t\t\t合计:' + str(sum + consume_sum)
            UI.dir_display(message, control_text)
        except Exception as e:
            print(e)
            control_text.delete(1.0, END)  # 将先前信息清清除
            textvar_entry.set('')
            UI.popup('error_input')
        return sum + consume_sum


# 确定支付此次入住的费用
def customer_payment(textvar_entry, control_text, room_number=''):
    # 获取总消费额
    consume_sum = cheak_customer_consume(textvar_entry, control_text, room_number)
    print(consume_sum)
    # 如果房间未住人(跳过)
    if consume_sum == 0 or consume_sum is None:
        pass
    else:
        # 获取客户身份证
        cur.execute('select cID from tab_customers where room="{}"'.format(room_number))
        customer_ID = cur.fetchone()[0]

        # 获取退房时间
        OutTime = str(datetime.datetime.now().year) + '-' \
                  + str(datetime.datetime.now().month) + '-' \
                  + str(datetime.datetime.now().day)

        # 收入表的更新
        cur.execute('insert into tab_income(past_ID,past_Out_Time,past_customer) values("{}","{}","{}");'
                    .format(customer_ID, OutTime, consume_sum))

        # 住户表更新(多项信息修改)
        cur.execute('update tab_customers set room=null ,cOutTime="{}" where room="{}";'
                    .format(OutTime, room_number))

        # 消费表清空
        cur.execute('delete from tab_extra_consume where room="{}";'.format(room_number))

        # 房间表归零
        cur.execute('update tab_room set rstate="0" where room="{}";'.format(room_number))

        # 信息保存
        cur.connection.commit()

        UI.popup('ok')

        # 将界面信息清清除
        control_text.delete(1.0, END)
        textvar_entry.set('')


# 完成添加商品操作并显示在文本框中
def add_commodity(room, textvar_room, name, number, textvar_number, control_text):
    # 检查房间号是否存在
    try:
        cur.execute('select * from tab_extra_consume where room="{}";'.format(room))
        response = cur.fetchone()
        if response is None:
            UI.popup('房号不存在!')
            return
        if name == '' or number == '':
            UI.popup('请输入所需商品和数量!')
            return
        # 缓存列表添加元素
        temp_commodity_list.append([room, name, number])
        # 添加框内容清空
        UI.input_clear(textvar_room, textvar_number)
        # 将添加的商品逐一显示在文本框中
        message = str(room) + '号房\t需要' + name + '\tX' + str(number) + '\n'
        UI.dir_display(message, control_text, 1)
    except:
        UI.input_clear(textvar_room, textvar_number)
        UI.popup('error_input')
    print(temp_commodity_list)


# 将添加的商品提交并且记录到数据库中
def save_add_commodity(control_text):
    # 将商品一一记录到额外消费表中
    for sub_temp_commodity_list in temp_commodity_list:
        print(sub_temp_commodity_list)
        if sub_temp_commodity_list[1] == '果汁':
            cur.execute('select juice from tab_extra_consume where room = "{}"; '.format(sub_temp_commodity_list[0]))
            past_number = cur.fetchone()[0]
            print(past_number)
            cur.execute('update tab_extra_consume set juice="{}" where room={};'
                        .format(past_number + int(sub_temp_commodity_list[2]), sub_temp_commodity_list[0]))
        elif sub_temp_commodity_list[1] == '烟':
            cur.execute('select smoke from tab_extra_consume where room = "{}"; '.format(sub_temp_commodity_list[0]))
            past_number = cur.fetchone()[0]
            cur.execute('update tab_extra_consume set smoke="{}" where room={};'
                        .format(past_number + int(sub_temp_commodity_list[2]), sub_temp_commodity_list[0]))
        elif sub_temp_commodity_list[1] == '毛巾':
            cur.execute('select towel from tab_extra_consume where room = "{}"; '.format(sub_temp_commodity_list[0]))
            past_number = cur.fetchone()[0]
            cur.execute('update tab_extra_consume set towel="{}" where room={};'
                        .format(past_number + int(sub_temp_commodity_list[2]), sub_temp_commodity_list[0]))
        # 信息保存
        cur.connection.commit()

    # 缓存列表清空
    temp_commodity_list.clear()

    # 清空text框
    message = ''
    UI.dir_display(message, control_text)

    # 弹窗
    UI.popup('ok')

 

6、运行截图

 

 

 

 

7、项目总结

本项目是一个非常适合练手的项目,对我们的XXXX技术的提升有很大的帮助,推荐大家学习研究这个项目,搞懂其中的原理流程以及知识点非常关键。
 
项目资料截图:

资料获取地址:https://h5.m.taobao.com/awp/core/detail.htm?ft=t&id=704188350264
B站视频讲解地址:https://www.bilibili.com/video/BV1dY4y1m71b/
 
注:其他问题请参看视频讲解,都有介绍,一定要认真看完哦!
代码编写、视频录制不易,感谢您的支持,祝您学习愉快!
 

避免走丢,记得关注哦

标签:execute,Tkinter,consume,Python,cur,number,源码,tab,room
From: https://www.cnblogs.com/bhml/p/17208709.html

相关文章