代码
from tkinter import ttk
from tkinter import messagebox
from tkinter import *
import xlwt
win = Tk()
# 设置窗口大小
win.geometry("400x400")
f = ttk.Frame(win)
# 设置第0行
f.grid(row=0)
f1 = ttk.Frame(f)
f1.grid(row=0)
# 定义标签按钮
l1 = ttk.Label(f1,text = "Book APP\n----------------------")
l1.pack()
# 定义f2容器
f2 = ttk.Frame(f)
f2.grid(row=1)
# Book ID"、"Title"、"Author"、"Publisher"、"Year"和“Total Page
# id
Lid = ttk.Label(f2,text="Book ID")
Lid.grid(row=0,pady=5)
Eid = ttk.Entry(f2)
Eid.grid(row=0,column=1,padx = 100,pady=5)
# Title
Litem = ttk.Label(f2,text="Title")
Litem.grid(row=1,pady=5)
Eitem = ttk.Entry(f2)
Eitem.grid(row=1,column=1,padx = 100,pady=5)
# Author
Lprice = ttk.Label(f2,text="Author")price.grid(row=2,pady=5)
Eprice = ttk.Entry(f2)
Eprice.grid(row=2,column=1,padx = 100,pady=5)
# Publisherquantity = ttk.Label(f2,text="Publisher")
Lquantity.grid(row=3,pady=5)
Equantity = ttk.Entry()
Equantity.grid(row=3,column=1,padx = 1,pady=5)
# Year
Lquantity1 = ttk.Label(f2,text="Year")
Lquantity1.grid(row=4,pady=5)
Equantity1 = ttk.Entry(f2)
Equantity1.grid(row=4,column=1,padx = 100ady=5)
# Total Page
Lquantity2 = ttk.Label(f2,text="Total Page")
Lquantity2.grid(row=5,pady=5)
Equantity2 = ttk.Entry(f2)
Equantity2.grid(row=5,column=1,padx = 100,pady=5)
item_list=[]
totalprice = 0
# 定义按钮点击函数
def add():
global list
global totalprice
# 获取输入框数据
s = [Eid.get(),Eitem.get(),Eprice.get(),Equantity.get(),Equantity1.get(),Equantity2.get()]
# 写入表格
treeview.insert('', 'end', values=s)
# 存入商品数据
item_list.append(s)
def save():
# 创建 xls 文件对象
wb = xlwt.Workbook()
# 新增表单页
sh1 = wb.add_sheet('books')
# Book ID"、"Title"、"Author"、"Publisher"、"Year"和“Total Page
sh1.write(0, 0, "Book ID")
sh1.write(0, 1, "Title")
sh1.write(0, 2, "Author")
sh1.write(0, 3, "Publisher")
sh1.write(0, 4, "Year")
sh1.write(0, 5, "Total Page")
# 可以判断item_list的数组长度,len(),数组长度就是excel的行数
# 这是列表长度,也就是excel行数。
x = len(item_list)+1
for i in range(1,x):
sh1.write(i,0,item_list[i-1][0])
sh1.write(i, 1, item_list[i-1][1])
sh1.write(i, 2, item_list[i-1][2])
sh1.write(i, 3, item_list[i-1][3])
sh1.write(i, 4, item_list[i-1][4])
sh1.write(i, 5, item_list[i-1][5])
# 保存文件
wb.save('book.xls')
# 弹出保存成功对话框
messagebox.showinfo(title="保存成功", message="保存成功!")
fb = ttk.Frame(f)
fb.grid(row=2)
# 定义添加按钮
AddButton = ttk.Button(fb,text="Add",command=add)
AddButton.grid(row=0,column=0,pady=10,padx=10)
# 定义保存按钮
SaveButton = ttk.Button(fb,text="save",command=save)
SaveButton.grid(row=0,column=1)
# 定义f3容器
f3 = ttk.Frame(f)
f3.grid(row=3)
# 定义表格
columns = ("ID", "Title","Author","Publisher","Year","Total Page")
treeview = ttk.Treeview(f3, height=18, show="headings", columns=columns) # 表格
# Book ID"、"Title"、"Author"、"Publisher"、"Year"和“Total Page
treeview.heading("ID", text="Book ID") # 显示表头
treeview.heading("Title", text="Title")
treeview.heading("Author", text="Author")
treeview.heading("Publisher", text="Publisher")
treeview.heading("Year", text="Year")
treeview.heading("Total Page", text="Total Page")
treeview.pack()
win.mainloop()
效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/cfc3e0d99b414bc8b5d3cf728154063c.png)
代码
import tkinter as tk
def calculate_total():
# 获取用户输入的值
name = book_name.get()
quantity = int(book_quantity.get())
price = float(book_price.get())
# 计算总价
total = quantity * price
# 更新标签以显示总价
total_label.config(text=f"总价: {total:.2f}元")
# 创建主窗口
root = tk.Tk()
root.title("图书价格计算器")
# 创建输入框和标签
book_name_label = tk.Label(root, text="图书名称:")
book_name_label.grid(row=0, column=0, padx=5, pady=5)
book_name = tk.Entry(root)
book_name.grid(row=0, column=1, padx=5, pady=5)
book_quantity_label = tk.Label(root, text="数量:")
book_quantity_label.grid(row=1, column=0, padx=5, pady=5)
book_quantity = tk.Entry(root)
book_quantity.grid(row=1, column=1, padx=5, pady=5)
book_price_label = tk.Label(root, text="单价:")
book_price_label.grid(row=2, column=0, padx=5, pady=5)
book_price = tk.Entry(root)
book_price.grid(row=2, column=1, padx=5, pady=5)
# 创建计算按钮
calculate_button = tk.Button(root, text="计算总价", command=calculate_total)
calculate_button.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
# 创建显示总价的标签
total_label = tk.Label(root, text="总价: ")
total_label.grid(row=4, column=0, columnspan=2, padx=5, pady=5)
# 运行主循环
root.mainloop()
效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/7d07bd48d89842fdb45b1de818ba2e78.png)