Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Registering user's drop down menu choice and finding corresponding line in files
#1
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!
Quote
#2
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()
Quote
#3
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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Choice of GUI implementation jefsummers 2 268 May-19-2019, 11:15 PM
Last Post: jefsummers

Forum Jump:


Users browsing this thread: 1 Guest(s)