Search code examples
pythonmysqltkintertreeview

Selected Row record how to diplayed relavent Entry python


i am a beginner of python programming.i am making crud system on python.while make a system ran into the problem with if i select the row record from table selected row record displayed on relavent textboxes.how to achieve this.i don't know.what i tried so far i attached below.i edited the code

import tkinter as tk
from tkinter import ttk
import mysql.connector
from tkinter import *


def work():
    e1.delete(0, END)
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)
    row_id = listBox.selection()[0]
    select = listBox.set(row_id)
    e1.insert(0,select['id'])
    e2.insert(0,select['stname'])
    e3.insert(0,select['course'])
    e4.insert(0,select['fee'])

def show():
        mysqldb = mysql.connector.connect(host="localhost", user="root", password="", database="smschool")
        mycursor = mysqldb.cursor()
        mycursor.execute("SELECT id,stname,course,fee FROM record")
        records = mycursor.fetchall()
        print(records)

        for i, (id,stname, course,fee) in enumerate(records, start=1):
            listBox.insert("", "end", values=(id, stname, course, fee))
            mysqldb.close()




root = Tk()

root.geometry("800x800")
global e1
global e2
global e3
tk.Label(root, text="Student ID").place(x=10, y=10)
Label(root, text="Student Name").place(x=10, y=40)
Label(root, text="Course").place(x=10, y=70)
Label(root, text="Fee").place(x=10, y=100)

e1 = Entry(root)
e1.place(x=140, y=10)

e2 = Entry(root)
e2.place(x=140, y=40)

e3 = Entry(root)
e3.place(x=140, y=70)

e4 = Entry(root)
e4.place(x=140, y=100)


Button(root, text="update",command = show,height=3, width= 13).place(x=140, y=130)
Button(root, text="work",command = work,height=3, width= 13).place(x=180, y=130)


cols = ('id', 'stname', 'course','fee')
listBox = ttk.Treeview(root, columns=cols, show='headings' )


for col in cols:
    listBox.heading(col, text=col)
    listBox.grid(row=1, column=0, columnspan=2)
    listBox.place(x=10, y=200)

show()
listBox.bind('<Double-Button-1>',work)
root.mainloop()

Solution

  • I just defined two function called work() and clear() and gave it buttons

    Button(root, text="work",command = work,height=3, width= 13).place(x=140, y=130)
    Button(root, text="Clear",command = clear,height=3, width= 13).place(x=350, y=130)
    
    

    and then...

    def work():
        row_id = listBox.selection()[0]
        select = listBox.set(row_id)
        e1.insert(0,select['id'])
        e2.insert(0,select['stname'])
        e3.insert(0,select['course'])
        e4.insert(0,select['fee'])
    
    def clear():
        e1.delete(0,END)
        e2.delete(0,END)
        e3.delete(0,END)
        e4.delete(0,END)
    

    If you dont want to use button, then try this

    listBox.bind('<Double-Button-1>',work)
    

    and pass in event as a parameter like def work(event):

    Try adding these and let me know

    Cheers