How to insert value into tkinter entry without curly brackets or additional symbols?

前端 未结 2 1247
北海茫月
北海茫月 2021-01-23 20:53

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

相关标签:
2条回答
  • 2021-01-23 21:31

    I solved the curly braces around entries when inserting data into a listboxes by just looping over the tuple ( a tuple was returned in my case from a SQL cursor.fetchall() ) and then selecting the index number for the listbox insert. That stripped the curly braces from displaying in the listbox.

    for columns in feedback:
        captured_columns_list.insert(END, columns[0])
    
    0 讨论(0)
  • 2021-01-23 21:46

    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.

    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()
    
    0 讨论(0)
提交回复
热议问题