python tkinter等待用户输入输入

huangapple go评论52阅读模式
英文:

python tkinter waiting for an input to be inputted by user

问题

我有一个代码片段。输入小部件创建自己并设置文本,然后等待若干秒后销毁自己。

entry_var_temporary = tk.StringVar()
entry_var_temporary.set(varsoundTitle_usernameHeroContainer)
entry_shtname_temp = tk.Entry(canvas2, width=30, textvariable=entry_var_temporary)
entry_shtname_temp.pack()
entry_shtname_temp.focus_set()
root.update()
time.sleep(10)
entry_shtname_temp.destroy()
root.update()

我已经等待了10秒,让用户修改文本,如果需要的话。但我发现,time.sleep 不允许修改输入小部件。如何解决这个问题?

通过使用 wait.window(),我意识到可以在小部件内编辑文本,但我的问题是,这不是强制性的。因此,如果用户没有在其中输入任何文本,那么在10秒后它需要被销毁。

英文:

I have a code snippet. Entry widget creates itself with text, then waits seconds then destroys itself.

    entry_var_temporary = tk.StringVar()
    entry_var_temporary.set(varsoundTitle_usernameHeroContainer)
    entry_shtname_temp=tk.Entry(canvas2,width=30,textvariable=entry_var_temporary)
    entry_shtname_temp.pack()
    entry_shtname_temp.focus_set()
    root.update()
    time.sleep(10)
    entry_shtname_temp.destroy()
    root.update()

I have put 10 seconds to wait, and let user to modify the text, if it wants so. But as I see, time.sleep does not let the entry widget to be modified.
How can I get around this problem?
With wait.window() I realized I can edit text inside widget, but my problem is, it is not compulsory. So, if user doesn't put any text on it, it then after 10 sec needs to be destroyed

答案1

得分: 1

使用after方法与destroy方法作为回调函数。

使用root.after而不是time.sleep的主要原因是time.sleep会停止线程,导致代码完全停止,而root.after是线程安全的,因为它是基于回调实现的。

您的小部件将在设置的一段时间后自动销毁。

例如:

import tkinter as tk

root = tk.Tk()

entry_var_temporary = tk.StringVar()
entry = tk.Entry(root, textvariable=entry_var_temporary)
entry.pack()
root.after(10000, entry.destroy)  # 10000毫秒
root.mainloop()
英文:

Use the after method with the destroy method as the callback.

The main reason to use root.after vs time.sleep is the time.sleep stops the thread causing the code to completely stop compared to root.after which is thread-safe because it is implemented in terms of call.

Your widget will destroy itself automatically after a set amount of time.

for example:

import tkinter as tk


root = tk.Tk()

entry_var_temporary = tk.StringVar()
entry = tk.Entry(root,  textvariable=entry_var_temporary)
entry.pack()
root.after(10000, entry.destroy)  # 10000 milliseconds
root.mainloop()

答案2

得分: 1

这似乎是以前的问题的扩展,您只需要在该示例中添加一行代码,以便在10秒后自动删除输入框小部件。

在以下示例中,请注意我如何使用after在10秒内销毁窗口。这个调用必须在等待窗口之前执行。这是我在您以前的问题中提供的示例,只是添加了一个新的语句:

entry_var = tk.StringVar()
new_sheetname_entryBox = tk.Entry(canvas2, width=30, textvariable=entry_var)
new_sheetname_entryBox.pack()
new_sheetname_entryBox.bind("<Return>", lambda event: new_sheetname_entryBox.destroy())
new_sheetname_entryBox.focus_set()

# 等待输入小部件被删除,但如果用户不响应,自动在10秒后删除它。
root.after(10000, new_sheetname_entryBox.destroy)
new_sheetname_entryBox.wait_window()
英文:

This seems to be an extension of a previous question you asked. You only need to add one more line of code to that example in order to automatically delete the entry widget after 10 seconds.

In the following example, notice how I use after to destroy the window in 10 seconds. This call must be done before waiting for the window. This is the example I gave in your previous question, with just that one new statement added:

entry_var = tk.StringVar()
new_sheetname_entryBox=tk.Entry(canvas2,width=30, textvariable=entry_var)
new_sheetname_entryBox.pack()
new_sheetname_entryBox.bind(&quot;&lt;Return&gt;&quot;, lambda event: new_sheetname_entryBox.destroy())
new_sheetname_entryBox.focus_set()

# wait for the entry widget to be deleted, but automatically
# delete it after 10 seconds if the user doesn&#39;t respond.
root.after(10000, new_sheetname_entryBox.destroy)
new_sheetname_entryBox.wait_window()

huangapple
  • 本文由 发表于 2023年2月6日 04:22:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定