繁体   English   中英

在 tkinter 中操作 treeview 的数据时出现问题

[英]Trouble in manipulating the data for treeview in tkinter

每个人。 我先贴代码。

c.execute("SELECT * FROM c20 WHERE Position = 'chain';")
data1 = c.fetchall()
c.execute("SELECT * FROM c20 WHERE Position = 'center';")
data2 = c.fetchall()
c.execute("SELECT * FROM c20 WHERE Position = 'Total';")
data3 = c.fetchall()

data1 = p_mod.list_multiply(data, copies_data)
data2 = p_mod.list_multiply(data2, copies_data)
data3 = p_mod.list_multiply(data3, copies_data)
    
meta_data = [data1, data2, data3]
n = 0
while n != 3:
    for i in meta_data:
        my_tree.insert(parent="", index="end", iid=n, text=f"{n + 1}", values=i)
        n += 1


if n == 3:
    my_tree.pack(pady=20)

root1.mainloop()

这是我需要获取有关需求的查询的代码,所需的 output 如下:

conn = sqlite3.connect("userdata.db")
>>> c = conn.cursor()
>>> c.execute("SELECT * FROM c20 WHERE Position = 'chain';")
<sqlite3.Cursor object at 0x00000221DA432F80>
>>> data1 = c.fetchall()         
>>> data1
[('chain', 100, 350, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)]

我还使用了一个名为p_mod.list_multiply()的远程 function。 function 看起来像这样:

def list_multiply(list_input, number):
    new_list = []
    list_input = list(list_input)[0]
    list_input1 = list_input[1 : -1]
    for i in list_input1:
        data = int(i) * number
        new_list.append(data)

    if list_input[0] == 'chain':
        new_list.insert(0, 'chain')

    elif list_input[0] == 'center':
        new_list.insert(0, 'center')

    elif list_input[0] == 'Total':
        new_list.insert(0, 'Total')

    new_list = tuple(new_list)
    return new_list

现在问题出现了......每当我尝试使用 function 从主代码远程运行具有相同输出(data1,data2,......)的代码时,它运行成功,但每当我试图在主程序它给我一个错误。

错误如下:

PS D:\RM INCORPORATION\RM Software DEV Company Pvt\Jewellery App> & C:/Users/ONE/AppData/Local/Programs/Python/Python39/python.exe "d:/RM INCORPORATION/RM Software DEV Company Pvt/Jewellery App/contact.py"
h
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\ONE\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
    return self.func(*args)
  File "d:\RM INCORPORATION\RM Software DEV Company Pvt\Jewellery App\contact.py", line 53, in select
    data1 = p_mod.list_multiply(data, copies_data)
  File "d:\RM INCORPORATION\RM Software DEV Company Pvt\Jewellery App\p_mod.py", line 15, in list_multiply
    data = int(i) * number
ValueError: invalid literal for int() with base 10: 'h'

让我向您展示 output 与 function 远程使用的主要代码...

PS D:\RM INCORPORATION\RM Software DEV Company Pvt\Jewellery App> & C:/Users/ONE/AppData/Local/Programs/Python/Python39/python.exe "d:/RM INCORPORATION/RM Software DEV Company Pvt/Jewellery App/p_mod.py"
('chain', 200, 700, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) ('center', 222, 826, 82, 124, 98, 70, 756, 2, 2, 6, 8, 24, 24, 16, 0, 0) ('Total', 422, 1526, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1878, 70)

那么老兄的问题是什么? 热切地等待某人的答复

您已通过list_multiply()中的以下行覆盖了list_input

list_input = list(list_input)[0]

因此, list_input之后将是一个字符串。

只需删除此行即可解决问题。

还有以下行:

list_input1 = list_input[1 : -1]

不会将list_input的最后一项复制到list_input1中。

它应该是

list_input1 = list_input[1:]

list_multiply()可以简化如下:

def list_multiply(list_input, number):
    new_list = tuple(int(x)*number for x in list_input[1:])
    return (list_input[0],) + new_list

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM