使用上一会话 Tkinter 中的输入框数据
我需要使用 Tkinter 创建一个 GUI 应用程序,需要大量文本字段条目。用户必须输入所有条目,然后使用“运行”按钮来执行特定功能。我想要做的是:包含一个功能,可以恢复用户在上一个会话中输入的所有文本数据,这样用户就不必每次都一次又一次地输入所有数据。我可以使用 Entry_box.set('desired string') 选项,但这是静态的,并且不考虑之前的 GUI 会话。
例如,在以下脚本中:
#!usr/bin/env python
from Tkinter import *
# Define input retrieve function for application input
def retrieve_text():
print(app_entry.get())
if __name__ == "__main__":
# Create window (or form)
app_win = Tk()
# Create label
app_label = Label(app_win, text="Enter value")
app_label.pack()
# Create entry box
app_entry = Entry(app_win)
app_entry.pack()
# Create button
app_button = Button(app_win, text="Print Value", command=retrieve_text)
app_button.pack()
# Initialize GUI loop
app_win.mainloop()
我运行此 GUI 并在输入框中输入“Value1”。然后,我想关闭窗口,重新运行它,默认情况下会出现条目“Value1”。每个会话应显示前一个会话的值(该值仅在第一次手动输入)。有什么办法可以做到这一点吗?
这将非常有用,因为在我的例子中,每次重新运行 GUI 时,用户只需更改大约 10 个字段中的一两个字段。
I need to create a GUI application with Tkinter that requires a lot of text-field entries. The user has to input all entries, then use a 'Run' button to execute a particular function. What I want to do is: include a functionality that restores ALL text data entered by the user from a previous session, so that the user doesn't have to enter all the data again and again each time. I could use the entry_box.set('desired string') option, but this is static and does not take into account the previous GUI sessions.
For example, in the following script:
#!usr/bin/env python
from Tkinter import *
# Define input retrieve function for application input
def retrieve_text():
print(app_entry.get())
if __name__ == "__main__":
# Create window (or form)
app_win = Tk()
# Create label
app_label = Label(app_win, text="Enter value")
app_label.pack()
# Create entry box
app_entry = Entry(app_win)
app_entry.pack()
# Create button
app_button = Button(app_win, text="Print Value", command=retrieve_text)
app_button.pack()
# Initialize GUI loop
app_win.mainloop()
I run this GUI and enter 'Value1' in the entry box. Then, I want to close the window, re-run it and the entry 'Value1' to appear by default. Each session should display the value from the previous session (The value is manually entered only the first time). Is there any way to do this?
This would be very useful because in my case, a user only has to change one or two fields out of around 10 each time the GUI is rerun.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
main
中,添加一个处理程序以使用它来关闭事件。现在,每当按下关闭按钮时,都会调用函数
handler
。现在,您可以修改您的程序来读取文件&设置脚本重新启动时的文本。
In your
main
, add a handler to close event using this.Now, whenever the close button will be pressed, the function
handler
will be called.Now, you can modify your program to read the file & set the text when the script is restarted.