景点门票销售管理系统 基于 python实现gui界面 之 笔记

本文介绍了如何使用Python构建一个景点门票销售管理系统的图形用户界面。内容包括数据库的建立和设计,如创建表、设置外键约束和触发器,以及Python3中tkinter库的应用,实现交互式界面。此外,还展示了部分程序窗口的截图。
摘要由CSDN通过智能技术生成

 1、建立数据库、建表、加外键约束、建触发器

此处106行

CREATE DATABASE IF NOT EXISTS `景点门票销售管理系统` DEFAULT CHARACTER SET utf8;

USE `景点门票销售管理系统`;

/*Table structure for table `游客信息` */

DROP TABLE IF EXISTS `游客信息`;

CREATE TABLE `游客信息` (
  `游客账号` CHAR(20) NOT NULL,
  `游客密码` CHAR(20) NOT NULL,
  `游客姓名` CHAR(20) DEFAULT NULL,
  `游客性别` CHAR(20) DEFAULT NULL,
  `联系电话` CHAR(20) DEFAULT NULL,
  `注册时间` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`游客账号`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

/*Data for the table `游客信息` */

INSERT  INTO `游客信息`(`游客账号`,`游客密码`,`游客姓名`,`游客性别`,`联系电话`,`注册时间`) VALUES ('11','11','Li','男','112','2021-11-15 20:05:13');

/*Table structure for table `管理员信息` */

DROP TABLE IF EXISTS `管理员信息`;

CREATE TABLE `管理员信息` (
  `管理员账号` CHAR(20) NOT NULL,
  `管理员密码` CHAR(20) NOT NULL,
  `管理员姓名` CHAR(20) NOT NULL,
  `管理员性别` CHAR(20) DEFAULT NULL,
  `联系电话` CHAR(20) DEFAULT NULL,
  `工号` CHAR(20) NOT NULL,
  PRIMARY KEY (`管理员账号`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

/*Data for the table `管理员信息` */

INSERT  INTO `管理员信息`(`管理员账号`,`管理员密码`,`管理员姓名`,`管理员性别`,`联系电话`,`工号`) VALUES ('123451','123451','杨华','男','111112','10081'),('123452','123452','李华','男','10081','111113');

/*Table structure for table `订票信息` */

DROP TABLE IF EXISTS `订票信息`;

CREATE TABLE `订票信息` (
  `订票编号` INT(11) NOT NULL AUTO_INCREMENT,
  `游客账号` CHAR(20) DEFAULT NULL,
  `身份证号` CHAR(20) DEFAULT NULL,
  `联系电话` CHAR(20) DEFAULT NULL,
  `订票数量` CHAR(20) DEFAULT NULL,
  `应付金额` CHAR(20) DEFAULT NULL,
  `操作时间` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`订票编号`),
  KEY `游客账号` (`游客账号`),
  CONSTRAINT `订票信息_ibfk_1` FOREIGN KEY (`游客账号`) REFERENCES `游客信息` (`游客账号`)
) ENGINE=INNODB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;

/*Data for the table `订票信息` */

INSERT  INTO `订票信息`(`订票编号`,`游客账号`,`身份证号`,`联系电话`,`订票数量`,`应付金额`,`操作时间`) VALUES (13,'11','11','111','3','120','2021-11-15 20:32:16');

/*Table structure for table `退票信息` */

DROP TABLE IF EXISTS `退票信息`;

CREATE TABLE `退票信息` (
  `订票编号` INT(11) NOT NULL,
  `游客账号` CHAR(20) DEFAULT NULL,
  `身份证号` CHAR(20) DEFAULT NULL,
  `联系电话` CHAR(20) DEFAULT NULL,
  `退票数量` INT(20) DEFAULT NULL,
  `退还金额` INT(20) DEFAULT NULL,
  `操作时间` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`订票编号`),
  KEY `游客账号` (`游客账号`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

/*Data for the table `退票信息` */


/* Trigger structure for table `订票信息` */

DELIMITER $$
DROP TRIGGER IF EXISTS `tmp1_insert` $$
CREATE TRIGGER `tmp1_insert`
BEFORE INSERT ON `订票信息` 
FOR EACH ROW 
BEGIN
    SET new.`应付金额`= new.`订票数量`*60;
END $$
DELIMITER ;


/* Trigger structure for table `订票信息` */

DELIMITER $$
DROP TRIGGER IF EXISTS `insert_date` $$
CREATE TRIGGER `insert_date` 
BEFORE DELETE ON `订票信息` 
FOR EACH ROW 
BEGIN
    #insert `退票信息` as t set new.`订票编号`= old.`订票编号`,new.`数量`= old.`数量`,new.`金额`= old.`金额` ;
    #insert `退票信息` set `订票编号`=old.`订票编号` where `订票编号`=old.`订票编号`;
    INSERT INTO `退票信息`(`订票编号`,`游客账号`,`身份证号`,`联系电话`,`退票数量`,`退还金额`) VALUES(old.`订票编号`,old.`游客账号`,old.`身份证号`,old.`联系电话`,old.`订票数量`,old.`应付金额`);
END $$ 
DELIMITER ;

2、实现交互界面代码,有待改进

此处2023行

#加油
import pymysql
from tkinter import ttk
import tkinter as tk
import tkinter.font as tkFont
from tkinter import * # 图形界面库
import tkinter.messagebox as messagebox # 弹窗

#开始页面
class StartPage:
	def __init__(self, parent_window):
		#parent_window.update()
		parent_window.destroy() # 销毁子界面
 
		self.window = tk.Tk()  # 初始框的声明
		self.window.title('景点门票销售管理系统')
		self.window.geometry('900x900') # 这里的乘是小x
		self.window.geometry('+330+0')
 
		label = Label(self.window, text="景点门票销售管理系统", font=("Verdana", 40))
		label.pack(pady=100)  # pady=100 界面的高度
 
		Button(self.window, text="管理员登陆", font=tkFont.Font(size=25), command=lambda: AdminPage(self.window), width=60, height=2,
			   fg='white', bg='gray', activebackground='black', activeforeground='white').pack()
		Button(self.window, text="游客登陆", font=tkFont.Font(size=25), command=lambda: TourPage(self.window), width=60,
			   height=2,fg='white', bg='gray', activebackground='black', activeforeground='white').pack()
		Button(self.window, text="用户注册", font=tkFont.Font(size=25), command=lambda: SignPage(self.window), width=60,
			   height=2,fg='white', bg='gray', activebackground='black', activeforeground='white').pack()	   
		Button(self.window, text="关于", font=tkFont.Font(size=25), command=lambda: AboutPage(self.window), width=60, height=2,
			   fg='white', bg='gray', activebackground='black', activeforeground='white').pack()
		Button(self.window, text='退出系统', height=2, font=tkFont.Font(size=25), width=60, command=self.window.destroy,
			   fg='white', bg='gray', activebackground='black', activeforeground='white').pack()
 
		self.window.mainloop() # 主消息循环


#管理员登陆页面
class AdminPage:
	def __init__(self, parent_window):
		parent_window.destroy() # 销毁主界面
 
		self.window = tk.Tk()  # 初始框的声明
		self.window.title('管理员登陆页面')
		self.window.geometry('900x900')  # 这里的乘是小x
		self.window.geometry('+330+0')
 
		label = tk.Label(self.window, text='管理员登陆', bg='green', font=('Verdana', 30), width=40, height=3)
		label.pack()
 
		Label(self.window, text='管理员账号:', font=tkFont.Font(size=20)).pack(pady=25)
		self.admin_username = tk.Entry(self.window, width=30, font=tkFont.Font(size=20), bg='Ivory')
		self.admin_username.pack()
 
		Label(self.window, text='管理员密码:', font=tkFont.Font(size=20)).pack(pady=25)
		self.admin_pass = tk.Entry(self.window, width=30, font=tkFont.Font(size=20), bg='Ivory', show='*')
		self.admin_pass.pack()
 
		Button(self.window, text="登陆", width=10, font=tkFont.Font(size=15), command=self.login).pack(pady=40)
		Button(self.window, text="返回", width=10, font=tkFont.Font(size=15), command=self.back).pack()
 
		self.window.protocol("WM_DELETE_WINDOW", self.back)  # 捕捉右上角关闭点击
		self.window.mainloop()  # 进入消息循环
 
	def login(self):
		#从界面获取的数据
		print(str(self.admin_username.get()))
		print(str(self.admin_pass.get()))
		admin_pass = None
 
		# 数据库操作 查询管理员表
		con = pymysql.connect(host='localhost',port=3306,user='root',passwd='CYFF',db='景点门票销售管理系统',charset='utf8')  # 打开数据库连接
		cursor = con.cursor()  # 使用cursor()方法获取操作游标
		sql = "SELECT * FROM 管理员信息 WHERE 管理员账号 = '%s'" % (self.admin_username.get())  # SQL 查询语句
		try:
		# 执行SQL语句
			cursor.execute(sql)
			# 获取所有记录列表
			results = cursor.fetchall()
			for row in results:
				admin_id = row[0]
				admin_pass = row[1]
				# 打印结果
				print("admin_id=%s,admin_pass=%s" % (admin_id, admin_pass))
		except:
			print("Error: unable to fecth data")
			messagebox.showinfo('警告!', '用户名或密码不正确!')
		con.close()  # 关闭数据库连接
 
		print("正在登陆管理员管理界面")
		#从界面获取的数据
		print("self",self.admin_pass)
		#数据库里的数据
		print("local",admin_pass)

		# 判断密码,正确则进入管理员操作界面
		if self.admin_pass.get() == admin_pass:
			TicketMessage(self.window)  
		else:
			messagebox.showinfo('警告!', '用户名或密码不正确!')
 
	def back(self):
		StartPage(self.window) # 显示主窗口 销毁本窗口


#游客登陆页面
class TourPage:
	def __init__(self, parent_window):
		parent_window.destroy() # 销毁主界面
 
		self.window = tk.Tk()  # 初始框的声明
		self.window.title('游客登陆')
		self.window.geometry('900x900')  # 这里的乘是小x
		self.window.geometry('+330+0')
 
		label = tk.Label(self.window, text='游客登陆', bg='green', font=('Verdana', 30), width=40, height=3)
		label.pack()
 
		Label(self.window, text='游客账号:', font=tkFont.Font(size=20)).pack(pady=25)
		self.tour_id = tk.Entry(self.window, width=30, font=tkFont.Font(size=20), bg='Ivory')
		self.tour_id.pack()
 
		Label(self.window, text='游客密码:', font=tkFont.Font(size=20)).pack(pady=25)
		self.tour_pass = tk.Entry(self.window, width=30, font=tkFont.Font(size=20), bg='Ivory', show='*')
		self.tour_pass.pack()
 
		Button(self.window, text="登陆", width=10, font=tkFont.Font(size=15), command=self.login).pack(pady=40)
		Button(self.window, text="返回", width=10, font=tkFont.Font(size=15), command=self.back).pack()
 
		self.window.protocol("WM_DELETE_WINDOW", self.back)  # 捕捉右上角关闭点击
		self.window.mainloop()  # 进入消息循环
 
	def login(self):
		print(str(self.tour_id.get()))
		print(str(self.tour_pass.get()))
		tou_pass = None
 
		# 数据库操作 查询管理员表
		con = pymysql.connect(host='localhost',port=3306,user='root',passwd='CYFF',db='景点门票销售管理系统',charset='utf8')  # 打开数据库连接
		cursor = con.cursor()  # 使用cursor()方法获取操作游标
		sql = "SELECT * FROM 游客信息 WHERE 游客账号 = '%s'" % (self.tour_id.get())  # SQL 查询语句
		try:
			# 执行SQL语句
			cursor.execute(sql)
			# 获取所有记录列表
			results = cursor.fetchall()
			for row in results:
				tou_id = row[0]
				tou_pass = row[1]
				# 打印结果
				print("tou_id=%s,tou_pass=%s" % (tou_id, tou_pass))
		except:
			print("Error: unable to fecth data")
			messagebox.showinfo('警告!', '用户名或密码不正确!')
		con.close()  # 关闭数据库连接
 
		print("正在登陆.....")
		print("self", self.tour_pass.get())
		print("local", tou_pass)

		# 进入门票信息查看界面
		if self.tour_pass.get() == tou_pass:
			TourKit(self.window) 
		else:
			messagebox.showinfo('警告!', '用户名或密码不正确!')
 
	def back(self):
		StartPage(self.window)  # 显示主窗口 销毁本窗口


#用户注册页面
class SignPage:
	def __init__(self, parent_window):
		#parent_window.update()
		parent_window.destroy() # 销毁子界面
 
		self.window = tk.Tk()  # 初始框的声明
		self.window.title('注册')
		self.window.geometry('900x900')  # 这里的乘是小x
		self.window.geometry('+330+0')
 
		label = Label(self.window, text="注册页面", font=("Verdana", 40))
		label.pack(pady=100)  # pady=100 界面的长度
 
		Button(self.window, text="管理员注册", font=tkFont.Font(size=25), command=lambda: AdminSign(self.window), width=60, height=2,
			   fg='white', bg='gray', activebackground='black', activeforeground='white').pack()
		Button(self.window, text="游客注册", font=tkFont.Font(size=25), command=lambda: TourSign(self.window), width=60,
			   height=2,fg='white', bg='gray', activebackground='black', activeforeground='white').pack()
		Button(self.window, text="返回首页", font=tkFont.Font(size=25), command= self.back, width=60,
			   height=2,fg='white', bg='gray', activebackground='black', activeforeground='white').pack()


		self.window.protocol("WM_DELETE_WINDOW", self.back)  # 捕捉右上角关闭点击
        
		self.window.mainloop()  # 进入消息循环

	def back(self):
		StartPage(self.window)  # 显示主窗口 销毁本窗口

#管理员
#管理员注册操作
class AdminSign:
	def __init__(self, parent_window):
		parent_window.destroy() # 销毁主界面
 
		self.window = tk.Tk()  # 初始框的声明
		self.window.title('管理员注册页面')
		self.window.geometry('900x900')  # 这里的乘是小x
		self.window.geometry('+330+0')
		
		self.admin_userid = [] #账号
		self.admin_pass = [] #密码
		self.admin_name = [] #姓名
		self.admin_sex = [] #性别
		self.admin_phone = [] #电话
		self.admin_jid = [] #工号
 
		# 打开数据库连接
		con = pymysql.connect(host='localhost',port=3306,user='root',passwd='CYFF',db='景点门票销售管理系统',charset='utf8')
		cursor = con.cursor()  # 使用cursor()方法获取操作游标
		sql = "SELECT * FROM 管理员信息"  # SQL 查询语句
		try:
			# 执行SQL语句
			cursor.execute(sql)
			# 获取所有记录列表
			results = cursor.fetchall()
			for row in results:
				self.admin_userid.append(row[0])
				self.admin_pass.append(row[1])
				self.admin_name.append(row[2])
				self.admin_sex.append(row[3])
				self.admin_phone.append(row[4])
				self.admin_jid.append(row[5])
				# print(self.admin_userid)
				# print(self.admin_name)
				# print(self.admin_sex)
				# print(self.admin_jid)
		except:
			print("Error: unable to fetch data")
			messagebox.showinfo('警告!', '数据库连接失败!')
		con.close()# 关闭数据库连接


		self.var_userid = StringVar()  # 声明管理员账号
		self.var_pass = StringVar()  # 声明管理员密码
		self.var_name = StringVar()  # 声明管理员姓名
		self.var_sex = StringVar()  # 声明管理员性别
		self.var_phone = StringVar()  # 声明管理员电话
		self.var_jid = StringVar()  # 声明管理员工号

		label = tk.Label(self.window, text='管理员注册', bg='green', font=('Verdana', 30), width=40, height=3)
		label.pack()
 
		Label(self.window, text='账号:', font=tkFont.Font(size=15)).pack(pady=10,anchor='w')
		self.var_userid = tk.Entry(self.window, width=40, font=tkFont.Font(size=20), bg='Ivory')
		self.var_userid.pack(anchor='nw')
 
		Label(self.window, text='密码:', font=tkFont.Font(size=15)).pack(pady=10,anchor='w')
		self.var_pass = tk.Entry(self.window, width=40, font=tkFont.Font(size=20), bg='Ivory', show='*')
		self.var_pass.pack(anchor='nw')

		Label(self.window, text='姓名:', font=tkFont.Font(size=15)).pack(pady=10,anchor='w')
		self.var_name = tk.Entry(self.window, width=40, font=tkFont.Font(size=20), bg='Ivory')
		self.var_name.pack(anchor='nw')

		Label(self.window, text='性别:', font=tkFont.Font(size=15)).pack(pady=10,anchor='w')
		self.var_sex = tk.Entry(self.window, width=40, font=tkFont.Font(size=20), bg='Ivory')
		self.var_sex.pack(anchor='nw')

		Label(self.window, text='联系电话:', font=tkFont.Font(size=15)).pack(pady=10,anchor='w')
		self.var_phone = tk.Entry(self.window, width=40, font=tkFont.Font(size=20), bg='Ivory')
		self.var_phone.pack(anchor='nw')

		Label(self.window, text='工号:', font=tkFont.Font(size=15)).pack(pady=10,anchor='w')
		self.var_jid = tk.Entry(self.window, width=40, font=tkFont.Font(size=20), bg='Ivory')
		self.var_jid.pack(anchor='nw')

		Button(self.window, text="注册", width=20, font=tkFont.Font(size=15), command=self.new_row).pack(pady=20)
		Button(self.window, text="返回", width=20, font=tkFont.Font(size=15), command=self.back).pack(pady=20)
 
		self.window.protocol("WM_DELETE_WINDOW", self.back)  # 捕捉右上角关闭点击
		self.window.mainloop()  # 进入消息循环

	def new_row(self):

		#从界面获取的数据
		print(self.var_userid.get())
		print(self.admin_userid)

		if str(self.var_userid.get()) in self.admin_userid:
			messagebox.showinfo('警告!', '该用户已存在!')
		elif (len(str(self.var_phone.get())) > 15) or (len(str(self.var_phone.get())) < 7):
			messagebox.showinfo('警告!', '请输入正确的电话号码!')
		else:
			if self.var_userid.get() != '' and self.var_pass.get() != '' and self.var_name.get() != '' and self.var_sex.get() != '' and self.var_phone.get() != '' and self.var_jid.get() != '':
				# 打开数据库连接
				con = pymysql.connect(host='localhost',port=3306,user='root',passwd='CYFF',db='景点门票销售管理系统',charset='utf8')
				cursor = con.cursor()  # 使用cursor()方法获取操作游标
				sql = "INSERT INTO 管理员信息(管理员账号,管理员密码,管理员姓名,管理员性别,联系电话,工号) \
				       VALUES ('%s', '%s', '%s', '%s', '%s', '%s')" % \
					  (self.var_userid.get(), self.var_pass.get(),self.var_name.get(), self.var_sex.get(),self.var_phone.get(), self.var_jid.get())  # SQL 插入语句
				try:
					cursor.execute(sql)  # 执行sql语句
					con.commit()  # 提交到数据库执行
				except:
					con.rollback()  # 发生错误时回滚
					messagebox.showinfo('警告!', '数据库连接失败!')
				con.close()  # 关闭数据库连接
 
				self.admin_userid.append(self.var_userid.get())
				self.admin_pass.append(self.var_pass.get())
				self.admin_name.append(self.var_name.get())
				self.admin_sex.append(self.var_sex.get())
				self.admin_phone.append(self.var_phone.get())
				self.admin_jid.append(self.var_jid.get())

				messagebox.showinfo('提示!', '注册成功!')
			else:
				messagebox.showinfo('警告!', '请填写正确的信息')

	def back(self):
		StartPage(self.window) # 显示主窗口 销毁本窗口



		
#游客
# 游客注册界面
class TourSign:
	def __init__(self, parent_window):
		parent_window.destroy() # 销毁主界面
 
		self.window = tk.Tk()  # 初始框的声明
		self.window.title('游客注册页面')
		self.window.geometry('900x900')  # 这里的乘是小x
		self.window.geometry('+330+0')
		
		self.admin_userid = [] #账号
		self.admin_pass = [] #密码
		self.admin_name = [] #姓名
		self.admin_sex = [] #性别
		self.admin_phone = [] #电话
		
 
		# 打开数据库连接
		con = pymysql.connect(host='localhost',port=3306,user='root',passwd='CYFF',db='景点门票销售管理系统',charset='utf8')
		cursor = con.cursor()  # 使用cursor()方法获取操作游标
		sql = "SELECT * FROM 游客信息"  # SQL 查询语句
		try:
			# 执行SQL语句
			cursor.execute(sql)
			# 获取所有记录列表
			results = cursor.fetchall()
			for row in results:
				self.admin_userid.append(row[0])
				self.admin_pass.append(row[1])
				self.admin_name.append(row[2])
				self.admin_sex.append(row[3])
				self.admin_phone.append(row[4])
	
		except:
			print("Error: unable to fetch data")
			messagebox.showinfo('警告!', '数据库连接失败!')
		con.close()# 关闭数据库连接


		self.var_userid = StringVar()  # 声明管理员账号
		self.var_pass = StringVar()  # 声明管理员密码
		self.var_name = StringVar()  # 声明管理员姓名
		self.var_sex = StringVar()  # 声明管理员性别
		self.var_phone = StringVar()  # 声明管理员电话

		label = tk.Label(self.window, text='游客注册', bg='green', font=('Verdana', 30), width=40, height=3)
		label.pack()
 
		Label(self.window, text='账号:', font=tkFont.Font(size=15)).pack(pady=10,anchor='w')
		self.var_userid = tk.Entry(self.window, width=40, font=tkFont.Font(size=20), bg='Ivory')
		self.var_userid.pack(anchor='nw')
 
		Label(self.window, text='密码:', font=tkFont.Font(size=15)).pack(pady=10,anchor='w')
		self.var_pass = tk.Entry(self.window, width=40, font=tkFont.Font(size=20), bg='Ivory', show='*')
		self.var_pass.pack(anchor='nw')

		Label(self.window, text='姓名:', font=tkFont.Font(size=15)).pack(pady=10,anchor='w')
		self.var_name = tk.Entry(self.window, width=40, font=tkFont.Font(size=20), bg='Ivory')
		self.var_name.pack(anchor='nw')

		Label(self.window, text='性别:', font=tkFont.Font(size=15)).pack(pady=10,anchor='w')
		self.var_sex = tk.Entry(self.window, width=40, font=tkFont.Font(size=20), bg='Ivory')
		self.var_sex.pack(anchor='nw')
	
		Label(self.window, text='联系电话:', font=tkFont.Font(size=15)).pack(pady=10,anchor='w')
		self.var_phone
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值