Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 用sql中的信息填充Treeview?_Python 3.x_Tkinter_Treeview - Fatal编程技术网

Python 3.x 用sql中的信息填充Treeview?

Python 3.x 用sql中的信息填充Treeview?,python-3.x,tkinter,treeview,Python 3.x,Tkinter,Treeview,我想就以下代码征求一些建议: from tkinter import * from tkinter import ttk import sqlite3 root = Tk() root.geometry("500x500") root.title("Inventory Balance") def db(): global conn, mycursor conn = sqlite3.connect('MyStock.sql3') mycursor = conn.cur

我想就以下代码征求一些建议:

from tkinter import *
from tkinter import ttk
import sqlite3

root = Tk()
root.geometry("500x500")
root.title("Inventory Balance")


def db():
    global conn, mycursor
    conn = sqlite3.connect('MyStock.sql3')
    mycursor = conn.cursor()


def data():`
    tree.delete(*tree.get_children())
    mycursor.execute("SELECT * FROM ItemCode")
    for row in myscursor:
        tree.insert('', 'end', values=row[1:6])

    conn.close()

frame = Frame(root)
frame.pack()

tree = ttk.Treeview(frame, columns = (1,2,3,4,5), height = 20, show = "headings")
tree.pack(side = 'top')

tree.heading(1, text="ItemCode")
tree.heading(2, text="Description")
tree.heading(3, text="Category")
tree.heading(4, text="Unit")
tree.heading(5, text="Quantity")

tree.column(1, width = 100)
tree.column(2, width = 100)
tree.column(3, width = 100)
tree.column(4, width = 100)
tree.column(5, width = 100)

# Inserting Scrollbar
scroll = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
scroll.pack(side = 'right', fill = 'y')

tree.configure(yscrollcommand=scroll.set)

root.mainloop()
稍后,我将不得不将其用作主屏幕,添加按钮,并在使用stock时显示一个不断更新的treeview,然后通过另一个python tkinter脚本进行更新。 我的主要问题是,它读取正确的列(ItemCode、Description、Category、Unit、Quantity),但数据库中包含的信息不会显示在树视图中

请提供帮助,如有需要,请随时询问更多信息

我按照建议编辑了脚本,结果仍然是:


再次感谢您

您的代码中有两个问题:

  • 将记录插入树状视图时使用
    info
    <应使用代码>行
  • treeview.insert(…)
    需要两个位置参数:
    parent
    index
因此,改变:

info = mycursor.fetchall()
for row in info:
    tree.insert(values=(info[1], info[2], info[3], info[4], info[5]))
致:


为什么您的代码在向treeview插入数据时使用
info[1]、info[2]、…
?应该改为
行[1],行[2],…
?为什么索引从1开始,而不是从0开始?请查找上面编辑的帖子以确认任何错误。我添加了一个图像链接来显示输出。你有没有收到任何错误?尝试将结果打印到控制台,以查看是否实际返回了记录。更新的代码中有一个输入错误:
对于mycursor中的行:
对于mycursor中的行:应该是
。另外,您的代码没有调用
db()
data()
,因此不会显示任何数据。对于拼写错误,很抱歉,它只是在文章中,没有运行带有拼写错误的脚本。我试图用代码截屏tkinter窗口以显示更新。没有错误代码。它只说明“进程已完成,退出代码为0”,我应该在哪里添加“call”函数?这是我的第一个更大的综合tkinter项目。
for row in mycursor:
    tree.insert('', 'end', values=row[1:6])