Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

python - 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 INCORPORATIONRM Software DEV Company PvtJewellery 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:UsersONEAppDataLocalProgramsPythonPython39libkinter\__init__.py", line 1884, in __call__
    return self.func(*args)
  File "d:RM INCORPORATIONRM Software DEV Company PvtJewellery Appcontact.py", line 53, in select
    data1 = p_mod.list_multiply(data, copies_data)
  File "d:RM INCORPORATIONRM Software DEV Company PvtJewellery Appp_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 INCORPORATIONRM Software DEV Company PvtJewellery 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

question from:https://stackoverflow.com/questions/65713812/trouble-in-manipulating-the-data-for-treeview-in-tkinter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...