2017-01-08 8 views
0

ListBoxを使用して検索GUIを作成しようとしています。入力した文字列と一致するデータがリストにある場合、リストボックスにデータを出力します。検索ボタンを使用してリストからデータを出力するpython

一致するデータを取得してListBoxに表示する際に問題が発生しています。リスト内のすべてのデータが表示されます。 Pythonの新機能以下は私が遠くまで持っているものです。あなたは、Python

if "dylan" in "bob dylan": # True 

if "bob dylan".startswith("dylan"): # False 

if "dylan" in "Bob Dylan": # False 

if "dylan".lower() in "Bob Dylan".lower(): # True 

の基礎を必要とする

おかげ

from tkinter import * 


w = Tk() 
liststuff = ["bob", "john", "theo", "bobby"] 
l1 = Label(w, text='Name') 
l1.grid(row=0, column=0) 

title_text = StringVar() 
e1 = Entry(w, textvariable=title_text) 
e1.grid(row=0, column=1) 

list1 = Listbox(w, height=0, width=35) 
list1.grid(row=1, rowspan=4, columnspan=2) 

sb1 = Scrollbar(w) 
sb1.grid(row=1, column=3) 


def search_command(): 

    list1.delete(0, END) 
    for x in liststuff: 
     list1.insert(END, x) 


list1.configure(yscrollcommand=sb1.set) 
sb1.configure(command=list1.yview) 

b1 = Button(w, text="Search", width=12, command=search_command) 
b1.grid(row=0, column=4) 

w.mainloop() 
+0

あなたの問題は、tkinterではなくPythonの基本です。 'A in B'または' A.startswith(B) 'ならば – furas

答えて

0

今、あなたはButtonせずに検索するには、リストに

import tkinter as tk 

# --- functions --- 

def search_command(event=None): 

    # to compare lower case 
    text = e1.get().lower() 

    list1.delete(0, 'end') 

    if text: # search only if text is not empty 
     for word in liststuff: 
      if word.lower().startswith(text): 
       list1.insert('end', word) 

# --- main --- 

liststuff = ["bob", "john", "theo", "bobby"] 

# GUI 

root = tk.Tk() 

l1 = tk.Label(root, text='Name') 
l1.grid(row=0, column=0) 

title_text = tk.StringVar() 

e1 = tk.Entry(root, textvariable=title_text) 
e1.grid(row=0, column=1) 
e1.bind('<KeyRelease>', search_command) 

list1 = tk.Listbox(root, height=0, width=35) 
list1.grid(row=1, rowspan=4, columnspan=2) 

sb1 = tk.Scrollbar(root) 
sb1.grid(row=1, column=3) 

list1.configure(yscrollcommand=sb1.set) 
sb1.configure(command=list1.yview) 

b1 = tk.Button(root, text="Search", width=12, command=search_command) 
b1.grid(row=0, column=4) 

root.mainloop() 

を検索することができ、あなたは012に<KeyRelease>をバインドすることができます

e1 = tk.Entry(root, textvariable=title_text) 
e1.grid(row=0, column=1) 

e1.bind('<KeyRelease>', search_command) 

、それはEntry

の全てのキーの後にsearch_commandを実行します。しかし、それはあなたが

def search_command(event): 

たり、機能を使用する場合を受信する必要が一つの引数eventsearch_command()を実行しますbindcommand=と同時に表示されます。

def search_command(event=None): 
関連する問題