简体   繁体   中英

Trouble in manipulating the data for treeview in tkinter

everyone. Let me first paste the code.

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()

This is the code where I need to fetch queries regarding a requirement and the output required is as follows:

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)]

I have also used a remote function named p_mod.list_multiply() . The function looks like this:

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

Now the problem arises... Whenever I try to run the code with same outputs(data1, data2,...) using the function remotely from the main code, it runs successfully, but whenever I am trying to run the script inside the main program it gives me an error.

Error is as follows:

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'

Let me show you the output used with the function remotely, from the main code...

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)

Then what is the problem dude? Eagerly waiting someone's reply

You have overwritten list_input by the following line in list_multiply() :

list_input = list(list_input)[0]

Therefore, list_input will be a string after that.

Just remove this line will solve the issue.

Also the following line:

list_input1 = list_input[1 : -1]

will not copy the last item of list_input into list_input1 .

It should be

list_input1 = list_input[1:]

list_multiply() can be simplified as below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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