[Tkinter] Trying to add data into a shelf from a submit button - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Trying to add data into a shelf from a submit button (/thread-39000.html) |
Trying to add data into a shelf from a submit button - TWB - Dec-18-2022 I am new to Python and trying to run a small program that takes a input from a text field and submit it into a shelf. The code runs with no errors but does not input any data Thanks for any direction you may give # Sample Solution from tkinter import * import shelve class MyFrame (Frame): def __init__(self): Frame.__init__(self) self.master.geometry("600x600") self.master.title("Student Scores") self.grid() #open text file student_scores = shelve.open('student_name', 'c') #Display menue here here self.student_name = StringVar() # Prompt, Entry and Button... will be more buttons added eventually self.prompt = Label(self, text = "Select one of the 3 menu options") self.prompt.grid(row = 0, column = 0) #Radio Button Menu options #Button to enter students name self.button_selected = IntVar() self.button_selected.set(1) #self.button_selected.get() == 1 self.menu_button = Radiobutton(self, text = "Add student name", variable = self.button_selected, value = 1) self.menu_button.grid(row = 3, column = 0) self.prompt = Label(self, text = "Enter students name: ") self.prompt.grid(row = 7, column = 0) #text box self.input = Entry(self) self.input.grid(row = 7, column = 1) # submit button self.button_submit = Button(self, text = "Submit", command = self.set_names) self.button_submit.grid(row = 7, column = 2) #print(list(student_scores.values())) #close test file student_scores.close() #function to set student names def set_names(self): student_name = [self.student_name.get()] print("submitt function pressed") asn_frame = MyFrame() asn_frame.mainloop() RE: Trying to add data into a shelf from a submit button - Yoriz - Dec-18-2022 To get the input from shelve into the GUI when it is created you could change #open text file student_scores = shelve.open('student_name', 'c') #Display menue here here self.student_name = StringVar()to #open text file with shelve.open('student_name', 'c') as db: student_name = db["Student Name"] #Display menue here here self.student_name = StringVar() self.student_name.set(student_name)This will open the file and automatically close it after the value has been read into student_name Then the StringVar is set to the value from student_name The following close is not needed as it automatically closed above #close test file student_scores.close()In the button handler event you didn't do anything with shelve, that is why nothing happened #function to set student names def set_names(self): student_name = [self.student_name.get()] print("submitt function pressed")You can change it to #function to set student names def set_names(self): student_name = self.student_name.get() print("submitt function pressed") with shelve.open('student_name', 'c') as db: db["Student Name"] = student_name Also, the entry needs to be told to use the stringvar Change #text box self.input = Entry(self) self.input.grid(row = 7, column = 1)to #text box self.input = Entry(self,textvariable=self.student_name) self.input.grid(row = 7, column = 1) RE: Trying to add data into a shelf from a submit button - woooee - Dec-18-2022 If you want to read from the file as well as write new records, use the writeback=True option. RE: Trying to add data into a shelf from a submit button - TWB - Dec-27-2022 Yoriz This was very helpful and resolved my issue Thanks TWB RE: Trying to add data into a shelf from a submit button - TWB - Dec-28-2022 Thanks again for your help I am having a issue with the following code modification #open text file with shelve.open('student_name', 'c') as db: student_name = db["Student Name"] #Display menue here here self.student_name = StringVar() self.student_name.set(student_name)i am receiving a error as following Quote:lib\shelve.py", line 111, in __getitem__value = self.cache[key] I assume its to do with the open text file code and most examples ive been able to find set the db["..."] a bit different But so far i havent been able to resolve it RE: Trying to add data into a shelf from a submit button - Yoriz - Dec-29-2022 That will be because at that point there has not yet been created a key "Student Name" As shelve seems to act like a dictionary try using its get method and set the default to an empty string for when the key does not exist.Change with shelve.open('student_name', 'c') as db: student_name = db["Student Name"]to with shelve.open('student_name', 'c') as db: student_name = db.get("Student Name", "") RE: Trying to add data into a shelf from a submit button - TWB - Jan-06-2023 I again appreciate the help I added a for loop to print out the values in the shelf and it seems to over write the files previous data. If I enter numerous input it will only I assume store the last entry. The for loop seen in the below code: #print shelf print("the values of the shef are ", list(db.values())) print("the keys of the shef are ", list(db.keys())) for key, value in db.items(): print(key, ' : ', value)Ive tried a few different options but it still only returns the last imputed value. I opened the .dir file and it also shows one line even though numerous were entered.. thanks again RE: Trying to add data into a shelf from a submit button - Yoriz - Jan-06-2023 Having never actually used shelve myself, I'm not sure what you intend shelve to do.As it acts like a dictionary I would expect that when you reassign a value to a key it will just overwrite the current value. I would assume that you could also make the value a list to which you could append items. Maybe an actual database would be more appropriate for what you want to achieve. RE: Trying to add data into a shelf from a submit button - TWB - Jan-06-2023 My goal was to take a program that I have already written in a command line format and convert it to a GUI. It takes input for student names and grades and stores them in a shelf. I can use that For loop and it will print out all the names that were entered and their scores. I also have it so I can search on students names. I can also open up the .dir file and see the names as they are the Keys. It all works in the command line just not working on the GUI conversion to that point of the program. As Im new to Python it was just a exercise of interest. Thanks for your direction though, it was helpful |