Python桌面应用之XX学院水卡报表查询系统(Tkinter+cx_Oracle)

本文主要是介绍Python桌面应用之XX学院水卡报表查询系统(Tkinter+cx_Oracle),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、功能样式

Python桌面应用之XX学院水卡报表查询系统功能:

连接Oracle数据库,查询XX学院水卡操作总明细报表,汇总数据报表,个人明细报表,进行预览并且支持导出报表

1.总明细报表样式
明细
2.汇总明细样式
汇总明细

3.个人明细样式
个人明细
4.导出报表样式
导出
5.错误提示样式
tip
tip2

二、核心点

1. 安装cx_Oracle:使用cx_Oracle三方库连接Oracle,该库使用的python版本略低,可以在[https://cx-oracle.readthedocs.io/en/latest/](https://cx-oracle.readthedocs.io/en/latest/进行查询,安装前先确定:python版本、Orale客户端版本(要不都是64位,要不都是32位),安装cx_Oracle的版本位数是跟python的位数相关的。
使用代码进行测试

import cx_Oracle
# 账户  密码  ip:1521/实例名
conn = cx_Oracle.connect('system','Yxq123456','127.0.0.1:1521/ecard'
# 挂载数据库连接游标
self.cursor = conn.cursor()
print('连接数据库成功!')

2. 多参数查询Sql: sql语句使用:参数名来定义参数,多参数使用cursor.execute(sql,(参数1,参数2)).fetchall()来查询

sql = "select a.outid ,a.name ,b.opfare,b.opdt,b.dscrp from base_customers a,rec_cust_acc b where a.customerid = b. customerid and b.opdt >= to_date(:preopdt,'yyyy-MM-dd HH24:mi:ss') and b.opdt <= to_date(:nextopdt,'yyyy-MM-dd HH24:mi:ss') order by b.opdt desc"
preopdt=self.pretimeInput.get()
nextopdt=self.nexttimeInput.get()
data = self.cursor.execute(sql,(preopdt,nextopdt)).fetchall()

**3. Treeview表格组件的使用:**这里使用了三个报表,其实可以将打开的Treeview做成一个表格class类,要使用的时候直接生成要使用的对象,传入该对象的大小,heading标题,data数据即可。

# 明细查询def Consumedetail(self):self.consumedetail = tk.Tk()self.consumedetail.title('XX学院明细查询')self.consumedetail.geometry("1000x600")# 加载滚动条scrollBar = tk.Scrollbar(self.consumedetail)scrollBar.pack(side = tk.RIGHT,fill = tk.Y)self.tree = ttk.Treeview(self.consumedetail, columns=('outid', 'name', 'opfare', 'opdt','dscrp'), show="headings", displaycolumns="#all",yscrollcommand = scrollBar.set)self.tree.pack()self.tree.heading('outid', text="学号", anchor=tk.W)self.tree.heading('name', text="姓名", anchor=tk.W)self.tree.heading('opfare', text="交易金额", anchor=tk.W)self.tree.heading('opdt', text="交易日期", anchor=tk.W)self.tree.heading('dscrp', text="交易类型", anchor=tk.W)# 设置关联scrollBar.config(command = self.tree.yview)# 每次打开清空页面for item in self.tree.get_children():self.consumedetail.tree.delete(item)sql = "select a.outid ,a.name ,b.opfare,b.opdt,b.dscrp from base_customers a,rec_cust_acc b where a.customerid = b. customerid and b.opdt >= to_date(:preopdt,'yyyy-MM-dd HH24:mi:ss') and b.opdt <= to_date(:nextopdt,'yyyy-MM-dd HH24:mi:ss') order by b.opdt desc"preopdt=self.pretimeInput.get()nextopdt=self.nexttimeInput.get()data = self.cursor.execute(sql,(preopdt,nextopdt)).fetchall()# print(data)# data = [['2013090101','张三','100','2023-10-19','PC存款']]for itm in data:self.tree.insert("",tk.END,values=itm)self.tree.pack(padx=10,pady=10, fill=tk.BOTH,expand=1)exportbtn = tk.Button(self.consumedetail,text='导出',width=8,command=self.export).pack()

4. 导出数据自定义文件名:报表里面导出数据,其实使用遍历treeview组件数据,在进行整理后写入csv文件,自定义文件名是使用filedialog.asksaveasfilename来打开文件框,里面的文件类型使用参数filetypes ,输入文件名后获取名称生成文件。这里导出的文件就只是csv文件,如果需要其他文件类型,可以自行导入其他三方库。

     def export(self):# 导出export        # 打开文件夹选择对话框# 更新标签文本# print(folder_path)list = []columns = []# 获取表格内容idfor row_id in self.tree.get_children():list.append(self.tree.item(row_id)['values'])print(len(self.tree.get_children()))   # 通过第一行获取列数生成标题# print(self.tree.item)if len(self.tree.get_children()) != 0:print('ok')folder_path = filedialog.asksaveasfilename(title='请选择一个文件',filetypes=[("CSV", ".csv")]) for i in range(0,len(self.tree.item('I001')['values'])):columns.append(self.tree.heading(column=i)['text'])# 导出csvwith open(f'{folder_path}.csv','w',newline='') as csvfile:fieldnames = columnswriter = csv.writer(csvfile)writer.writerow(fieldnames)writer.writerows(list)else:messagebox.showwarning("提示", "没有数据,无法导出")return

5.遍历Treeview表格数据与标题:获取Treeview里面的数据与标题,这里现获取id值,然后通过item获取[‘values’]值,获取标题这里先遍历了第一行有多少数据,然后使用self.tree.heading(column=i)['text']来获取标题。

 # 获取表格内容id
for row_id in self.tree.get_children():list.append(self.tree.item(row_id)['values'])
 # 通过第一行获取列数生成标题
for i in range(0,len(self.tree.item('I001')['values'])):columns.append(self.tree.heading(column=i)['text'])

三、完整代码

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import cx_Oracle
import time
import csv
from tkinter import filedialog# mainapp
class mainApp(object):def __init__(self,parent,**kwargs):self.root = parentcurrent_timestamp = time.time()# 将时间戳转换为本地时间的struct_time对象local_time = time.localtime(current_timestamp)# 使用strftime()方法将struct_time对象格式化为指定的时间字符串  # 挂在self时间self.pretime = time.strftime("%Y-%m-%d 00:00:00", local_time)self.nexttime = time.strftime("%Y-%m-%d %H:%M:%S", local_time)conn = cx_Oracle.connect('system','Yxq123456','127.0.0.1:1521/ecard')# conn = cx_Oracle.connect('ccense','XCXY123*','127.0.0.1:1521/ecard')# 挂载数据库连接游标self.cursor = conn.cursor()print('连接数据库成功!')self.root.config(**kwargs)self.root.title('XX学院')self.root.resizable(False, False)self.create_widgets()# 获取屏幕尺寸screen_width = self.root.winfo_screenwidth()screen_height = self.root.winfo_screenheight()# 确定窗口位置,并设置大小x_coordinate = (screen_width / 2) - 300 # 300是窗口的宽度y_coordinate = (screen_height / 2) - 200 # 200是窗口的高度self.root.geometry('650x400+{}+{}'.format(int(x_coordinate), int(y_coordinate)))# self.root.geometry("600x400")# 创建UIdef create_widgets(self):self.titleLab = tk.Label(self.root,text='XX学院水卡报表管理系统',font=("kaiti",18)).place(x=190,y=30)self.outidLab = tk.Label(self.root,text='学号:').place(x=80,y=100)self.outidInput = tk.Entry(self.root, width=20)self.outidInput.place(x=130,y=100)# 姓名# 学号self.nameLab = tk.Label(self.root,text='姓名:').place(x=380,y=100)self.nameInput = tk.Entry(self.root,width=20)self.nameInput.place(x=430,y=100)# 起始时间self.mustLabel1 = tk.Label(self.root,text='*',font=('Arial', 16),fg = 'red').place(x=45,y=160)self.pretimeLab = tk.Label(self.root,text='起始时间:').place(x=55,y=160)self.pretimeInput = tk.Entry(self.root, width=20)self.pretimeInput.place(x=130,y=160)self.pretimeInput.insert(0,self.pretime)# 终始时间self.mustLabel2 = tk.Label(self.root,text='*',font=('Arial', 16),fg = 'red').place(x=350,y=160)self.nexttimeLab = tk.Label(self.root,text='终止时间:').place(x=360,y=160)self.nexttimeInput = tk.Entry(self.root,width=20)self.nexttimeInput.place(x=430,y=160)self.nexttimeInput.insert(0,self.nexttime)self.consumeBtn = tk.Button(self.root,text='明细查询',command=self.Consumedetail,width=10).place(x=130,y=260)self.sumBtn = tk.Button(root,text='汇总查询',command=self.sumDetail,width=10).place(x=300,y=260)self.personBtn = tk.Button(root,text='个人查询',command=self.personDetail,width=10).place(x=480,y=260)# 明细查询def Consumedetail(self):self.consumedetail = tk.Tk()self.consumedetail.title('XX学院明细查询')self.consumedetail.geometry("1000x600")# 加载滚动条scrollBar = tk.Scrollbar(self.consumedetail)scrollBar.pack(side = tk.RIGHT,fill = tk.Y)self.tree = ttk.Treeview(self.consumedetail, columns=('outid', 'name', 'opfare', 'opdt','dscrp'), show="headings", displaycolumns="#all",yscrollcommand = scrollBar.set)self.tree.pack()self.tree.heading('outid', text="学号", anchor=tk.W)self.tree.heading('name', text="姓名", anchor=tk.W)self.tree.heading('opfare', text="交易金额", anchor=tk.W)self.tree.heading('opdt', text="交易日期", anchor=tk.W)self.tree.heading('dscrp', text="交易类型", anchor=tk.W)# 设置关联scrollBar.config(command = self.tree.yview)# 每次打开清空页面for item in self.tree.get_children():self.consumedetail.tree.delete(item)sql = "select a.outid ,a.name ,b.opfare,b.opdt,b.dscrp from base_customers a,rec_cust_acc b where a.customerid = b. customerid and b.opdt >= to_date(:preopdt,'yyyy-MM-dd HH24:mi:ss') and b.opdt <= to_date(:nextopdt,'yyyy-MM-dd HH24:mi:ss') order by b.opdt desc"preopdt=self.pretimeInput.get()nextopdt=self.nexttimeInput.get()data = self.cursor.execute(sql,(preopdt,nextopdt)).fetchall()# print(data)# data = [['2013090101','张三','100','2023-10-19','PC存款']]for itm in data:self.tree.insert("",tk.END,values=itm)self.tree.pack(padx=10,pady=10, fill=tk.BOTH,expand=1)exportbtn = tk.Button(self.consumedetail,text='导出',width=8,command=self.export).pack()# 汇总查询def sumDetail(self):sql = "select sum(opfare),count(acccode),dscrp from rec_cust_acc where opdt >= to_date(:preopdt,'yyyy-MM-dd HH24:mi:ss') and opdt <= to_date(:nextopdt,'yyyy-MM-dd HH24:mi:ss') group by dscrp"self.sumtail = tk.Tk()self.sumtail.title('XX学院汇总查询')self.sumtail.geometry("800x600")# 加载滚动条# exportbtn = Button(sumtail,text='导出',width=8,command=export).pack()scrollBar = tk.Scrollbar(self.sumtail)scrollBar.pack(side = tk.RIGHT,fill = tk.Y)self.tree = ttk.Treeview(self.sumtail, columns=('sum', 'count', 'dscrp'), show="headings", displaycolumns="#all",yscrollcommand = scrollBar.set)self.tree.pack()self.tree.heading('sum', text="总金额", anchor=tk.W)self.tree.heading('count', text="总次数", anchor=tk.W)self.tree.heading('dscrp', text="交易类型", anchor=tk.W)# 设置关联scrollBar.config(command = self.tree.yview)# 每次打开清空页面for item in self.tree.get_children():self.tree.delete(item)# sql = "select a.outid ,a.name ,b.opfare,b.opdt,b.dscrp from base_customers a,rec_cust_acc b where a.customerid = b. customerid and b.opdt >= to_date(:preopdt,'yyyy-MM-dd HH24:mi:ss') and b.opdt <= to_date(:nextopdt,'yyyy-MM-dd HH24:mi:ss') order by b.opdt desc"preopdt=self.pretimeInput.get()nextopdt=self.nexttimeInput.get()data = self.cursor.execute(sql,(preopdt,nextopdt)).fetchall()# print(data)for itm in data:self.tree.insert("",tk.END,values=itm)self.tree.pack(padx=10,pady=10, fill=tk.BOTH,expand=1)exportbtn = tk.Button(self.sumtail,text='导出',width=8,command=self.export).pack()# 个人明细def personDetail(self):if(self.outidInput.get()):print('outid not is null')sql="select a.outid ,a.name ,b.opfare,b.oddfare,b.opdt,b.dscrp from base_customers a,rec_cust_acc b where a.customerid = b. customerid and b.opdt >= to_date(:preopdt,'yyyy-MM-dd HH24:mi:ss') and b.opdt <= to_date(:nextopdt,'yyyy-MM-dd HH24:mi:ss') and a.outid = :outid order by b.opdt desc"outidname = self.outidInput.get()elif(self.nameInput.get()):sql="select a.outid ,a.name ,b.opfare,b.oddfare,b.opdt,b.dscrp from base_customers a,rec_cust_acc b where a.customerid = b. customerid and b.opdt >= to_date(:preopdt,'yyyy-MM-dd HH24:mi:ss') and b.opdt <= to_date(:nextopdt,'yyyy-MM-dd HH24:mi:ss') and a.name like :name order by b.opdt desc"outidname = self.nameInput.get()else:messagebox.showwarning("提示", "请输入学号或者姓名!")returnself.persontail = tk.Tk()self.persontail.title('XX学院个人查询')self.persontail.geometry("1200x600")# 加载滚动条# exportbtn = Button(persontail,text='导出',width=8,command=export).pack()scrollBar = tk.Scrollbar(self.persontail)scrollBar.pack(side = tk.RIGHT,fill = tk.Y)self.tree = ttk.Treeview(self.persontail, columns=('outid', 'name', 'opfare','oddfare', 'opdt','dscrp'), show="headings", displaycolumns="#all",yscrollcommand = scrollBar.set)self.tree.pack()self.tree.heading('outid', text="学号", anchor=tk.W)self.tree.heading('name', text="姓名", anchor=tk.W)self.tree.heading('opfare', text="交易金额", anchor=tk.W)self.tree.heading('oddfare', text="账户余额", anchor=tk.W)self.tree.heading('opdt', text="交易日期", anchor=tk.W)self.tree.heading('dscrp', text="交易类型", anchor=tk.W)# 设置关联scrollBar.config(command = self.tree.yview)# 每次打开清空页面for item in self.tree.get_children():self.tree.delete(item)# sql = "select a.outid ,a.name ,b.opfare,b.opdt,b.dscrp from base_customers a,rec_cust_acc b where a.customerid = b. customerid and b.opdt >= to_date(:preopdt,'yyyy-MM-dd HH24:mi:ss') and b.opdt <= to_date(:nextopdt,'yyyy-MM-dd HH24:mi:ss') order by b.opdt desc"preopdt=self.pretimeInput.get()nextopdt=self.nexttimeInput.get()# print(outidname)data = self.cursor.execute(sql,(preopdt,nextopdt,outidname)).fetchall()# print(data)for itm in data:self.tree.insert("",tk.END,values=itm)self.tree.pack(padx=10,pady=10, fill=tk.BOTH,expand=1)def export():# 导出export        # 打开文件夹选择对话框folder_path = filedialog.asksaveasfilename(title='请选择一个文件',filetypes=[("CSV", ".csv")]) # 更新标签文本print(folder_path)list = []for row_id in self.tree.get_children():list.append(self.tree.item(row_id)['values'])with open(f'{folder_path}.csv','w',newline='') as csvfile:fieldnames = ['学号', '姓名', '交易金额','账户余额','交易日期','交易类型']writer = csv.writer(csvfile)writer.writerow(fieldnames)writer.writerows(list)exportbtn = tk.Button(self.persontail,text='导出',width=8,command=self.export).pack()def export(self):# 导出export        # 打开文件夹选择对话框# 更新标签文本# print(folder_path)list = []columns = []# 获取表格内容idfor row_id in self.tree.get_children():list.append(self.tree.item(row_id)['values'])print(len(self.tree.get_children()))   # 通过第一行获取列数生成标题# print(self.tree.item)if len(self.tree.get_children()) != 0:print('ok')folder_path = filedialog.asksaveasfilename(title='请选择一个文件',filetypes=[("CSV", ".csv")]) for i in range(0,len(self.tree.item('I001')['values'])):columns.append(self.tree.heading(column=i)['text'])# 导出csvwith open(f'{folder_path}.csv','w',newline='') as csvfile:fieldnames = columnswriter = csv.writer(csvfile)writer.writerow(fieldnames)writer.writerows(list)else:messagebox.showwarning("提示", "没有数据,无法导出")returnif __name__ == "__main__":root = tk.Tk()app =  mainApp(root)root.mainloop()

这篇关于Python桌面应用之XX学院水卡报表查询系统(Tkinter+cx_Oracle)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/274604

相关文章

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Python包管理工具pip的升级指南

《Python包管理工具pip的升级指南》本文全面探讨Python包管理工具pip的升级策略,从基础升级方法到高级技巧,涵盖不同操作系统环境下的最佳实践,我们将深入分析pip的工作原理,介绍多种升级方... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合