0

So far everything is working correctly, however I am struggling with getting the data below, once highlighted by the mouse and once hitting the select button to populate the correct entry boxes. This may be asking too much.

Listbox display

2-12312 Bob Seesaw Active

4-1212 Jim Beene Off

So if I highlight the Bob Seesaw row and hit select. The 2-12312 will pretype 'num_input', Bob 'nam_input' and so on. The purpose of this is so I can then type a new assignment and update the database for that entry. ( I pasted code specific to my initiative.

from tkinter import *
import pymysql as mdb

from tkinter import ttk
from tkinter import Listbox

def viewroster():
    rosterList.delete(0, "end")

    dbi = mdb.connect("localhost", port=3306, user="user", passwd="pass", db="interactive_db")
    cursor = dbi.cursor()
    cursor.execute("""SELECT number, firstname, surname, assign FROM active_roster""")
    rows=cursor.fetchall()
    dbi.close()
    print (rows)
    for results in rows:
     rosterList.insert("end", results)


 #Input Fields

num_input=StringVar()
num_input=Entry(root,textvariable=num_input)
num_input.grid(row=0,column=1)

ass_input=StringVar()
 ass_input=Entry(root,textvariable=ass_input)
ass_input.grid(row=0,column=3)


 nam_input=StringVar()
 nam_input=Entry(root,textvariable=nam_input)
 nam_input.grid(row=1,column=1)

 sur_input=StringVar()
 sur_input=Entry(root,textvariable=sur_input)
 sur_input.grid(row=1,column=3)

 # This is to select mouse highlighted data 
 rosSelButt=Button(root, text="Select", width=12)
 rosSelButt.grid(row=13, column=0)

-

Number [2-12312]

Firstname    [Bob]  Surname  [Seesaw]  Assign [......] (ready for text)

I do not want to loose the capability of typing a name or number as an entry and searching the database.

5
  • 1
    example on GitHub event-listboxselect-get-curselection.py Commented Dec 12, 2017 at 15:14
  • I am new to Python and dont quite understand how this pre-populates the entry boxes as per my example. This part is obvious an advance setting and why I am struggling Commented Dec 12, 2017 at 15:51
  • when you select element in list then <<ListboxSelect>> will execute function in which you have to copy selected data from Listbox to Entries manually. Commented Dec 12, 2017 at 15:58
  • How to I seperate the number and the name and put it in the correct entry box? Commented Dec 12, 2017 at 16:04
  • you have string so use slicing, split(), strip() and other string functions. Commented Dec 12, 2017 at 16:12

1 Answer 1

1

Listbox gives selected element as strin and I have this text formated in columns with

'{:10s}|{:15s}|{:10s}'

so I can use slicing to get data from correct place, and strip() to remove spaces.

import tkinter as tk

# --- function ---

def on_selection(event):
    line = event.widget.get(event.widget.curselection())

    e1.delete(0, 'end')
    e2.delete(0, 'end')
    e3.delete(0, 'end')

    e1.insert('end', line[:10].strip())
    e2.insert('end', line[11:26].strip())
    e3.insert('end', line[27:].strip())

# --- main ---

root = tk.Tk()
root.geometry('400x300')

listbox = tk.Listbox(root, font=('monospace', 10), selectbackground='red')
listbox.pack(expand='yes', fill='both')

listbox.insert('end', '{:10s}|{:15s}|{:10s}'.format('2-12312', 'Bob Seesaw', 'Active'))
listbox.insert('end', '{:10s}|{:15s}|{:10s}'.format('4-1212', 'Jim Beene', 'Off'))

listbox.bind('<<ListboxSelect>>', on_selection)

e1 = tk.Entry(root)
e1.pack()

e2 = tk.Entry(root)
e2.pack()

e3 = tk.Entry(root)
e3.pack()

root.mainloop()

enter image description here

Sign up to request clarification or add additional context in comments.

12 Comments

Holy thats great - I really appreciate your help
AttributeError: 'tuple' object has no attribute 'strip' - is the error I get for database values. I am pulling from three different columns in the Mysql table so it is seeing that. If I have only one cell or column it works fine but this is not my situation.
As seen in console ('2-12312', 'Bob', 'Seesaw', None), ('1-3212', 'Jen', 'Jenny', None) from Mysql SELECT query
I use strip() with single string but you have row with many elements. If you need to strip something then you have to do it with every element separately - ie. id = row[0].strip(). But your tuple has data which doesn't need to strip anything.
duprows = [ ' '.join(row) for row in rows] I have tried adding JOIN but no success - keeps erroring out
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.