Python大作业 校园导游程序的设计
4.2源代码
- Py
import tkinter as tk
from PIL import ImageTk, Image
from ConnSql import select # 用于链接数据库并进行查询
from 大作业.line import selectline
def clear_text():
text.delete(1.0, tk.END) # 清空回显框的内容
def submit_location():
text.delete(1.0, tk.END) # 删除回显框中的所有文本
name = entry.get() # 获取输入框的文本内容
des = "简介:"+select(name)
location = entry.get()
text.insert(tk.END,des)
print("输入的地点是:", location)
def loca_get():
text.delete(1.0, tk.END) # 删除回显框中的所有文本
location="第一教学楼"
name = select(location)
des="当前位置:"+location+"\n"+"简介:"+name
text.insert(tk.END,des)
def line():
text.delete(1.0, tk.END) # 删除回显框中的所有文本
line = entry2.get()
print(line)
des = "路线:"+selectline(line)
text.insert(tk.END, des)
print(des)
root = tk.Tk()
root.title("石家庄铁道大学校园导游程序:")
# 设置背景图片路径
background_image_path = "STD.jpg"
# 加载背景图片
background_image = Image.open(background_image_path)
background_image = background_image.resize((500, 500)) # 调整图片大小
photo = ImageTk.PhotoImage(background_image)
# 创建画布
canvas = tk.Canvas(root, width=800, height=500)
canvas.create_image(0, 0, anchor=tk.NW, image=photo)
canvas.pack()
# 创建回显框
text = tk.Text(root)
text.place(x=10, y=root.winfo_height() - 210, width=30, height=20) # 设置回显框的位置和大小
text.pack()
#说明
label = tk.Label(root, text="请输入地点:")
label.place(relx=0.8, rely=0.2, anchor=tk.E) # 设置相对坐标
# 创建文本输入框
entry = tk.Entry(root)
entry.place(relx=0.9, rely=0.2, anchor=tk.CENTER) # 设置相对坐标
# 创建提交按钮
submit_button = tk.Button(root, text="地点查询", command=submit_location)
submit_button.place(relx=0.8, rely=0.26, anchor=tk.CENTER) # 设置相对坐标
# 创建提交按钮2
submit_button2 = tk.Button(root, text="当前位置查询", command=loca_get)
submit_button2.place(relx=0.8, rely=0.4, anchor=tk.CENTER) # 设置相对坐标
#说明2
label2 = tk.Label(root, text="请输入目的地:")
label2.place(relx=0.78, rely=0.5, anchor=tk.E) # 设置相对坐标
# 创建文本输入框2
entry2 = tk.Entry(root)
entry2.place(relx=0.88, rely=0.5, anchor=tk.CENTER) # 设置相对坐标
print(entry2.get())
# 创建提交按钮3
submit_button3 = tk.Button(root, text="目的地查询", command=line)
submit_button3.place(relx=0.96, rely=0.5, anchor=tk.CENTER) # 设置相对坐标
# 创建清空按钮,点击清空回显框的内容
clear_button = tk.Button(root, text="清空", command=clear_text)
clear_button.pack()
root.mainloop()
Connsql.py
import mysql.connector
def select(name):
# 连接MySQL数据库
cnx = mysql.connector.connect(
host="localhost", # 主机名
user="root", # 用户名
password="sqlsql", # 密码
database="db1" # 数据库名
)
# 创建游标对象
cursor = cnx.cursor()
# 执行查询语句
query = "SELECT introduce FROM tb_std where name=" + "'" + name + "'"
cursor.execute(query)
# 获取查询结果
result = cursor.fetchall()
# 处理查询结果
des = "输入有误"
for row in result:
des= row[0]
print(des)
# 关闭游标和数据库连接
cursor.close()
cnx.close()
return des
line.py
import mysql.connector
def selectline(end):
cnx = mysql.connector.connect(
host="localhost", # 主机名
user="root", # 用户名
password="sqlsql", # 密码
database="db1" # 数据库名
)
# 创建游标对象
cursor = cnx.cursor()
# 执行查询语句
query = "SELECT line FROM tb_line where end=" + "'" + end + "'"
cursor.execute(query)
# 获取查询结果
result = cursor.fetchall()
# 处理查询结果
des = "查询失败"
for row in result:
des = row[0]
# 关闭游标和数据库连接
cursor.close()
cnx.close()
return des