Python Forum
[Tkinter] insert table columns data in different textboxes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] insert table columns data in different textboxes
#1
Hi i have a table with two columns, A & B. In A column there are Names and in column B there are grades, where as one Name may be related to many grades i.e
A|B
T1|1
T1|3
T1|5
T2|1
T3|4
In my select query to the oracle table, according to column A, if the "where clause" refers to name=T1, the results will be:
T1,1
T1,3
T1,5
I want to insert these results to six textboxes (using tkinker):
NameBox:T1 GradeBox:1
NameBox:T1 GradeBox:3
NameBox:T1 GradeBox:5

After fetchall() i dont know how to match the above results and insert them into the boxes. If the Name-Grade was one table row, there would be no problem.For example, i could directly link textboxes textvariables=column A value and column B value respectively.
When the rows>1, i dont know how to handle it. I must insert the fetchall results into lists ? and then how i relate lists' elements to each textbox?
Reply
#2
Make an effort and show what you come up with. We will be glad to help, but wont write the code for you.
Reply
#3
Hi, again, sorry that i didnt upload the code. The code below has the actual names of A,B columns that is, name_project, description. As i said above, if name_project(or A) was unigue in my table (= 1 row table) then i would need only two textboxes,
NameBox:T1 GradeBox:1
Instead, name_project varies from 1-5.
import cx_Oracle
from tkinter import*
from tkinter import messagebox

def search():
    try:
#=============DB Connect======================#                
        connstr='SOLVATIO/SOLVATIO@localhost'
        conn = cx_Oracle.connect(connstr)
        curs = conn.cursor()
        curs1= conn.cursor()
#=============first_query======================#   
        curs.execute("select * from customers where afm='%s'"%afm.get())      
        result=curs.fetchone()
        company_name.set(result[1])
        siebel_customer_code.set(result[3])
#=============2nd_query======================#
        curs1.execute("select name_project,description, count(*) from customer_desc where company_name_d='%s' group by name_project, description"%e2.get())
        results=curs1.fetchall()
        r_count=curs.rowcount
        if r_count==0:
         print("no rows")
        else:
         for i in range (0, r_count):
          if i==0:
           name_project.set(results[0][0])
           description.set(results[0][1])
          elif i==1:
           name_project.set(results[1][0])
           description.set(results[1][1])
          elif i==2:
           name_project.set(results[2][0])
           description.set(results[2][1])
          elif i==3:
           name_project.set(results[3][0])
           description.set(results[3][1])
          else:
           name_project.set(results[4][0])
           description.set(results[4][1])
        
        e1.configure(state='disabled')
        conn.close()

    except:
        messagebox.showinfo('No data', 'No such data')
        
def clear():
#================Initialize==============#    
    afm.set('')
    company_name.set('')
    siebel_customer_code.set('')
    name_project.set('')
    description.set('')
    e1.configure(state='normal')
    f1.configure(state='normal')

#================GUI====================#
w1=Tk()
w1.title('GUI Special Solutions')
w1.geometry('1350x750+0+0')
ptitle=Label(w1, font=('arial',11,'bold'),text='''Search Asset''')
ptitle.grid(row=0, column=0, columnspan=2)

#========== Variables===================#
afm=StringVar()
company_name=StringVar()
siebel_customer_code=StringVar()
name_project=StringVar()
description=StringVar()

#==========Buttons,TextBoxes ============#
l1=Label (w1, text=' AFM ')
e1=Entry(w1, textvariable=afm)
l2=Label (w1, text=' Company ')
e2=Entry(w1, textvariable=company_name)
l3=Label (w1, text=' Siebel Code ')
e3=Entry(w1, textvariable=siebel_customer_code)
b1=Button(w1, text=' Search ', command=search)
b2=Button(w1, text=' Clear ', command=clear)
l4=Label (w1, text=' Project Name ')
f1=Entry(w1, textvariable=name_project)
l5=Label (w1, text=' Description ')
f2=Entry(w1, textvariable=description, fg="lime green", bd=4, width=140)

f3=Entry(w1, textvariable=name_project)
f4=Entry(w1, textvariable=description, fg="lime green", bd=4, width=140)
f5=Entry(w1, textvariable=name_project)
f6=Entry(w1, textvariable=description, fg="lime green", bd=4, width=140)
f7=Entry(w1, textvariable=name_project)
f8=Entry(w1, textvariable=description, fg="lime green", bd=4, width=140)
f9=Entry(w1, textvariable=name_project)
f10=Entry(w1, textvariable=description, fg="lime green", bd=4, width=140)


l1.grid(row=1, column=0)
e1.grid(row=1, column=1)
l2.grid(row=2, column=0)
e2.grid(row=2, column=1)
l3.grid(row=3, column=0)
e3.grid(row=3, column=1)
b1.grid(row=1, column=2)
b2.grid(row=4, column=0)
l4.grid(row=5, column=0)
f1.grid(row=5, column=1)
l5.grid(row=5, column=2)
f2.grid(row=5, column=3)

f3.grid(row=6, column=1)
f4.grid(row=6, column=3)
f5.grid(row=7, column=1)
f6.grid(row=7, column=3)
f7.grid(row=8, column=1)
f8.grid(row=8, column=3)
f9.grid(row=9, column=1)
f10.grid(row=9, column=3)

w1.mainloop()
The TextBoxes f1,f3,f5,f7,f9 take the value of name_project and the boxes f2, f4, f6,f8,f10 take the value of description (where as, f1-f2 are in the same row in table, etc).
The existing code is obviously wrong, because after the "for" execution, all the text boxes take the same value (because the text variable in the box attribute is set to column names).
I tried to set the textvariable=name_project.set(results[0][0]) for each button, but there was an indexing issue
Possibly the "for" approach is totally wrong, so i tried to use list (below i isolate only the 2nd query):

curs.execute("select name_project,description, count(*) from customer_desc where company_name_d='Fraport' group by name_project, description")      
result=curs.fetchall()
resultList = []  
for data_out in result:  
resultList.append(data_out[0])
resultList.append(data_out[1])
Having the two lists, i cant find a way to isolate each element from the list although my biggest problem remains the matching of data with textboxes.

thanks
Reply
#4
you need to create tkinter string and/or int variables for each of the text variables.
They have the format:
fname_project = StringVar()
fname_project.set(name_project)
# and the definition for f1:
f1=Entry(w1, textvariable=fname_project)
and IntVar() for any integers
Reply
#5
It works!!!!!
I followed your instructions making the below modification and the result was the expected one.
Thanks a lot!!!

name_project=(results[0][0])
description=(results[0][1])
fname_project1.set(name_project)
fdescription1.set(description)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 4,216 Jan-19-2023, 03:58 PM
Last Post: Shakanrose
  [Tkinter] modify the html table to include data from tkinter labels rwahdan 0 1,590 Jun-20-2021, 10:33 AM
Last Post: rwahdan
  [PyQt] How to display multiple data(from 2 different related table) in one cell of QTableWid yangjae 4 4,320 Oct-17-2018, 07:54 AM
Last Post: Larz60+
  Help - GTK3 + Pango: Printing data as table format scandido 0 3,169 Jan-26-2018, 06:03 PM
Last Post: scandido

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020