【问题标题】:Show the possibilities while typing in entry field tkinter在输入字段 tkinter 中键入时显示可能性
【发布时间】:2022-08-17 19:08:43
【问题描述】:

当用户开始在输入字段中输入时,我希望在 tkinter 输入字段下方显示的可能性,就像您在 Google 搜索栏中输入一样。可能性被放置在水果数组中。

import tkinter as tk

window = tk.Tk()

fruits = [\"Apple\", \"Banana\", \"Peach\", \"Pear\"]

label = tk.Label(text=\"Fruit\")
label.grid(row=0, column=0)

entry = tk.Entry()
entry.grid(row=0, column=1)

window.mainloop()

你可以在这里找到我想要的图片:https://i.stack.imgur.com/pnozv.png(我还不能发布图片)

如何使此输入字段与 Google 搜索栏一样?

  • 也许this 可以提供帮助

标签: python tkinter tkinter-entry


【解决方案1】:
# Import the Required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame or window
win= Tk()

# Update the Entry widget with the selected item in list
def check(e):
   v= entry.get()
      if v=='':
      data= values
   else:
      data=[]
      for item in values:
         if v.lower() in item.lower():
            data.append(item)
   update(data)

def update(data):
   # Clear the Combobox
   menu.delete(0, END)
   # Add values to the combobox
   for value in data:
      menu.insert(END,value)


# Add a Label widget
label= Label(win, text= "Demo Combobox Widget", font= ('Helvetica 15
bold'), background= "green3")
label.pack(padx= 10, pady= 25)

# Add a Bottom Label
text= Label(win, text="Select your prefered fruit")
text.pack(padx= 15,pady= 20)

# Create an Entry widget
entry= Entry(win, width= 35)
entry.pack()
entry.bind('<KeyRelease>',check)

# Create a Listbox widget to display the list of items
menu= Listbox(win)
menu.pack()

# Create a list of all the menu items
values= ["Apple", "Banana", "Peach", "Pear"]
# Add values to our combobox
update(values)

# Binding the combobox onclick

win.mainloop()

【讨论】:

  • 感谢您尝试提供帮助。这不完全是我的意思。对于这个例子,我只使用了 4 个值,但我将使用 60 多个值的列表。我不希望列表在用户开始输入之前可见。此外,当正确的值可见时,我希望用户能够单击它或使用箭头键来选择和使用该值。就像谷歌搜索栏一样。
  • 也许这是您正在寻找的东西:stackoverflow.com/questions/55649709/…
猜你喜欢
  • 1970-01-01
  • 2011-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 2018-06-20
  • 2020-07-05
相关资源
最近更新 更多