简体   繁体   English

从Tkinter Entry小部件自动更新变量

[英]Automatically Update a Variable from Tkinter Entry Widget

I am trying to make a real-time searching algorithm in GUI with tkinter in Python 3. So I have this window and entry box for typing keywords in for searching: 我正在尝试使用Python 3中的tkinter在GUI中创建实时搜索算法。因此,我具有此窗口和输入框,用于在其中输入关键字进行搜索:

from tkinter import *

root = Tk()
root.geometry('800x400')
root.resizable(False,False)
root.title('Window')

var = StringVar()

root.SearchBox = ttk.Entry(root, textvariable=var, font='Fixedsys 28')
root.SearchBox.place(relx=0.5, rely=0.5, height=50, width=400, anchor="center")
search_input = var.get()

What should I put here, without an enter button for the search box 我应该在这里放什么,没有用于搜索框的输入按钮

...Some codes here...

To get the following results? 得到以下结果?

When the tkinter window is already open and some keywords typed into the search entry box 当tkinter窗口已经打开并且在搜索输入框中键入了一些关键字时

>>>print(search_input)
Search Terms

and as soon as the search entry box search_input =! '' 并尽快在搜索输入框search_input =! '' search_input =! '' how could I get a global variable to change, say is_entry_box_empty = False to is_entry_box_empty = True search_input =! ''如何更改全局变量,例如is_entry_box_empty = False更改为is_entry_box_empty = True

I found this to be impossible without threading, since the GUI window will block any codes after it. 我发现如果没有线程,这是不可能的,因为GUI窗口将阻止所有代码。

Many thanks 非常感谢

You can trace changes in the textvariable: 您可以跟踪text变量中的更改:

var.trace('w',var_change_callback_function)

and write a function to deal with the changes. 并编写一个函数来处理更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM