python3 优惠券查询GUI程序

from tkinter import ttk
from tkinter import messagebox
import pymssql
import tkinter
import decimal
import time
#定义数据库连接函数
def da(str):
conn = pymssql.connect(host=' 数据库地址',database='数据库名称',user='用户账号',password='用户密码')
cur = conn.cursor()
cur.execute(str)
row = cur.fetchall()
return row
#定义treeview控件子节点数据清空
def dletebutton(tree):
x = tree.get_children()
for item in x:
tree.delete(item)
#定义查询函数
def det():
phone = ent.get()#获取文本框内容传给变量phone 保存
vip = ent1.get()#获取文本框内容传给变量vip保存
tim = (time.strftime('%Y-%m-%d', time.localtime()))#获取当前系统时间以%Y-%m-%d 格式输出
if phone == ''and vip == '':
tkinter.messagebox.showerror('警告:', '手机号码、会员号不能为空')
elif phone == '':
dletebutton(tree)
ent1.delete(0,'end')
sql1 = '''select khm013.khhykh,khm013.xjqbh,xjqms,xjqmz,xfmeje,syqsrq,syjzrq
from khm013 inner join khm17 on khm013.xjqbh=khm17.xjqbh
inner join khm01 on khm013.khhykh=khm01.khhykh
where khxfzt=\'n\' and syjzrq>=\'%s\' and khm013.khhykh=\'%s\'''' % (tim, vip)
a = da(sql1)
for i in a:
tree.insert('', 'end', values=i)
elif vip == '':
dletebutton(tree)
ent.delete(0,'end')
sql2 = '''select khm013.khhykh,khm013.xjqbh,xjqms,xjqmz,xfmeje,syqsrq,syjzrq from
khm013 inner join khm17 on khm013.xjqbh=khm17.xjqbh
inner join khm01 on khm013.khhykh=khm01.khhykh
where khxfzt=\'n\' and syjzrq>=\'%s\' and khm01.khhysj=\'%s\'''' % (tim, phone)
b = da(sql2)
for i in b:
tree.insert('', 'end', values=i)
else:
tkinter.messagebox.showerror('警告:', '手机号码、会员号只能选其一查询')

root = tkinter.Tk()
root.title('优惠券查询')
lab = ttk.Label(root,text='优惠券查询',font=('黑体',20))
lab.grid(row=0,columnspan=5)

lab1 = ttk.Label(root,text='手机号码:')
lab1.grid(row=1,column=0)

ent = ttk.Entry(root,text='手机号码')
ent.grid(row=1,column=1)

lab2 = ttk.Label(root,text='会员号码:')
lab2.grid(row=1,column=2)

ent1 = ttk.Entry(root,text='会员号码')
ent1.grid(row=1,column=3)

but = ttk.Button(root,text='确定',command=det)
but.grid(row=1,column=4)

#使用tkinter编写界面
tree = ttk.Treeview(root,columns=['会员号码','优惠券ID','优惠券描述','优惠券面值','满多少可使用','起始日期','截止日期'],show = 'headings')
tree['columns']=('会员号','优惠券ID','优惠券描述','优惠券面值','满多少可使用','起始日期','截止日期')
tree['height']=15
tree.column('会员号',width=100,anchor='center')
tree.column('优惠券ID',width=100,anchor='center')
tree.column('优惠券描述',width=100,anchor='center')
tree.column('优惠券面值',width=100,anchor='center')
tree.column('满多少可使用',width=100,anchor='center')
tree.column('起始日期',width=100,anchor='center')
tree.column('截止日期',width=100,anchor='center')
tree.heading('会员号',text='会员号')
tree.heading('优惠券ID',text='优惠券ID')
tree.heading('优惠券描述',text='优惠券描述')
tree.heading('优惠券面值',text='优惠券面值')
tree.heading('满多少可使用',text='满多少可使用')
tree.heading('起始日期',text='起始日期')
tree.heading('截止日期',text='截止日期')
tree.grid(row=2,columnspan=5)
root.mainloop()

 

--------最后界面浏览----------

 

posted @ 2019-10-17 23:24  Demo_one  阅读(339)  评论(0编辑  收藏  举报