Home > Net >  Need help in getting my combination of strings from treeview column into textbox
Need help in getting my combination of strings from treeview column into textbox

Time:04-15

I am trying to get the text inside my Address column which contains a combination of string fetched from mysql into the tkinter text box but it returns error. Can anyone help me?

fetching from sql

_sql = "SELECT * FROM customers"
_val = None
custs = sql.fetchAll(_sql,_val)
if custs != None:
    for i, (id,name,address1,address2,address3,address4,phone,email,birthdate) in enumerate(custs, start=1):
        listBox.insert("","end",values=(id,name,address1 ", " address2 ", " address3 ", " address4,phone,email,birthdate))

my treeview code:

cols=('ID','Name','Address','Phone','Email','Birthdate')
listBox=ttk.Treeview(listFrame, columns=cols, show='headings',height=36)
for col in cols:
listBox.heading(col, text=col)
listBox.grid(row=1,column=0,columnspan=2)
listBox.column(col,minwidth=0,width=262,stretch=NO,anchor=CENTER)
listBox.place(x=30,y=173)
listBox.bind('<Double-Button-1>',self.selectCust)

textbox code:

e2=Text(listFrame,fg="#18191b",bg="#fbb913",width=20,borderwidth=0,highlightthickness=0,height=5,wrap=WORD)
e2.place(x=100,y=52)

trying to insert strings from address column to textbox:

e2.insert(0,sel['Address'])

error output:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "e:\AMAR\ADBMS\db.py", line 121, in selectCust
    e2.insert(0,sel['Address'])
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 3743, in insert
    self.tk.call((self._w, 'insert', index, chars)   args)
_tkinter.TclError: bad text index "0"

CodePudding user response:

Tkinter Text objects accept indexes in the form "<line>.<character>", so the first line, first character would be represented as "1.0". The second line, first character would be "2.0", the fourth line, ninth character would be "4.8" and so on.

In this case, you could change the problem line to:

e2.insert('1.0', sel['Address'])

TkDocs Text widget

  • Related