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
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:
        comment_input = comment.get()
        with open("comments.txt", 'a') as file:
        birthday_input = birthday.get()
        with open("birthdays.txt", 'a') as file:
        name.delete(0, tk.END)
        comment.delete(0, tk.END)
        birthday.delete(0, tk.END)
    add_win = tk.Tk()
    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), rely=0.3, anchor=tk.W), rely=0.35, anchor=tk.W), 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()

    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:
    var=tk.StringVar ()
    var.set ('Click to Select Member')
    memberlist = tk.OptionMenu (win, var, *list)
    memberlist.pack (fill=tk.X,padx=10)
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!
Bind a function to the command option of tk.OptionMenu

import tkinter as tk

def selected(value):

win = tk.Tk()
# 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)

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  PyQt5: How do you set the user input of a line edit to a specific variable? YoshikageKira 17 1,411 Dec-26-2019, 03:18 PM
Last Post: Denni
  Choice of GUI implementation jefsummers 2 523 May-19-2019, 11:15 PM
Last Post: jefsummers

Forum Jump:

Users browsing this thread: 1 Guest(s)