python tkinter列表框在每次按钮点击时更新

时间:2015-04-07 23:25:29

标签: tkinter

下面是我试图在文本框中搜索模式然后在列表框中获取结果的代码段。单击列表框中的每一行,它将显示文本框。

我面临一个问题,每次点击“搜索”按钮,它都会创建一个新的列表框,而不是更新父列表框。我怎样才能做到这一点。请指教。

import pysftp
import sys
import spur
import paramiko
from Tkinter import *



message =""


ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
    paramiko.AutoAddPolicy())
ssh.connect('hostname', username='user', password='password')



root = Tk()


L1 = Label(root, text="Pattern")
L1.pack( side = LEFT)
E1 = Entry(root, bd =5)

E1.pack(side = LEFT)

def OnDouble(event):
        master = Tk()
        widget = event.widget
        selection=widget.curselection()
        value = widget.get(selection[0])
        #print "selection:", selection, ": '%s'" % value
        command="~/Format.sh "+ value
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)
        data = ssh_stdout.readlines()
        xscrollbar = Scrollbar(master, orient=HORIZONTAL)
        yscrollbar = Scrollbar(master)
        yscrollbar.pack( side = RIGHT, fill=Y )
        xscrollbar.pack( side = BOTTOM, fill=X )
        #print data
        text = Text(master, width=100,yscrollcommand = yscrollbar.set, xscrollcommand = xscrollbar.set)
        for line in data:
         text.insert(INSERT, line)
        text.insert(END, "Bye Bye.....")
        text.pack(side = LEFT, fill = BOTH)
        xscrollbar.config( command = text.xview)
        yscrollbar.config( command = text.yview)



def callback():
    message= E1.get()
    command="~/IndexSearch_py.sh "+ message
    ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)


    data = ssh_stdout.readlines()
    xscrollbar = Scrollbar(root, orient=HORIZONTAL)
    yscrollbar = Scrollbar(root)
    yscrollbar.pack( side = RIGHT, fill=Y )
    xscrollbar.pack( side = BOTTOM, fill=X )

    mylist = Listbox(root, width=80,yscrollcommand = yscrollbar.set, xscrollcommand = xscrollbar.set )
    length=len(data);
    for i in range(length):
     mylist.insert(END, data[i])
    print mylist.size()




    mylist.bind("<Double-Button-1>", OnDouble)
    mylist.pack( side = LEFT, fill = BOTH )

    xscrollbar.config( command = mylist.xview)
    yscrollbar.config( command = mylist.yview)




b = Button(root, text="Search", width=20, command=callback)
b.pack()


mainloop()

0 个答案:

没有答案