簡體   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