选中行记录如何显示相关条目python

Selected Row record how to diplayed relavent Entry python

我是 python 的初学者 programming.i 正在 python.while 制作 crud 系统 制作系统 运行 如果我 select来自 table 的行记录 selected 显示在相关 textboxes.how 上的行记录以实现 this.i 不要 know.what 我到目前为止已尝试 我已附加 below.i编辑了代码

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()

我刚刚定义了两个函数 work()clear() 并给了它按钮

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)

然后...

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)

如果你不想使用按钮,试试这个

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

并传入 event 作为参数,如 def work(event):

尝试添加这些并告诉我

干杯