简体   繁体   中英

Saving and searching data with Tkinter entry boxes

This might be a strange question because I am new to Python.

I am trying to create form in Python which data can be entered into boxes and saved, then opened again. I'm currently using Tkinter to create a Gui which has entry boxes and buttons:

import sys
from tkinter import * 
def mstore():
pass
return

def msearch():
file_path = filedialog.askopenfilename()
return



mGui=Tk()

mGui.geometry('450x450+200+200')
mGui.title('Form Test')

#Top
mTitle = Label (mGui,text='Heading Text',bg='white').grid(row=1,column=1)
mDetail = Label (mGui,text='Flavour you can see',bg='white').grid(row=2,column=1)


#Entry Boxes
mFName = Label (mGui,text='Barcode',bg='white').grid(row=3,column=1)
mEntryname = Entry().grid(row=3,column=2)


#Buttons
mSave = Button (mGui,text='Save',bg='white', command = mstore).grid(row=4,column=1)
mSearch = Button (mGui,text='Search',bg='white', command = msearch).grid(row=5,column=1)




mGui.mainloop()

The search was going to be used to open up a file which has been saved before and fill in the boxes with that data, however before that I need help saving the data in a way it will be retrievable - All the information I find is about web-forms. I have also tried saving information with SQLite3 but I found that to not be quite what I was looking for. Any help/guidance will be appreciated. Thanks,

Hello Gregulimy!

I have simplified your code and made it do what you want it to do. I have left comments explaining what the code does. If you have any questions about what I have done feel free to ask!

from tkinter import *

def mstore(text):
    file = open("file.txt", "w")            # Create file.txt
    file.write(text)                        # Write contents of mEntryname to file
    file.close()                            # Closes text file

def msearch():
    file = filedialog.askopenfilename()     # Stores file directory that user chose
    open_file = open(file, 'r')             # Opens file user chose
    print(open_file.read())                 # Displays contents in console
    open_file.close()                       # Closes text file

# Window Creation and Settings
window = Tk()
window.geometry('450x500')          
window.title('Form Test')

# Create Widgets
mTitle = Label (window,text='Heading Text',bg='white')
mDetail = Label (window,text='Flavour you can see',bg='white')
mFName = Label (window,text='Barcode',bg='white')
mEntryname = Entry(window)
# Runs mstore function when pressed (passing the contents of the entry box)
mSave = Button (window,text='Save',bg='white', command = lambda: mstore(mEntryname.get()))
# Runs msearch function when pressed
mSearch = Button (window,text='Search',bg='white', command = lambda: msearch())

# Render Widgets
mTitle.pack()
mDetail.pack()
mFName.pack()
mEntryname.pack()
mSave.pack()
mSearch.pack()

window.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM