无法在 tkinter 的 Radiobutton 中正确获取变量

时间:2021-07-03 15:44:29

标签: python tkinter

我正在制作一个问答游戏,目前,我有 4 个问题,我将它们以 2 个循环的形式显示在屏幕上。 现在我想获取单选按钮中的变量,但每次尝试这样做时,我都会一次又一次地从第一个问题中获取变量。

所以我基本上想获得用户点击的变量并将其与真实答案进行比较,并判断他是否错了,我不能这样做,因为它给了我第一个问题的价值,不管怎样。

from tkinter import *


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

def instructions_Window():
    instructions_Window = Toplevel()
    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():
    def clear():
        WaitState.set(1)
        for widgets in trivia_Window.winfo_children():
            widgets.destroy()

    trivia_Window = Toplevel()
    trivia_Window.title("Q&A")
    trivia_Window.config(bg="#858585")

    x = StringVar()
    WaitState = StringVar()
    i = 0

    for key in questions:
        j = 0

        question_number = 1
        question_label = Label(trivia_Window, text=key, bg="#858585",font=("Squada One",40,"bold"))
        question_label.pack()

        continue_but = Button(trivia_Window, text="Continue", font=("Knewave",25,"bold"), bg="#942222" , command=clear)
        continue_but.pack()

        for index in options[question_number-1]:
            options_radio = Radiobutton(trivia_Window, text=options[i][j], variable=x, value=index,bg="#858585", font=("Squada One",40,"bold"))
            options_radio.pack(anchor=W)
            question_number = question_number + 1
            j += 1
            print(x.get())

        i += 1
        continue_but.wait_variable(WaitState)

questions = {
     "How old is the universe?": "B",
     "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.iconbitmap("pp.ico")
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()

2 个答案:

答案 0 :(得分:1)

我无法找到带有单选按钮的解决方案(尽管可能有一个可能的解决方案)。但是,我已经成功使用普通按钮。

基本上,我只是将您的单选按钮重写为普通按钮,然后定义了更改答案值的命令(A、B、C、D)。之后,我只是抛出了一个 if 语句来将答案与 for 循环中键的值进行比较。

代码如下:

from tkinter import *

answer = ""


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


def instructions_Window():
    instructions_Window = Toplevel()
    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 A():
    global answer
    answer = "A"


def B():
    global answer
    answer = "B"


def C():
    global answer
    answer = "C"


def D():
    global answer
    answer = "D"


def trivia_Window():
    def clear():
        WaitState.set(1)
        for widgets in trivia_Window.winfo_children():
            widgets.destroy()

    trivia_Window = Toplevel()
    trivia_Window.title("Q&A")
    trivia_Window.config(bg="#858585")

    x = StringVar()
    WaitState = StringVar()
    i = 0

    for key in questions:
        question_label = Label(trivia_Window, text=key, bg="#858585", font=("Squada One", 40, "bold"))
        question_label.pack()

        continue_but = Button(trivia_Window, text="Continue", font=("Knewave", 25, "bold"), bg="#942222", command=clear)
        continue_but.pack()

        options_button = Button(trivia_Window, text=options[i][0], command=A)
        options_button2 = Button(trivia_Window, text=options[i][1], command=B)
        options_button3 = Button(trivia_Window, text=options[i][2], command=C)
        options_button4 = Button(trivia_Window, text=options[i][3], command=D)
        options_button.pack(anchor=W)
        options_button2.pack(anchor=W)
        options_button3.pack(anchor=W)
        options_button4.pack(anchor=W)
        i += 1
        continue_but.wait_variable(WaitState)
        if answer == questions[key]:
            print("correct, ect")  # & other actions you want to take
        else:
            print("incorrect, ect")  # & other actions you want to take


questions = {
    "How old is the universe?": "B",
    "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()

可能不是最好的解决方案,因为我是初学者,它也不是很花哨,但嘿,它有效。

答案 1 :(得分:0)

这是如何做到的。我更改了您的 GUI 的布局,因为我系统上的屏幕与您的屏幕尺寸不同,我需要能够在为您开发答案时运行它。我还进行了许多其他修改以简化事情并使其更具可读性,并修复我在此过程中遇到的几个错误 - 但它仍然具有与您相同的基本架构并使用 {{1 }}s。

我将您的 Radiobutton 函数重命名为 clear(),因为这是调用的 _continue() 的名称。就是你可以检查用户的最新答案与正确答案——其中有一条评论显示它会去哪里,但代码目前没有任何内容,因为我不知道你想如何处理正确和不正确的答案——所以就像他们说的,这是留给读者的练习。 Button

;¬)