1

I am doing a project of a quiz and I need to add 4 Radiobuttons. Now I want them to have some value that I could know what is the answer that the user chose. So the problem is that I won't let me define IntVar() not on the main window in python tkinter (it is in the trivia_Window window, I have tried the exact same code but only in the main window and it worked as I wanted and I tried to figure out what was the problem and found no answer on the web.

So if you have any suggestions please tell me.

from tkinter import *

def sumbit():
    name = entry_name.get()
    print(name)

def instructions_Window():
    instructions_Window = Tk()
    instructions_Window.title("instructions")

    instructions = Label(
            instructions_Window,
            text = "WELCOME TO THE TRIVIA GAME!\nYour will answer 10 question, the questions will be about general knowledge.\nMake sure you are doing your best maybe you will on the leaderboard soon!\n GOOD LUCK!!!",
            font = ("Staatliches",30))
    instructions.pack()

    back_but = Button(instructions_Window, text="Back", command=instructions_Window.destroy)
    back_but.pack()

def trivia_Window():
    x = IntVar()

    trivia_Window = Tk()
    trivia_Window.title("Q&A")

    question_label = Label(trivia_Window, text="How old is the universe?", font=("Arial",40))
    question_label.pack()

    option1_radio = Radiobutton(trivia_Window, text=options[0][0], font=("Arial",25),variable=x, value=1)
    option1_radio.pack(anchor=W)
    option2_radio = Radiobutton(trivia_Window, text=options[0][1], font=("Arial",25),variable=x, value=2)
    option2_radio.pack(anchor=W)
    option3_radio = Radiobutton(trivia_Window, text=options[0][2], font=("Arial",25),variable=x, value=3)
    option3_radio.pack(anchor=W)
    option4_radio = Radiobutton(trivia_Window, text=options[0][3], font=("Arial",25), variable=x, value=4)
    option4_radio.pack(anchor=W)

questions = {
     "How old is the universe?": "C",
     "Who was the first person in space?": "C",
     "In which year the first covid-19 case was discovered?": "C",
     "What is the most populated country?": "A"
}

options = [[["A. 5.3 billion years old"], ["B. 13.8 billion years old"], ["C. 13.8 milion years old"], ["D. 241.1 billion years old"]],
          [["A. Alan Shepard"], ["B. Neil Armstrong"], ["C. Yuri Alekseyevich Gagarin"], ["D. Ilan Ramon"]],
          [["A. 2018"], ["B. 2001"], ["C. 2019"], ["D.2020"]],
          [["A. China"],["B. Russia"], ["C. India"], ["D. United States"]]]

window = Tk()
window.title("Home")
window.geometry("1920x1080")
window.config(bg="#93b4ba")

label_welcome = Label(window, text="Welcome Back To Our Trivia Game!", font=("Akaya Kanadaka",80,"bold"), bg = "#93b4ba")
label_welcome.pack()

label_enter_name = Label(window, text="Enter you name:", font=("Lato",50,"bold"), bg = "#93b4ba", fg="#3038d1")
label_enter_name.pack(side=LEFT)

entry_name = Entry(window,font=("Arial",40))
entry_name.pack(side=LEFT)

sumbit_but = Button(window, text="Sumbit", font=("Arial",10,"bold"), width=15, height=4,command=sumbit, bg="#0f0f0f", fg="white")
sumbit_but.pack(side=LEFT)

quit_but = Button(window, text="Quit", font=("Arial",10,"bold"), width=20, height=10,command=quit,bg="#b5aa72")
quit_but.place(x=0,y=845)

start_but = Button(window, text="Start", font=("Arial",10,"bold"), width=20, height=10,command=trivia_Window ,bg="#a1ad90")
start_but.place(x=1750,y=845)

instructions_but = Button(window, text="Instructions", font=("Arial",10,"bold"), width=20, height=10,command=instructions_Window,bg="#626363")
instructions_but.pack(side=RIGHT)

window.mainloop()

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Pass in the master attribute when creating `IntVar()` as you would when creating a normal label/button/... Or use `Toplevel` instead of `Tk` for your windows (except your main one) – TheLizzard Jul 02 '21 at 20:53
  • 1
    Vars (and various other Tkinter objects) are tied to a particular instance of `Tk()`, and will fail to work (often with no indication of why they're failing) if you try to use them from a different instance. It's possible to explicitly create them in a certain instance, but it's simpler and more efficient to only have one instance of `Tk()`, and use `Toplevel()` instead to create additional windows that share the same instance. – jasonharper Jul 02 '21 at 20:55
  • To reinforce part of what both of the previous comments said, see the accepted answer to [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) Also, here's some [documentation](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/toplevel.html) on `Toplevel` widgets. – martineau Jul 02 '21 at 21:54

0 Answers0