Search code examples
pythonfunctiontkintertkinter-entry

Tkinter GUI program issue. Entry.get() problem


Working on a project in which I use Tkinter in order to create a GUI that gives a list of software in a drop-down and when a particular software is chosen, it takes you to a separate window where a user's name will be entered and they would be added to a database. With the code I have so far, I am able to link a "submit" button on the second window to a function that prints a confirmation message as a test to make sure the button works. My issue now is trying to get the input from the entry field and link the input to the "Submit" button but I can't seem to find a way to do so. I was wondering if I could get some advice on how to go about this. Would classes need to be used in order to make it work? or can I stick with functions and keep the code relatively simple?

I have added the code for my program below.

import tkinter as tk
from tkinter import *
from tkinter import ttk

root = tk.Tk()                                  # Main window
root.title("Software Licences")
root.geometry("300x300")

frame = ttk.Frame(root, padding="50 0 50 50")
frame.pack(fill=tk.BOTH, expand=True)

tkvar = StringVar()

choices = ['Imagenow',                          # Dropdown menu with software options
           'FileMakerPro',
           'Acrobat',
           'Office',
           'Lotus Notes']

tkvar.set('Acrobat')                            # Shows default dropdown menu option when program is opened

popupMenu = OptionMenu(frame, tkvar, *sorted(choices))
popupLabel = ttk.Label(frame, text="Choose Software")

popupLabel.pack()
popupMenu.pack()


def software_pages():                           # In this function is the 2nd window with for each individual software
    top = Toplevel()
    top.title("Software Licences")
    top.geometry("300x300")
    myLabel = Label(top, text=tkvar.get()).pack()
    employee_entrylbl = Label(top, text="Employee name").pack()
    employee_entry = Entry(top, width=25, textvariable=tk.StringVar) # Entry field for adding user's name
    employee_entry.pack() # Entry field is displayed

    if tkvar.get() == "Acrobat":                # for each if statement, button command is link to the functions
        # defined below
        button = ttk.Button(top, text="Submit", command=add_to_acrobat).pack()
    elif tkvar.get() == "Imagenow":
        button = ttk.Button(top, text="Submit", command=add_to_imagenow).pack()
    elif tkvar.get() == "FileMakerPro":
        button = ttk.Button(top, text="Submit", command=add_to_filemakerpro).pack()
    elif tkvar.get() == "Office":
        button = ttk.Button(top, text="Submit", command=add_to_office).pack()
    else:
        button = ttk.Button(top, text="Submit", command=add_to_lotusnotes).pack()

    exit_button = ttk.Button(top, text="Exit", command=top.destroy).pack() # Exit button for second window


add_emp_button = ttk.Button(frame, text="Next", command=software_pages) # "Next" button in the main window takes the
# user to the second window
add_emp_button.pack()

# Functions below are linked to the button commands of each software in the second window function defined earlier.
# They print out specified messages that confirm the user had been added


def add_to_acrobat():
    return print("User added to Acrobat")


def add_to_lotusnotes():
    print("User added to IBM")


def add_to_imagenow():
    print("User added to imagenow")


def add_to_office():
    print("User added to 365")


def add_to_filemakerpro():
    print("User added to FMP")


def click_button():                         # Function for Exit button for main window
    root.destroy()


exit_button = ttk.Button(frame, text="Exit", command=click_button)  # Exit button for main window
exit_button.pack()

root.mainloop()

Solution

  • You can pass parameters to the command of tkinter.command using partial from the functools module.

    in your case:

    button = ttk.Button(top, text="Submit", command=partial(add_to_acrobat, employee_entry)).pack()
    

    in the above line, I send the employee_entry(Which holds your desired text) to the add_to_acrobat function

    and the add_acrobat function should look like this:

    def add_to_acrobat(e):
        print(e.get())
        return print("User added to Acrobat")
    

    Hope it helps