python - Python:如何从数据库创建饼图?
问题描述
我想从我的数据库构建一个饼图。
我正在构建的 python GUI 应用程序是使用 tKinter 完成的。目前我正在使用 sqlite3 作为数据库,并且有人告诉我使用 matplotlib 作为表生成器。
我正在制作的这个应用程序是用于储蓄系统的
这是我用来将数据输入数据库的代码
def Submit():
#Creating or connecting to a Database
conn = sqlite3.connect("Balance.db")
#create cursor
c = conn.cursor()
c.execute("INSERT INTO Log VALUES (:value, :type, :tag)",
{
'value': value.get(),
'type': var.get(),
'tag': tag.get()
})
#commit changes
conn.commit()
#close connection
conn.close()
#clear the text boxes to allow for new input
value.delete(0, END)
#cbv is not included in this since cbv is checkbox data
tag.delete(0,END)
Submit_Button = Button(root, text = "Add to Logs", command = Submit)
Submit_Button.grid(row=9, column = 0)
但是,我想做的是从这个日志中,拉出这三个变量,值、类型和标签,以创建一个饼图,分别代表类型(费用)、值(来自总费用)和标签(我想如何分割馅饼)
不幸的是,我不知道如何从数据库中提取奇异变量。
有人能帮助我吗?谢谢
解决方案
如果您正在寻找类似的内容,请检查该片段。
您可以将value,type and tag
值存储在变量中,然后使用它插入数据库并调用 matplotlib
import tkinter as tk
from matplotlib import pyplot as plt
root=tk.Tk()
root.geometry("200x100")
def Submit():
value=value_entry.get()
types=type_entry.get()
tags=tags_entry.get()
conn = sqlite3.connect("Balance.db")
c = conn.cursor()
c.execute("INSERT INTO Log VALUES (:value, :type, :tag)",
{
'value': value,
'type': types,
'tag': tags
})
conn.commit()
conn.close()
label=["value","type","tag"]
data=[value,types,tags]
fig = plt.figure(figsize =(5, 5))
plt.pie(data, labels = label,autopct='%1.2f%%')
plt.show()
#clear the text boxes to allow for new input
values.set("")
typess.set("")
tagss.set("")
values=tk.IntVar()
typess=tk.IntVar()
tagss=tk.IntVar()
value_label = tk.Label(root, text = 'Value')
value_entry = tk.Entry(root, textvariable = values)
type_label = tk.Label(root, text = 'Type')
type_entry = tk.Entry(root, textvariable = typess)
tags_label = tk.Label(root, text = 'Tags')
tags_entry = tk.Entry(root, textvariable = tagss)
Submit_Button = tk.Button(root, text = "Add to Logs", command = Submit)
value_label.grid(row=0,column=0)
value_entry.grid(row=0,column=1)
type_label.grid(row=1,column=0)
type_entry.grid(row=1,column=1)
tags_label.grid(row=2,column=0)
tags_entry.grid(row=2,column=1)
Submit_Button.grid(row=3, column = 1)
root.mainloop()
注意- 如果您不想要百分比数据,您可以删除autopct='%1.2f%%'
推荐阅读
- terminal - 为什么安装汤筛后模块仍然无法访问?
- python - 如何在 Python 中解析这样的 JSON 响应?
- python - 如何递归地转换类实例的属性
- c# - 从 SQL Server 表中获取数据并更新为 XML 文件内容
- java - 我可以在 JpaRepository 的 saveAll 中混合更新和插入吗
- uml - 如何建模全局变量集
- html - 如何在 CSS 中使用多边形创建饼图
- uvm - item_collected_port 和 item_observed_port 之间的区别
- objective-c - Cocoa 编译失败
- angularjs - 未使用字段中的数据表搜索