[Tkinter] Registering user's drop down menu choice and finding corresponding line in files - 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] Registering user's drop down menu choice and finding corresponding line in files (/thread-21471.html) |
Registering user's drop down menu choice and finding corresponding line in files - ShadeLily - Oct-01-2019 My program uses three files to store information: "names.txt", "birthdays.txt" and "comments.txt". Every entry is on matching lines. It gets its info through three Entries as shown below. def add_data(): def enter(): names_input = name.get() with open("names.txt", 'a') as file: file.write(names_input) file.write('\n') comment_input = comment.get() with open("comments.txt", 'a') as file: file.write(comment_input) file.write('\n') birthday_input = birthday.get() with open("birthdays.txt", 'a') as file: file.write(birthday_input) file.write('\n') name.delete(0, tk.END) comment.delete(0, tk.END) birthday.delete(0, tk.END) add_win = tk.Tk() add_win.geometry("400x600+300+300") add_win.overrideredirect(1) tk.Label(add_win, text='Name ').place(relx=0.1, rely=0.3, anchor=tk.W) tk.Label(add_win, text='Birthday ').place(relx=0.1, rely=0.35, anchor=tk.W) tk.Label(add_win, text='Comment ').place(relx=0.1, rely=0.4, anchor=tk.W) name = tk.Entry(add_win) birthday = tk.Entry(add_win) comment = tk.Entry(add_win) name.place(relx=0.3, rely=0.3, anchor=tk.W) birthday.place(relx=0.3, rely=0.35, anchor=tk.W) comment.place(relx=0.3, rely=0.4, anchor=tk.W) tk.Button(add_win, text='Quit', command=add_win.destroy).place(relx=0.3, rely=0.6, anchor=tk.W) tk.Button(add_win, text='Enter', command=enter).place(relx=0.4, rely=0.6, anchor=tk.W)Then, using the info from "names.txt", I create a drop down menu for the names on each line. def view_data(): win = tk.Tk() win.geometry("450x600+300+300") win.overrideredirect(1) title = tk.Label(win, text = "Please choose which member's info you would like to view.") title.pack(fill=tk.X, padx=10) file = open("names.txt", 'r') list = [] for line in file: list.append(line) var=tk.StringVar () var.set ('Click to Select Member') memberlist = tk.OptionMenu (win, var, *list) memberlist.pack (fill=tk.X,padx=10) file.close()How do I record the users click on chosen name, so that I can get the matching line with data from birthdays.txt and comments.txt to display it? The code is pretty long, but if anyone wishes, I will post all of it. Thanks ahead! RE: Registering user's drop down menu choice and finding corresponding line in files - Yoriz - Oct-01-2019 Bind a function to the command option of tk.OptionMenu import tkinter as tk def selected(value): print(value) win = tk.Tk() win.geometry("450x600+300+300") # win.overrideredirect(1) title = tk.Label( win, text="Please choose which member's info you would like to view.") title.pack(fill=tk.X, padx=10) names = ['one', 'two', 'three'] var = tk.StringVar() var.set('Click to Select Member') memberlist = tk.OptionMenu(win, var, *names, command=selected) memberlist.pack(fill=tk.X, padx=10) win.mainloop() RE: Registering user's drop down menu choice and finding corresponding line in files - joe_momma - Oct-03-2019 one way is to create a data base using shelve or pickle; you create a dictionary and nest the information: shelve doc in finished code I posted a hangman game and keep track of the stats using shelve and flat text file |