Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何选择Python列表框数据并将其填充到特定的enty box-pretype_Python_Mysql_Tkinter - Fatal编程技术网

如何选择Python列表框数据并将其填充到特定的enty box-pretype

如何选择Python列表框数据并将其填充到特定的enty box-pretype,python,mysql,tkinter,Python,Mysql,Tkinter,到目前为止,一切都正常工作,但我正在努力获取下面的数据,一次鼠标高亮显示,一次点击选择按钮填充正确的输入框。这可能要求太高了 列表框显示 2-12312 Bob Seesaw Active 4-1212 Jim Beene Off 因此,如果我高亮显示Bob跷跷板行并点击select。2-12312将预输入'num_input',Bob'nam_input'等。这样做的目的是,我可以键入一个新的分配,并更新该条目的数据库。我粘贴了特定于我的计划的代码 from tkinter import

到目前为止,一切都正常工作,但我正在努力获取下面的数据,一次鼠标高亮显示,一次点击选择按钮填充正确的输入框。这可能要求太高了

列表框显示

2-12312 Bob Seesaw Active

4-1212 Jim Beene Off
因此,如果我高亮显示Bob跷跷板行并点击select。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)
-


我不想失去键入名称或数字作为条目并搜索数据库的功能。

列表框将所选元素指定为strin,我将此文本格式化为带有

'{:10s}|{:15s}|{:10s}'
因此,我可以使用切片从正确的位置获取数据,并剥离以删除空格

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

GitHub上的示例我是Python新手,不太了解如何按照我的示例预先填充输入框。这一部分很明显是一个高级设置,为什么我会挣扎当您选择列表中的元素,然后将执行功能,您必须手动将所选数据从列表框复制到条目中。如何将数字和名称分离,并将其放入正确的输入框中?您有字符串,因此使用切片、拆分、,strip和其他字符串函数。太好了-我非常感谢您的帮助AttributeError:“tuple”对象没有属性“strip”-这是我在数据库值中遇到的错误。我从Mysql表中的三个不同列中提取数据,因此它可以看到这一点。如果我只有一个单元格或列,它可以正常工作,但这不是我的情况。如控制台“2-12312”、“Bob”、“Seesaw”、“None”、“1-3212”、“Jen”、“Jenny”中所示,Mysql中的None SELECT query使用带单个字符串的条带,但您有带许多元素的行。如果需要剥离某些内容,则必须分别对每个元素进行剥离-即.id=行[0]。剥离。但是您的元组包含的数据不需要剥离任何内容。duprows=[''.joinrow for row for row in rows]我已尝试添加JOIN,但没有成功-不断出错
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()