到目前为止,一切工作正常,但是我很难获取下面的数据,一旦被鼠标突出显示,一旦点击选择按钮来填充正确的输入框。这可能要求太多了。
列表框显示
2-12312 Bob Seesaw Active
4-1212 Jim Beene Off
因此,如果我突出显示 Bob Seesaw 行并点击选择。 2-12312 将预先输入“num_input”、Bob“nam_input”等。这样做的目的是让我可以输入新的作业并更新该条目的数据库。 (我粘贴了特定于我的计划的代码。
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)
我不想失去输入姓名或号码作为条目并搜索数据库的功能。
最佳答案
列表框将选定的元素提供为字符串,我将此文本格式化为列
'{:10s}|{:15s}|{:10s}'
所以我可以使用切片从正确的位置获取数据,并使用 strip()
删除空格。
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()
关于python - 如何选择 Python 列表框数据并将其填充到特定的实体框 - 预输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47775715/