Hàng đã chọn ghi lại cách diplayed relavent Entry python

Aug 29 2020

tôi là người mới bắt đầu lập trình python. tôi đang tạo hệ thống crud trên python. trong khi làm cho hệ thống gặp sự cố nếu tôi chọn bản ghi hàng từ bản ghi hàng đã chọn được hiển thị trên relavent textboxes. làm thế nào để đạt được điều này. tôi không biết. những gì tôi đã thử cho đến nay, tôi đã đính kèm bên dưới. tôi đã chỉnh sửa mã

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

Trả lời

1 CoolCloud Aug 29 2020 at 07:40

Tôi vừa xác định hai chức năng được gọi work()clear()và cho nó các nút

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)

và sau đó...

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)

Nếu bạn không muốn sử dụng nút, hãy thử điều này

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

và chuyển vào eventdưới dạng một tham số nhưdef work(event):

Hãy thử thêm những thứ này và cho tôi biết

Chúc mừng