import sqlite3 from tkinter import * from tkinter import ttk class BusStationManager: def __init__(self, master): self.master = master self.master.title("公交车站管理系统") # 创建数据库 self.conn = sqlite3.connect('bus_station.db') self.cursor = self.conn.cursor() self.create_table() # 创建GUI self.label_frame = ttk.LabelFrame(self.master, text="操作") self.label_frame.grid(column=0, row=0, padx=10, pady=10, sticky=W) self.add_button = ttk.Button(self.label_frame, text="添加", command=self.add_entry) self.add_button.grid(column=0, row=0, padx=10, pady=10, sticky=W) self.delete_button = ttk.Button(self.label_frame, text="删除", command=self.delete_entry) self.delete_button.grid(column=1, row=0, padx=10, pady=10, sticky=W) self.update_button = ttk.Button(self.label_frame, text="修改", command=self.update_entry) self.update_button.grid(column=2, row=0, padx=10, pady=10, sticky=W) self.query_button = ttk.Button(self.label_frame, text="查询", command=self.query_entry) self.query_button.grid(column=3, row=0, padx=10, pady=10, sticky=W) self.result_frame = ttk.LabelFrame(self.master, text="结果") self.result_frame.grid(column=0, row=1, padx=10, pady=10, sticky=W) self.result_text = Text(self.result_frame, width=40, height=10) self.result_text.grid(column=0, row=0, padx=10, pady=10) def create_table(self): self.cursor.execute('''CREATE TABLE IF NOT EXISTS stations (id INTEGER PRIMARY KEY, name TEXT, location TEXT)''') self.cursor.execute('''CREATE TABLE IF NOT EXISTS routes (id INTEGER PRIMARY KEY, name TEXT, start_station INTEGER, end_station INTEGER)''') self.cursor.execute('''CREATE TABLE IF NOT EXISTS vehicles (id INTEGER PRIMARY KEY, license_plate TEXT, model TEXT, capacity INTEGER)''') self.cursor.execute('''CREATE TABLE IF NOT EXISTS passengers (id INTEGER PRIMARY KEY, name TEXT, destination INTEGER, status TEXT)''') self.conn.commit() def add_entry(self): # 添加车站 name = self.add_station_name.get() location = self.add_station_location.get() self.cursor.execute("INSERT INTO stations (name, location) VALUES (?, ?)", (name, location)) # 添加线路 route_name = self.add_route_name.get() start_station = self.add_route_start.get() end_station = self.add_route_end.get() self.cursor.execute("INSERT INTO routes (name, start_station, end_station) VALUES (?, ?, ?)", (route_name, start_station, end_station)) # 添加车辆 license_plate = self.add_vehicle_license.get() model = self.add_vehicle_model.get() capacity = self.add_vehicle_capacity.get() self.cursor.execute("INSERT INTO vehicles (license_plate, model, capacity) VALUES (?, ?, ?)", (license_plate, model, capacity)) # 添加乘客 passenger_name = self.add_passenger_name.get() destination = self.add_passenger_destination.get() status = self.add_passenger_status.get() self.cursor.execute("INSERT INTO passengers (name, destination, status) VALUES (?, ?, ?)", (passenger_name, destination, status)) self.conn.commit() def delete_entry(self): # 删除车站 name = self.delete_station_name.get() self.cursor.execute("DELETE FROM stations WHERE name=?", (name,)) # 删除线路 route_name = self.delete_route_name.get() self.cursor.execute("DELETE FROM routes WHERE name=?", (route_name,)) # 删除车辆 license_plate = self.delete_vehicle_license.get() self.cursor.execute("DELETE FROM vehicles WHERE license_plate=?", (license_plate,)) # 删除乘客 passenger_name = self.delete_passenger_name.get() self.cursor.execute("DELETE FROM passengers WHERE name=?", (passenger_name,)) self.conn.commit() def update_entry(self): # 更新车站 name = self.update_station_name.get() location = self.update_station_location.get() self.cursor.execute("UPDATE stations SET location=? WHERE name=?", (location, name)) # 更新线路 route_name = self.update_route_name.get() start_station = self.update_route_start.get() end_station = self.update_route_end.get() self.cursor.execute("UPDATE routes SET start_station=?, end_station=? WHERE name=?", (start_station, end_station, route_name)) # 更新车辆 license_plate = self.update_vehicle_license.get() model = self.update_vehicle_model.get() capacity = self.update_vehicle_capacity.get() self.cursor.execute("UPDATE vehicles SET model=?, capacity=? WHERE license_plate=?", (model, capacity, license_plate)) # 更新乘客 passenger_name = self.update_passenger_name.get() destination = self.update_passenger_destination.get() status = self.update_passenger_status.get() self.cursor.execute("UPDATE passengers SET destination=?, status=? WHERE name=?", (destination, status, passenger_name)) self.conn.commit() def query_entry(self): # 查询车站 name = self.query_station_name.get() self.cursor.execute("SELECT * FROM stations WHERE name=?", (name,)) result = self.cursor.fetchone() self.result_text.delete(1.0, END) self.result_text.insert(END, f"查询结果: {result}\n") # 查询线路 route_name = self.query_route_name.get() self.cursor.execute("SELECT * FROM routes WHERE name=?", (route_name,)) result = self.cursor.fetchone() self.result_text.insert(END, f"查询结果: {result}\n") # 查询车辆 license_plate = self.query_vehicle_license.get() self.cursor.execute("SELECT * FROM vehicles WHERE license_plate=?", (license_plate,)) result = self.cursor.fetchone() self.result_text.insert(END, f"查询结果: {result}\n") # 查询乘客 passenger_name = self.query_passenger_name.get() self.cursor.execute("SELECT * FROM passengers WHERE name=?", (passenger_name,)) result = self.cursor.fetchone() self.result_text.insert(END, f"查询结果: {result}\n")