如何在没有大括号或附加符号的情况下将值插入 tkinter 条目? [英] How to insert value into tkinter entry without curly brackets or additional symbols?

查看:17
本文介绍了如何在没有大括号或附加符号的情况下将值插入 tkinter 条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是我正在创建一个表单,目前我需要在 tk.Entry 中输入条目才能保存和加载.我已经到了它可以工作的地步,但是任何包含多个单词的条目都被视为元组,加载时在输入框中显示为 {what's this for?},再次保存时它变为 {{what's this for?}} 等.我已经考虑过使用 print 命令将元组设置为字符串值,但我不确定在这种情况下如何做到这一点.

So my issue is I'm creating a form and currently I need entries into the tk.Entry to be able to be saved and loaded. I have it to the point where it works, however any entries with multiple words are treated as tuples and when loaded appear as {what's this for?} in the entry box and when saved again it becomes {{what's this for?}} etc. I've considered using the print command to get the tuple to a string value, but I'm not sure how to do that in this instance.

附言如果有人能告诉我如何替换加载时添加的文本而不是添加文本,那就太好了.

p.s. If anyone who could tell me how to replace text added when loaded instead of adding, that would be great.

def ldload():
    f=open(ldcreds, 'r')
    content = f.readlines()
    l = [x.strip() for x in content] 
    ldsplitlists = [i.split(", ") for i in l]
    Characterentx, Playerentx = ldsplitlists
    Characterent.insert(0, Characterentx)
    Playerent.insert(0, Playerentx)

如果有帮助,我将如何保存它

and here is how I save it if it will help

def ldsave():
with open(ldcreds, 'w') as f:
        f.write(Characterent.get()) 
        f.write('\n') 
        f.write(Playerent.get()) 
        f.write('\n')

是的,我确实关闭了它,我只是遗漏了大部分变量.

yes I do close it, I just left out most of the variables.

感谢您的帮助,如果我遗漏了什么,请告诉我

I appreciate all your help, let me know if I am missing something

推荐答案

这里是一个简单程序的例子,它将接收 3 个字段并将它们保存到主目录中名为 test_text 的文件中.

Here is an example of a simple program that will take in 3 fields and save them to a file called test_text in your main directory.

当按下加载按钮时,通过使用一些 if 语句,可以使用同一个文件加载到每个字段.

That same file can be used to load to each field through the use of some if statements when the load button is pressed.

这应该是一个很好的例子,可以帮助您解决问题.

This should be a good example to assist you with your problem.

import tkinter as tk

class App(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        self.master = parent
        self.save_btn = tk.Button(self.master, text = "Save fields", command= self.ldsave)
        self.save_btn.grid(row=0, column=0)
        load_btn = tk.Button(self.master, text = "Load fields", command= self.ldload)
        load_btn.grid(row=0, column=1)

        label1 = tk.Label(self.master, text = "First Name: ")
        label1.grid(row=1, column=0)
        self.entry1 = tk.Entry(self.master)
        self.entry1.grid(row=1, column=1)

        label2 = tk.Label(self.master, text = "Last Name: ")
        label2.grid(row=2, column=0)
        self.entry2 = tk.Entry(self.master)
        self.entry2.grid(row=2, column=1)

        label3 = tk.Label(self.master, text = "Age: ")
        label3.grid(row=3, column=0)
        self.entry3 = tk.Entry(self.master)
        self.entry3.grid(row=3, column=1)



    def ldsave(self):
        list_of_entries = [self.entry1.get(), self.entry2.get(), self.entry3.get()]
        with open("./test_text", 'w') as f:
            for item in list_of_entries:
                f.write("{}\n".format(item))

    def ldload(self):
        try:
            with open("./test_text", 'r') as f:
                content = f.readlines()
                content = [x.strip() for x in content]
                for i in range(3):
                    if i == 0:
                        self.entry1.delete(0,tk.END)
                        self.entry1.insert(0,content[i])
                    if i == 1:
                        self.entry2.delete(0,tk.END)
                        self.entry2.insert(0,content[i])
                    if i == 2:
                        self.entry3.delete(0,tk.END)
                        self.entry3.insert(0,content[i])
        except:
            print("File test_text does not exist")



if __name__ == "__main__":
    root = tk.Tk() 
    my_app = App(root)
    tk.mainloop()

这篇关于如何在没有大括号或附加符号的情况下将值插入 tkinter 条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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