Selected Row记录如何显示相关条目python [英] Selected Row record how to diplayed relavent Entry python

查看:34
本文介绍了Selected Row记录如何显示相关条目python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python编程的初学者.我正在python上制作crud系统.而使系统遇到问题,如果我从相关文本框上显示的表中选择行记录中选择行记录.如何实现这一点.i不知道.到目前为止我尝试了什么,我附在下面.我编辑了代码

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

推荐答案

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

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)

然后……

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)

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

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

尝试添加这些并告诉我

干杯

这篇关于Selected Row记录如何显示相关条目python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆