Python Forum
Print Values from a Sequence of Entries / ComboBoxes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print Values from a Sequence of Entries / ComboBoxes
#1
Hi - I want to build a program to add a basketball lineup.

Ideally I want the output to be (as an example):
Center, John
Point Guard, Jack
Shooting Guard, James

This would depend on how many values you add and what you type for the name. I am struggling to pull these values that are entered. I am not getting an error - just not getting the results I am looking for. For example, instead of "Point Guard", it says "<tkinter.ttk.Combobox object .!combobox2>". I am also not returning a value for the Entry fields. Any help would be greatly appreciated!!



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

root = Tk() 
menu = Menu(root)
root.config(menu=menu)

combovalues = ['Center' , 'Point Guard' , 'Shooting Guard' , 'Power Forward' , 'Small Forward' ]
startinglineup = []
entry_values = []


root.counter = 2
my_lineup = []
string_var = tk.StringVar()
entry_values.append(string_var)

def addlineup():

    Label(root, text='Lineup Name').grid(row=0) 
    e1 = Entry(root) 
    e1.grid(row=0, column=1)
    combobox = ttk.Combobox(root, values=combovalues)
    combobox.grid(column=0, row=1)
    e2 = Entry(root)
    e2.grid(row=1, column=1)
    addbutton = tk.Button(root, text='Add', width=25, command=add) 
    addbutton.grid(column=0, row=14)
    confirmbutton = tk.Button(root, text='Confirm', width=25, command=save)
    confirmbutton.grid(column=0, row=15)


def save():
    number = root.counter
    print(my_lineup)


def add():
    root.counter += 1
    combobox = ttk.Combobox(root, values=combovalues)
    combobox.grid(column=0, row=root.counter)
    entry = Entry(root) 
    entry.grid(row=root.counter, column=1)
    for stringvar in entry_values:
        text = string_var.get()
        if text:
            my_lineup.append(text)
    my_lineup.append([text, combobox])
    


# --- main menu ---
filemenu = Menu(menu) 
menu.add_cascade(label='File', menu=filemenu) 


# --- lineupss ----
lineupmenu = Menu(menu) 
menu.add_cascade(label='Lineups', menu=lineupmenu) 
lineupmenu.add_command(label='Add Lineup', command=addlineup)
lineupmenu.add_command(label='View Lineups')


mainloop() 
Output:
[['', <tkinter.ttk.Combobox object .!combobox2>], ['', <tkinter.ttk.Combobox object .!combobox3>]]
Reply
#2
For starters, to get the value of a Combobox you either use Combobox.get(), or you assign the Combobox a String_Var and use String_Var.get().

Even if you used the correct command to get the Combobox value you don't have any way to get hold of the combo boxes. You could do what you tried to do with the Entry widgets (more about that in a moment), creating a String_Var for each one, putting the String_Var's in a list and using them to get the value, or you could just keep the Combobox objects in a list. What you cannot do is use a local variable and throw it away leaving no way to access the Combobox widget or value.

The Entry widgets don't work either, and that is because you are not binding the entry_values to the Entry widgets. You need to do something like this:
string_var = tk.StringVar()
entry_values.append(string_var)
e2 = Entry(root, textvariable=string_var)  #<- Need this for it to work
You are missing a lot of bookkeeping that needs to be done to convert this into a viable program. If you want to grow the list of players each time using the Add button you'll have to keep track of the player name Entry widgets and position combo boxes so they can be hidden (pack_forget) when you want to start a new lineup. You may find it easier to create all the widgets you will ever need, and provide a way to identify which widgets contain players. Perhaps checking for a non-empty string in the player name Entry.
Reply
#3
Thank you very much for the response. I am new to learning this and it can be difficult to find exactly what I am looking for online. Your reply is incredibly helpful.

I am trying to make a simpler program now to understand some of the basics before I move on to anything more advanced. For example, below I have rebuilt a simple program. I am trying to print a list of all of the values in each entry. The amount of entrys is variable based on how many the user decides to add. I am having trouble figuring out how to reference the added entrys. They would all have the name 'entry 2'. Is there a loop that needs to be added to append a blank variable for each entry? I have tried a few different ways and nothing has worked for me.

If I can wrap my head around how this works, I think it would help me tremendously moving forward.

This is the error I am getting:

Error:
result2 = entry2.get() NameError: name 'entry2' is not defined
import tkinter as tk
from tkinter import *
from tkinter import ttk

root = Tk() 
menu = Menu(root)
root.config(menu=menu)

string_var = tk.StringVar()

entry = tk.Entry(root, textvariable=string_var)
entry.pack() 


def addentry():
    entry2 = tk.Entry(root)
    entry2.pack()


def printa():
    
    result = entry.get()
    result2 = entry2.get()

    print(result)
    print(result2)


confirmbutton = tk.Button(root, text='Confirm', width=25, command=printa)
confirmbutton.pack()

confirmbutton2 = tk.Button(root, text='Add', width=25, command=addentry)
confirmbutton2.pack()


mainloop() 
Ideally if I had 5 entries with a, b, c, d, e typed into each respective entry, the print(results) line would return 'a, b, c, d, e'.
Reply
#4
When you have multiple things you need to use a container of some sort. Python uses dictionaries to hold a collection of things that can be referenced by a key. Python also has lists and tuples which use an index instead of a key. A tuple is immutable (cannot be changed), so that is not a good choice for containing items where the number of items can grow. That leaves a list.
import tkinter as tk
 
root = tk.Tk() 
menu = tk.Menu(root)
root.config(menu=menu)
entry_list = []    # List to hold all my Entry's
 
def addentry():
    entry = tk.Entry(root)
    entry.pack()
    entry_list.append(entry)
 
def printa():
    for entry in entry_list:
        print(entry.get())
     
addentry()

confirmbutton = tk.Button(root, text='Confirm', width=25, command=printa)
confirmbutton.pack()
 
confirmbutton2 = tk.Button(root, text='Add', width=25, command=addentry)
confirmbutton2.pack()
 
tk.mainloop()
The program is a little wonky in that the first entry appears above the Add and Confirm button and additional entries appear below. In a GUI it would be more common to have an entry field, an add button, and another widget that displays all the values that were entered.

Going back to your basketball lineup program, I can see it having an entry for the player name and a list box for the position, and an additional widget that displays the current lineup. The program would have an add button for adding a player using info from the player name entry and position listbox as well as a bench button for removing a player selected in the lineup widget.
Reply
#5
(Mar-28-2020, 08:14 PM)deanhystad Wrote: When you have multiple things you need to use a container of some sort. Python uses dictionaries to hold a collection of things that can be referenced by a key. Python also has lists and tuples which use an index instead of a key. A tuple is immutable (cannot be changed), so that is not a good choice for containing items where the number of items can grow. That leaves a list.
import tkinter as tk
 
root = tk.Tk() 
menu = tk.Menu(root)
root.config(menu=menu)
entry_list = []    # List to hold all my Entry's
 
def addentry():
    entry = tk.Entry(root)
    entry.pack()
    entry_list.append(entry)
 
def printa():
    for entry in entry_list:
        print(entry.get())
     
addentry()

confirmbutton = tk.Button(root, text='Confirm', width=25, command=printa)
confirmbutton.pack()
 
confirmbutton2 = tk.Button(root, text='Add', width=25, command=addentry)
confirmbutton2.pack()
 
tk.mainloop()
The program is a little wonky in that the first entry appears above the Add and Confirm button and additional entries appear below. In a GUI it would be more common to have an entry field, an add button, and another widget that displays all the values that were entered.

Going back to your basketball lineup program, I can see it having an entry for the player name and a list box for the position, and an additional widget that displays the current lineup. The program would have an add button for adding a player using info from the player name entry and position listbox as well as a bench button for removing a player selected in the lineup widget.


Thank you very much for the response. This was a tremendous help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't get tkinter database aware cascading comboboxes to update properly dford 6 3,536 Jan-11-2022, 08:37 PM
Last Post: deanhystad
  Linking Comboboxes MrP 24 6,908 Feb-03-2021, 10:59 PM
Last Post: MrP
  [Tkinter] Tkinter delete values in Entries, when I'm changing the Frame robertoCarlos 11 5,651 Jul-29-2020, 07:13 PM
Last Post: deanhystad
  [PyQt] making dependant comboBoxes Hitsugaya 3 4,961 May-23-2019, 06:05 PM
Last Post: Alfalfa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020