Mar-28-2020, 08:14 PM
(This post was last modified: Mar-28-2020, 08:14 PM by deanhystad.)
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.
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.
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.