利用python编写列车票务管理系统 要求: 1. 数据库使用数据库软件MySQL或SQLite 3. 功能主要包括信息添加、删除、修改、查询 4. 图形化界面
时间: 2024-03-23 18:42:01 浏览: 156
好的,以下是一份简单的列车票务管理系统的代码示例,供您参考:
```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语句,根据用户输入的条件进行筛选。在添加、修改、删除操作完成后,车次信息列表会自动刷新。
阅读全文