I made two entries for the user, then I took them and turned them into a string using the str()
, then put them in a list and put that list in choice from the random module and turned that into a string using str()
and put it in a function that turns the text into a label and that puts it onto the screen. then the button runs that function.
I was expecting 2 entry boxes and on button underneath it like:
ENTRY BOX
ENTRY BOX
BUTTON
And after that it takes the two things and randomly chooses between the two of them. Then the button would stick it onto the screen. Here's my code:
opt_1 = Entry(win, width=40)
opt_1.focus_set()
opt_1.pack()
opt_2 = Entry(win, width=40)
opt_2.focus_set()
opt_2.pack()
opt_1str = str(opt_1.get())
opt_2str = str(opt_2.get())
choice_list = [opt_1str, opt_2str]
rando_choice = choice(choice_list)
ai_rando_choice = str(rando_choice)
def output_answer():
output = Label(win, text=ai_rando_choice, font="Courier 9 bold")
output.pack()
tk.Button(win, text="GO!", width=20, command=output_answer).pack(pady=20)
It's in a switch case and here's the rest of the code if it helps.
from tkinter import *
import tkinter as tk
from random import *
win = Tk()
win.geometry("750x750")
# function time
def display_answer():
global output
entri = choice.get()
match entri:
case "0":
output = Label(win, text="Then why are you here? dang this generation is getting dumber and dumber.",
font="Courier 9 bold")
output.pack()
case "1":
opt_1 = Entry(win, width=40)
opt_1.focus_set()
opt_1.pack()
def output_answer():
output = Label(win, text="Uh-hh " + opt_1.get() + "?? I dunno, what do you want? You only gave me one, "
"dang this generation is really dumb", font="Courier "
"9 bold")
output.pack()
tk.Button(win, text="GO!", width=20, command=output_answer).pack(pady=20)
case "2":
opt_1 = Entry(win, width=40)
opt_1.focus_set()
opt_1.pack()
opt_2 = Entry(win, width=40)
opt_2.focus_set()
opt_2.pack()
opt_1str = str(opt_1.get())
opt_2str = str(opt_2.get())
choice_list = [opt_1str, opt_2str]
rando_choice = choice(choice_list)
ai_rando_choice = str(rando_choice)
def output_answer():
output = Label(win, text=ai_rando_choice, font="Courier 9 bold")
output.pack()
tk.Button(win, text="GO!", width=20, command=output_answer).pack(pady=20)
# Initialize a Label to display the User Input
instructions = Label(win, text="type in how many decisions you have (max 10) \n↓\n", font="Courier 14 bold")
instructions.pack()
# Create an Entry widget to accept User Input
choice = Entry(win, width=40)
choice.focus_set()
choice.pack()
# Create a Button to validate Entry Widget
tk.Button(win, text="Okay", width=20, command=display_answer).pack(pady=20)
win.mainloop()