利用python编写列车票务管理系统 要求: 1. 数据库使用数据库软件MySQL或SQLite 3. 功能主要包括信息添加、删除、修改、查询 4. 图形化界面

时间: 2024-03-23 08:42:01 浏览: 22
好的,以下是一份简单的列车票务管理系统的代码示例,供您参考: ```python import tkinter as tk import tkinter.messagebox as msgbox import sqlite3 class TrainTicketSystem: def __init__(self): self.conn = sqlite3.connect('train_ticket.db') self.c = self.conn.cursor() self.create_table() self.window = tk.Tk() self.window.title('列车票务管理系统') self.window.geometry('600x400') self.create_widgets() self.window.mainloop() def create_table(self): self.c.execute('''CREATE TABLE IF NOT EXISTS train ( id INTEGER PRIMARY KEY, train_no TEXT, start_station TEXT, end_station TEXT, departure_time TEXT, arrive_time TEXT, price TEXT)''') self.conn.commit() def create_widgets(self): # 车次信息标签 tk.Label(self.window, text='车次信息', font=('Arial', 16)).place(x=20, y=20) # 车次信息输入框 tk.Label(self.window, text='车次号:').place(x=20, y=60) self.train_no_entry = tk.Entry(self.window, width=20) self.train_no_entry.place(x=80, y=60) tk.Label(self.window, text='始发站:').place(x=20, y=100) self.start_station_entry = tk.Entry(self.window, width=20) self.start_station_entry.place(x=80, y=100) tk.Label(self.window, text='终点站:').place(x=20, y=140) self.end_station_entry = tk.Entry(self.window, width=20) self.end_station_entry.place(x=80, y=140) tk.Label(self.window, text='出发时间:').place(x=20, y=180) self.departure_time_entry = tk.Entry(self.window, width=20) self.departure_time_entry.place(x=80, y=180) tk.Label(self.window, text='到达时间:').place(x=20, y=220) self.arrive_time_entry = tk.Entry(self.window, width=20) self.arrive_time_entry.place(x=80, y=220) tk.Label(self.window, text='价格:').place(x=20, y=260) self.price_entry = tk.Entry(self.window, width=20) self.price_entry.place(x=80, y=260) # 操作按钮 tk.Button(self.window, text='添加', width=10, command=self.add_train).place(x=20, y=300) tk.Button(self.window, text='修改', width=10, command=self.update_train).place(x=100, y=300) tk.Button(self.window, text='删除', width=10, command=self.delete_train).place(x=180, y=300) tk.Button(self.window, text='查询', width=10, command=self.query_train).place(x=260, y=300) # 车次信息列表 tk.Label(self.window, text='车次列表', font=('Arial', 16)).place(x=320, y=20) self.train_listbox = tk.Listbox(self.window, width=50, height=15) self.train_listbox.place(x=320, y=60) self.refresh_train_listbox() def add_train(self): train_no = self.train_no_entry.get() start_station = self.start_station_entry.get() end_station = self.end_station_entry.get() departure_time = self.departure_time_entry.get() arrive_time = self.arrive_time_entry.get() price = self.price_entry.get() if not train_no or not start_station or not end_station or not departure_time or not arrive_time or not price: msgbox.showerror('错误', '请填写完整的车次信息') return self.c.execute('INSERT INTO train (train_no, start_station, end_station, departure_time, arrive_time, price) VALUES (?, ?, ?, ?, ?, ?)', (train_no, start_station, end_station, departure_time, arrive_time, price)) self.conn.commit() self.refresh_train_listbox() msgbox.showinfo('提示', '添加车次信息成功') def update_train(self): selection = self.train_listbox.curselection() if not selection: msgbox.showerror('错误', '请先选择要修改的车次信息') return train_no = self.train_no_entry.get() start_station = self.start_station_entry.get() end_station = self.end_station_entry.get() departure_time = self.departure_time_entry.get() arrive_time = self.arrive_time_entry.get() price = self.price_entry.get() if not train_no or not start_station or not end_station or not departure_time or not arrive_time or not price: msgbox.showerror('错误', '请填写完整的车次信息') return train_id = self.train_listbox.get(selection[0])[0] self.c.execute('UPDATE train SET train_no=?, start_station=?, end_station=?, departure_time=?, arrive_time=?, price=? WHERE id=?', (train_no, start_station, end_station, departure_time, arrive_time, price, train_id)) self.conn.commit() self.refresh_train_listbox() msgbox.showinfo('提示', '修改车次信息成功') def delete_train(self): selection = self.train_listbox.curselection() if not selection: msgbox.showerror('错误', '请先选择要删除的车次信息') return train_id = self.train_listbox.get(selection[0])[0] self.c.execute('DELETE FROM train WHERE id=?', (train_id,)) self.conn.commit() self.refresh_train_listbox() msgbox.showinfo('提示', '删除车次信息成功') def query_train(self): train_no = self.train_no_entry.get() start_station = self.start_station_entry.get() end_station = self.end_station_entry.get() departure_time = self.departure_time_entry.get() arrive_time = self.arrive_time_entry.get() price = self.price_entry.get() sql = 'SELECT * FROM train WHERE 1=1' if train_no: sql += ' AND train_no="' + train_no + '"' if start_station: sql += ' AND start_station="' + start_station + '"' if end_station: sql += ' AND end_station="' + end_station + '"' if departure_time: sql += ' AND departure_time="' + departure_time + '"' if arrive_time: sql += ' AND arrive_time="' + arrive_time + '"' if price: sql += ' AND price="' + price + '"' self.c.execute(sql) rows = self.c.fetchall() if not rows: msgbox.showinfo('提示', '没有符合条件的车次信息') return self.train_listbox.delete(0, tk.END) for row in rows: self.train_listbox.insert(tk.END, row) def refresh_train_listbox(self): self.train_listbox.delete(0, tk.END) self.c.execute('SELECT * FROM train') rows = self.c.fetchall() for row in rows: self.train_listbox.insert(tk.END, row) if __name__ == '__main__': t = TrainTicketSystem() ``` 这份代码使用了SQLite作为数据库软件,使用了Tkinter模块实现图形化界面。它包括了车次信息的添加、删除、修改、查询等主要功能。在界面上,车次信息通过标签和输入框展示,操作按钮用于触发相应的操作,车次信息列表用于展示查询结果。每个操作都对应着一个方法,其中添加、修改、删除方法都会先检查用户输入的信息是否完整,查询方法会动态生成SQL语句,根据用户输入的条件进行筛选。在添加、修改、删除操作完成后,车次信息列表会自动刷新。

相关推荐

最新推荐

recommend-type

Python SQLite3数据库操作类分享

主要介绍了Python SQLite3数据库操作类分享,需要的朋友可以参考下
recommend-type

Python实现读取TXT文件数据并存进内置数据库SQLite3的方法

主要介绍了Python实现读取TXT文件数据并存进内置数据库SQLite3的方法,涉及Python针对txt文件的读取及sqlite3数据库的创建、插入、查询等相关操作技巧,需要的朋友可以参考下
recommend-type

Python实现读写sqlite3数据库并将统计数据写入Excel的方法示例

主要介绍了Python实现读写sqlite3数据库并将统计数据写入Excel的方法,涉及Python针对sqlite3数据库的读取及Excel文件相关操作技巧,需要的朋友可以参考下
recommend-type

Python实现将sqlite数据库导出转成Excel(xls)表的方法

主要介绍了Python实现将sqlite数据库导出转成Excel(xls)表的方法,结合实例形式分析了Python针对sqlite数据库的连接、读取及使用写操作包(xlwt)生成Excel表的相关实现技巧,需要的朋友可以参考下
recommend-type

Python操作SQLite数据库的方法详解

主要介绍了Python操作SQLite数据库的方法,较为详细的分析了Python安装sqlite数据库模块及针对sqlite数据库的常用操作技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SQL怎么实现 数据透视表

SQL可以通过使用聚合函数和GROUP BY子句来实现数据透视表。 例如,假设有一个销售记录表,其中包含产品名称、销售日期、销售数量和销售额等信息。要创建一个按照产品名称、销售日期和销售额进行汇总的数据透视表,可以使用以下SQL语句: ``` SELECT ProductName, SaleDate, SUM(SaleQuantity) AS TotalQuantity, SUM(SaleAmount) AS TotalAmount FROM Sales GROUP BY ProductName, SaleDate; ``` 该语句将Sales表按照ProductName和SaleDat
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。