Skip to content

Instantly share code, notes, and snippets.

@cwg83
Created November 2, 2021 22:39
Show Gist options
  • Save cwg83/8ffa8a40cf66a58be5c0b1147eacd78a to your computer and use it in GitHub Desktop.
Save cwg83/8ffa8a40cf66a58be5c0b1147eacd78a to your computer and use it in GitHub Desktop.
def input_ui(results_text):
# Create Tkinter frame instance
win = Tk()
# Set Tkinter frame geometry
win.geometry("750x250")
# Initialize Label widgets to display the total accuracy, instructions, and validation warning text
results_label = Label(win, text=results_text, font="Courier 12")
results_label.grid(row=0, column=0, columnspan=4)
instructions_label = Label(win, text=(
"To see a graph showing the accuracy for specific counts,\n"
"enter the number of outs/balls/strikes you wish to view\n"
"(or leave blank for 'all' results for that datapoint)"), font="Courier 12")
instructions_label.grid(row=2, column=0, columnspan=4, rowspan=3)
warning_label = Label(win, text="", font="Courier 12", fg='red')
warning_label.grid(row=8, column=1, columnspan=2)
# Create remaining Label widgets and Entry widgets
outs_label = Label(text="Outs:")
balls_label = Label(text="Balls:")
strikes_label = Label(text="Strikes:")
outs_entry = Entry(win, width=10)
balls_entry = Entry(win, width=10)
strikes_entry = Entry(win, width=10)
outs_entry.focus_set()
# Place widgets in the grid
outs_label.grid(row=5, column=1)
balls_label.grid(row=6, column=1)
strikes_label.grid(row=7, column=1)
outs_entry.grid(row=5, column=2)
balls_entry.grid(row=6, column=2)
strikes_entry.grid(row=7, column=2)
win.grid_columnconfigure(0, weight=1)
win.grid_rowconfigure(1, weight=1)
win.grid_rowconfigure(10, weight=1)
win.grid_columnconfigure(3, weight=1)
# Create empty list to which we will add data from the Entries
entries_output = []
# Get text from Entries and compare text to validation lists
def get_entries():
# Create a validation list for each Entry field
entries = [(outs_entry.get(), "Outs", ['0', '1', '2', '']),
(balls_entry.get(), "Balls", ['0', '1', '2', '3', '']),
(strikes_entry.get(), "Strikes", ['0', '1', '2', ''])]
message = ""
for entry in entries:
# If the text does not match the respective validation list
if entry[0] not in entry[2]:
# Add the appropriate warning text to the message
message += f"{entry[1]} can only contain {entry[2]}\n"
# If warning text was added to the message
if message:
# Change our warning label to the message text
warning_label.config(text=message)
else:
# Else make the warning label text blank and destroy the window, returning the entries
warning_label.config(text="")
win.destroy()
for entry in entries:
entries_output.append(entry[0])
# Create button that runs get_entries command
ttk.Button(win, text="Display Graph", width=20, command=get_entries).grid(row=9, column=1, columnspan=2)
# Run the event loop
win.mainloop()
# Return the Entry field data to use in a matplotlib graph
return entries_output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment