Python:使用 SQlite3 数据库中的数据在 Tkinter 中构建条形图

萨夫萨德夫

在我开始之前,我是堆栈溢出的新手,所以如果我的问题格式不正确,我深表歉意。这也是我学校图书馆的 A 级学校项目,旨在帮助管理图书馆。我创建了一个表格,其中包含有关学生以前贷款的数据,名为“过去贷款”(请参阅此处),我需要一种方法来找出哪些书籍在图书馆用户中最受欢迎,并在条形图上表示结果。为此,我创建了一个 SQL 命令,用于计算书名从“过去贷款”表的“书”列中出现的次数,目前我在那里有 2 本书(请参阅此处)。

由于Tk.canvas条形图的性质,除了整数分别作为数据之外,所以我需要找到一种拆分书名和它在表格中出现的次数的方法,使用它的次数作为要显示在条形图上的数据,书名作为 X 轴上的标签。

目前,我已经编写了 SQL 命令,以使用 SQLite3 中的“COUNT”函数从包含过去贷款数据的表中提取我需要的数据,此外,我还为条形图编写了框架,并测试了它与来自一个列表,例如 [1,2,3,4,5,..]

请注意,条形图成功显示在 Tkinter 上并带有正确的数据值,不幸的是我无法添加结果图片,因为我没有足够的代表。

我的代码如下所示:

    command = ("SELECT book,COUNT(book) AS cnt FROM pastLoans GROUP BY 
    book ORDER BY cnt DESC;")

    c.execute(command)
    result = c.fetchall()
    print (result)                            
    """This is the code for pulling the book name and amount of books 
    from the "pastLoans" as well as the book name, the result is this:

    >>> [('Book', 1), ('Harry Potter', 1)]


    This is my bar chart frame:"""

    data = [1, 2, 3, 4, 5] #The data used here is sample data.

    g_width = 900  # Define it's width
    g_height = 400  # Define it's height
    g = tk.Canvas(self, width=g_width, height=g_height)
    g.grid()

    # The variables below size the bar graph
    y_stretch = 15  # The highest y = max_data_value * y_stretch
    y_gap = 20  # The gap between lower canvas edge and x axis
    x_stretch = 10  # Stretch x wide enough to fit the variables
    x_width = 20  # The width of the x-axis
    x_gap = 20  # The gap between left canvas edge and y axis

    for x, y in enumerate(data):

        # coordinates of each bar

        # Bottom left coordinate
        x0 = x * x_stretch + x * x_width + x_gap

        # Top left coordinates
        y0 = g_height - (y * y_stretch + y_gap)

        # Bottom right coordinates
        x1 = x * x_stretch + x * x_width + x_width + x_gap

        # Top right coordinates
        y1 = g_height - y_gap

        # Draw the bar
        g.create_rectangle(x0, y0, x1, y1, fill="red")

        # Put the y value above the bar
        g.create_text(x0 + 2, y0, anchor=tk.SW, text=str(y))
燃烧的卡尔

由于您已经完成了让 tkinter 显示栏和其上方的一些文本的所有工作,因此您只需要迭代result而不是data

# Sort so that the most popular book is on the left
result.sort(key=lambda e: e[1], reverse=True)

for x, (name, y) in enumerate(result):
   ...

   # Put the name above the bar
   g.create_text(x0 + 2, y0, anchor=tk.SW, text=name)

您可能需要改变x_stretch变量,以便文本不会重叠。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用GUI Tkinter在Python中以特定格式显示来自数据库sqlite3的信息?

在Python中从sqlite3数据库写入CSV

使用mysql数据库的d3中的条形图

使用ʻexecutemany`更新现有SQLite3数据库中的条目(使用Python sqlite3)

sqlite数据库中的achartengine条形图

使用SQLite数据库填充MPAndroidChart条形图

使用 Flask 在 sqlite3 数据库中添加数据时出错

数据未使用 php 插入到 sqlite3 数据库中

如何使用python在sqlite3数据库中存储0.2值

在python中,使用sqlite3时,数据库物理存储在哪里?

使用python在sqlite3中附加数据库

使用Python将图像从sqlite3数据库显示到QTableWidget中的列

使用Python确定用户名是否在SQLite3数据库中

如何使用 Python 将日期时间插入到 sqlite3 数据库中?

Python中的数据类型错误匹配尝试使用某些Twitter数据更新Sqlite3数据库

如何限制Django数据库中的条目数?使用默认数据库(sqlite3)

如何使用sqlite3_exec从sqlite3数据库中选择数据到变量中?

如何判断sqlite3数据库中是否存在值,python

索引列表中的列表以创建数据库列sqlite3 python时出错

在 Python/SQLite3 中,如何定义用于查询数据库表的变量?

如何用1行在python中创建sqlite3数据库

如何使用C ++在sqlite3中打开受密码保护的数据库?

如何使用Java从Sqlite3数据库中解析日期?

使用SQLite3在php中创建新数据库

如何在Swift 4中的sqlite3数据库中正确使用附加功能

如何使用C ++在sqlite3数据库中执行writefile函数

如何使用python sqlite3包在python中的不同进程之间共享:memory:数据库

Python数据库sqlite3

SQLITE3,使用其他表模式将数据复制到新的数据库文件中