保存和重新加载 tkinter 条目字段值 [英] Saving and reloading tkinter entry filed values

查看:28
本文介绍了保存和重新加载 tkinter 条目字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个 tkinter 应用程序.用户可以输入一些设置值.其中一项设置是日期.我想要做的是,用户可以输入设置值并保存设置,甚至在关闭应用程序后,重新打开后,加载按钮应该将设置值带回输入字段.我这样做是成功的,但只针对一个领域.如何保存多个输入字段并重新加载,包括日期输入?有什么建议吗?

I am trying to develop a tkinter app. User can enter some setting values. One of the settings is a date. What I am trying to do is, user can enter settings values and save the settings and even after closing the app, after reopening, a load button should bring back the settings values to entry field. I was successful in doing so, but for just one field. How to save multiple entry fields and reload it, including date entry? any suggestions?

from tkinter import *
import pickle
from tkcalendar import Calendar, DateEntry

root = Tk()
root.geometry("400x400")

my_text1 = StringVar()
my_text2 = StringVar()
my_date = StringVar()

entry1 = Entry(root, textvariable=my_text1)
entry1.pack()

entry2 = Entry(root, textvariable=my_text2)
entry2.pack()

entry3 = DateEntry(root, textvariable=my_date)
entry3.pack()


def save():
    text = my_text1.get()
    with open("saved_settings.dat", "wb") as pickle_file:
        pickle.dump(text, pickle_file, pickle.HIGHEST_PROTOCOL)

def clear():
    my_text1.set('')

def load():
    with open("saved_settings.dat", "rb") as pickle_file:
        text = pickle.load(pickle_file)
    my_text1.set(text)

Button(root, text="Save Settings", command=save).pack()
Button(root, text="Clear", command=clear).pack()
Button(root, text="Load Settings", command=load).pack()
root.mainloop()


推荐答案

用所有值创建字典,然后pickle这个字典.

Create dictionary with all values and then pickle this dictinary.

开始时,您可以从文件加载字典并使用字典中的值在小部件中设置值.

At start you can load dictionary from file and use values from dictionary to set values in widgets.

from tkinter import *
import pickle
from tkcalendar import Calendar, DateEntry

# --- functions ---

def save():
    config = {
        'my_text1': my_text1.get(),
        'my_text2': my_text2.get(),
        'my_date': my_date.get(),
    }

    with open("saved_settings.dat", "wb") as pickle_file:
        pickle.dump(config, pickle_file, pickle.HIGHEST_PROTOCOL)

def clear():
    my_text1.set('')
    my_text2.set('')
    my_date.set('')

def load():
    with open("saved_settings.dat", "rb") as pickle_file:
        config = pickle.load(pickle_file)

    my_text1.set(config.get('my_text1'))
    my_text2.set(config.get('my_text2'))
    my_date.set(config.get('my_date'))


# --- main ---

root = Tk()
root.geometry("400x400")

my_text1 = StringVar()
my_text2 = StringVar()
my_date = StringVar()

entry1 = Entry(root, textvariable=my_text1)
entry1.pack()

entry2 = Entry(root, textvariable=my_text2)
entry2.pack()

entry3 = DateEntry(root, textvariable=my_date)
entry3.pack()

Button(root, text="Save Settings", command=save).pack()
Button(root, text="Clear", command=clear).pack()
Button(root, text="Load Settings", command=load).pack()

# load at start
load()

root.mainloop()

您还可以使用 shelve 代替 picklea> 或纯文本文件,如 JSON、INI、YALM,您可以在文本编辑器中打开和编辑这些文件,或者在其他不能使用 Python 的 pickle 的程序(任何其他语言)中打开.

Instead of pickle you can also use shelve or plain text files like JSON, INI, YALM which you can open in text editor and edit, or open in other program (in any other language) which can't use python's pickle.

你也可以使用数据库——比如不运行服务器的SQLite.

You can also use database - like SQLite which doesn't run server.

这篇关于保存和重新加载 tkinter 条目字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆