Home > Net >  tkinter indexing incorrectly
tkinter indexing incorrectly

Time:10-11

This is similar to the colour game; here you have to state the number of letters and not the text word. That is: five = four, six = three, etc. In the tkinter version of this the answers don't match the questions. This is the problem area:

while True:
    word = random.choice(words)
    label1.config(text=numbers[word])

    button_pressed = StringVar()
    button = Button(
        root,
        bg="#F20505",
        fg="White",
        activebackground="yellow",
        activeforeground="black",
        width=10,
        height=5,
        relief=RAISED,
        text="Enter",
        command=lambda: button_pressed.set("button pressed"),
    )
    button.grid(row=2, column=0)

    button.wait_variable(button_pressed)
    button.destroy()

    guess = box.get()
    if guess == numbers[word]:
        label3.config(text="Correct!", bg="yellow", fg="green")
        num_right  = 1
        scoreLabel = Label(
            root,
            text=f"Your score is {num_right}",
            width="25",
            height="5",
            bg="#0C00FF",
            fg="white",
            font="Arial, 24",
        )
        scoreLabel.grid(row=1, column=1)
        box.delete(0, END)
    elif guess.strip().lower() == "exit":
        sys.exit()
    elif guess.strip().lower() == "end":
        label3.config(bg="yellow", fg="black", text=result())
    
    else:
        label3.config(text="Incorrect", bg="yellow", fg="red")

        num_wrong  = 1
        box.delete(0, END)

And here is the code that works in the terminal(linux mint: 21)

import random
import sys

numbers = {
    "four": "four",
    "five": "four",
    "six": "three",
    "seven": "five",
    "eight": "five",
    "nine": "four",
    "ten": "three",
}

words = tuple(numbers)

while True:
    word = random.choice(words)
    ans = input(f"{word.title()}?\n").strip().lower()
    if ans == numbers[word]:
        print("Correct")

    elif ans.strip().lower() == "exit":
        sys.exit()

    else:

        print("Correct answer is:", numbers[word])

What am I doing wrong?

CodePudding user response:

While doesn't work in mainloop or beside tkinter mainloop. Use tkinter after method insted like after(). It takes a function name and time in miliseconds in which the fucntion will be called overtime

CodePudding user response:

Working with a GUI framework is very different than working with a text interface. There not a lot of documentation on tkinter, but I would highly suggest you go read TkDocs' website to understand how all of this is working.

Here's a very bare-bones example of what you were trying to do:

mport random
import tkinter as tk
import tkinter.ttk as ttk

numbers = {
    "four": "four",
    "five": "four",
    "six": "three",
    "seven": "five",
    "eight": "five",
    "nine": "four",
    "ten": "three",
}

questions = tuple(numbers)

root = tk.Tk()
root.title = "Stackoverflow question 74002893"

question_value = tk.StringVar()
answer_received = tk.StringVar()
answer_provided = tk.StringVar()

def asking_another_question():
    question_value.set(f'{random.choice(questions).title()}')
    answer_received.set('')
    answer_provided.set('')

def submitting_answer():
    ans = answer_received.get().strip().lower()
    if ans:
        word = question_value.get().lower()
        if ans == numbers[word]:
            answer_provided.set('Your answer is correct')
        else:
            answer_provided.set(f'Correct answer is {numbers[word]}')

main_window = ttk.Frame(root)
# main_window.grid(column=0, row=0, sticky(ttk.N, ttk.W, ttk.E, ttk.S))
question_label = ttk.Label(main_window, text="Question: ")
question_variable_part = ttk.Label(main_window, textvariable=question_value)
answer_label = ttk.Label(main_window, text="Your answer")
answer_field = ttk.Entry(main_window, textvariable=answer_received)
correct_answer_label = ttk.Label(main_window, textvariable=answer_provided)
next_question_button = ttk.Button(main_window, text="Next Question", command=asking_another_question)
submit_button = ttk.Button(main_window, text='Submit you answer', command=submitting_answer)

main_window.grid(column=0, row=0)
question_label.grid(column=0, row=0)
question_variable_part.grid(column=1, row=0)
next_question_button.grid(column=2, row=0)
answer_label.grid(column=0, row=1)
answer_field.grid(column=1, row=1)
submit_button.grid(column=2, row=1)
correct_answer_label.grid(column=0, row=2, columnspan=3, sticky="w")

asking_another_question()

root.mainloop()

You will very probably need to adapt it to your situation but it should get you started.

  • Related