|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2024-3-31 14:47:22
|
显示全部楼层
class InsertCsv:
def __init__(self):
self.csv_path = os.path.join(os.getcwd(),"csv")
def insert(self,df):
self.file,self.min,self.max,self.sum = df
self.year = self.max[:4] + ".csv"
self.file_path = os.path.join(self.csv_path,self.year)
self.is_file_name()
self.file.to_csv(self.file_path,mode='a', header=(['日期', '车号', '服务项目', '现金']), index=False)
pass
def is_file_name(self):
if not os.path.isfile(self.file_path):
with open(self.file_path,'w'):
pass
insert_csv = InsertCsv()
这个是我把拿到的Excel表格转化写成CSV的代码,,每个月都有新的数据,要续写进这个csv文件的。。。
class SearchFrame(tk.Frame):#这里继承了tk.Frame,所以下面也不需要去创建Frame对象
def __init__(self,root):#这里需要去接受一下传进来的父容器root
super().__init__(root)#这里重写的时候需要去设置接受一下父容器
self.query_frame = tk.LabelFrame(self,text="查询内容")
self.create_page()
def filter_dataframe(self):
self.data_begin = self.data_begin_entry.get()
self.data_end = self.data_end_entry.get()
self.data_begin = pd.to_datetime(self.data_begin)
self.data_end = pd.to_datetime(self.data_end)
self.car_license_keyword = self.car_license_entry.get()
self.maintenance = self.maintenance_entry.get()
self.filtered_df = self.df[(self.df['日期'] >= self.data_begin) &
(self.df['日期'] <= self.data_end) &
(self.df['车号'].str.contains(self.car_license_keyword)) &
(self.df['服务项目'].str.contains(self.maintenance))]
self.sum_var.set(self.filtered_df['现金'].sum())
self.display_dataframe(self.filtered_df)
def display_dataframe(self,df):
self.prompt.set("")
self.treeview.delete(*self.treeview.get_children())
for index, row in df.iterrows():
self.treeview.insert("", 'end', values=row.tolist())
def define(self):
self.prompt = tk.StringVar()
self.data_begin_var = tk.StringVar()
self.data_end_var = tk.StringVar()
self.car_license_var = tk.StringVar()
self.maintenance_var = tk.StringVar()
self.car_num_var = tk.StringVar()
self.sum_var = tk.StringVar()
self.file_path = 'C:/Users/Administrator/Desktop/维修单/2024.1维修单.xlsx'
self.one_year_ago = date.today() - timedelta(days=365)
self.data_begin_var.set(self.one_year_ago)
self.data_end_var.set(date.today())
def get_sheet(self):
# 从Excel读取数据到DataFrame
# self.df1 = pd.read_excel(self.file_path, index_col=None)
csv_path = os.path.join(path_all.service_csv_path,"2024.xlsx")
self.ddf = pd.read_csv(csv_path,encoding="gbk")
self.ddf['日期'] = pd.to_datetime(self.ddf['日期'],format='%Y-%m-%d')
self.ddf['现金'] = self.ddf['现金'].astype(float)
self.ddf['车号'] = self.ddf['车号'].str.upper()
self.df1 = self.ddf
self.df = self.df1[['日期', '车号', '服务项目', '现金']].fillna(method='ffill')
self.df["现金"] = self.df["现金"].astype(float)
self.df['日期'] = pd.to_datetime(self.df['日期'])
self.df['日期'] = self.df['日期'].dt.date
self.df['车号'] = self.df['车号'].str.upper()
self.df = self.df.dropna(axis=1, how='all')
# 在Treeview中显示DataFrame的所有数据
self.display_dataframe(self.df)
def treeview_laber(self):
self.define()
self.yscroll = tk.Scrollbar(orient=tk.VERTICAL)
self.treeview = ttk.Treeview(self,height=37, yscrollcommand=self.yscroll.set)# 创建一个Treeview控件
self.columns = ['日期', '车号', '服务项目', '现金']# 添加表头
self.treeview['columns'] = self.columns
for column in self.columns:
self.treeview.heading(column, text=column)
self.get_sheet()
# 显示TreeView
self.vsb = ttk.Scrollbar(orient='vertical', command=self.treeview.yview)
self.vsb.pack(side='right', fill='y')
self.treeview.column(column='#0', width=1)
self.treeview.column(column='日期', width=15)
self.treeview.column(column='车号', width=10, anchor='w')
self.treeview.column(column='服务项目', width=330)
self.treeview.column(column='现金', width=8, anchor='e')
# 设置TreeView的垂直滚动条
self.treeview.configure(yscrollcommand=self.vsb.set)
def get_car_num(self):
self.car_num = self.car_num_entry.get()
flag,self.car_lic = carmessage.search_car_num(self.car_num)
if flag:
self.prompt.set(f"")
else:
self.prompt.set(f"没有{self.car_num}号车")
self.car_license_var.set(self.car_lic)
def condition_label(self):
self.data_begin_label = tk.Label(self.query_frame, text="开始日期 :")
self.data_begin_entry = tk.Entry(self.query_frame, textvariable=self.data_begin_var,width=10)
self.data_begin_label.grid(row=0, column=0,padx=5)
self.data_begin_entry.grid(row=0, column=1,padx=5)
self.data_end_label = tk.Label(self.query_frame, text="截止日期 :")
self.data_end_entry = tk.Entry(self.query_frame, textvariable=self.data_end_var,width=10)
self.data_end_label.grid(row=1, column=0,padx=5)
self.data_end_entry.grid(row=1, column=1,padx=5)
self.empty_label_1 = tk.Label(self.query_frame,text=" ")
self.empty_label_1.grid(row=0,column=2,padx=10)
self.car_num_button = tk.Button(self.query_frame,text="查询车号:",command=self.get_car_num)
self.car_num_entry = tk.Entry(self.query_frame,textvariable=self.car_num_var,width=10)
self.car_num_button.grid(row=0,column=3,padx=5)
self.car_num_entry.grid(row=0,column=4,padx=5)
self.car_license_label = tk.Label(self.query_frame, text="车 牌 :")
self.car_license_entry = tk.Entry(self.query_frame, textvariable=self.car_license_var,width=10)
self.car_license_label.grid(row=1, column=3,padx=5)
self.car_license_entry.grid(row=1, column=4,padx=5)
self.empty_label_2 = tk.Label(self.query_frame,text=" ")
self.empty_label_2.grid(row=0,column=5,padx=10)
self.maintenance_label = tk.Label(self.query_frame, text="维修项目:")
self.maintenance_entry = tk.Entry(self.query_frame, textvariable=self.maintenance_var)
self.maintenance_label.grid(row=0, column=6,padx=10)
self.maintenance_entry.grid(row=0, column=7,padx=10)
self.sum_label = tk.Label(self.query_frame,text="金额小计:")
self.sum_label.grid(row=1, column=6,padx=10)
self.sum_label = tk.Label(self.query_frame,textvariable=self.sum_var,font=10)
self.sum_label.grid(row=1, column=7,padx=10)
self.empty_label_3 = tk.Label(self.query_frame,text=" ")
self.empty_label_3.grid(row=0,column=8,padx=10)
self.prompt_label = tk.Label(self.query_frame,textvariable=self.prompt,font=20,foreground="red")
self.prompt_label.grid(row=2,column=4,pady=5)
self.button = tk.Button(self.query_frame, text='查询', command=self.filter_dataframe,width=10)
self.button.grid(row=4, column=4,pady=10)
def create_page(self):
# 创建一个Entry控件用于输入筛选关键字
self.treeview_laber()
self.condition_label()
self.query_frame.pack(pady=10,padx=5)
self.treeview.pack(fill=tk.BOTH, expand=False,pady=10,padx=5)
这段是我读取csv文件后,让它按选择的条件显示在treeview上的
但总是不行
——————————————
———————— |
|