0

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()
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • 2
    Basic issue is that you are generation the value of `ai_rando_choice` outside of the function. It generates the value once at initialisation where its "options" are `["",""]`. Move the retrieving of the strings and the generating of the random choice into the function so that it is re-evaluated when the function is called. – Shorn Apr 13 '23 at 01:05
  • Umm I tried putting the ai_rando_choice inside of the function but that still didn't change the fact that I'm still getting this error File "C:\Users\arjun\PycharmProjects\python games\option chooser.py", line 42, in display_answer rando_choice = choice(choice_list) ^^^^^^^^^^^^^^^^^^^ TypeError: 'Entry' object is not callable I really don't understand – Arjun Nandakumar Apr 13 '23 at 03:56
  • 1
    You have conflicting names, since you did `from random import *` and also `choice = Entry(...)`. Fix the conflict, and remember that this is why [wildcard imports are not recommended](https://peps.python.org/pep-0008/#imports) in [many if not most languages](https://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad). – Shorn Apr 13 '23 at 05:30
  • Thanks that worked in getting the error off an I figured ou that I have tograb the entries in the function and also randomly choose in the function Thanks again! – Arjun Nandakumar Apr 13 '23 at 13:08

0 Answers0