Викторина в Ткинтере - PullRequest
0 голосов
/ 06 июля 2018

Привет, я не уверен, как изменить параметры в моей викторине с вопроса 1 на 2 без необходимости открывать новое окно. Мне нужно, чтобы после того, как пользователь нажмет кнопку «Отправить», откроется новое окно, в котором будет сказано «правильно», если ответ правильный (ответ 1) с кнопкой «ОК». Затем, после того как пользователь нажмет кнопку «ОК», окно предыдущего вопроса изменится на новый. Спасибо

import tkinter as tk
def open_easyquiz():
    window = tk.Tk()
    window.title("6 Questions")
    window.geometry("500x150")
    score = 0
    labelcorrect = tk.Label(window, text="Correct! +1")
    answers = ["Livro", "1"]
    def inst():
        t = tk.Label(window, text="All you need to do is just answer each question with either a '1, 2, 3' or the actual word. You will see a word in English at the top and you have to answer with the most similar word in Portuguese.")
        t.pack()

    def start():

        def submit():
            if ans == answers[0] or answers[1]:


                rootc = tk.Tk()
                rootc.title = "Correct!"
                ok = tk.Button(rootc, text="OK", command = question2 and rootc.destroy)
                print("correct")
                labelcorrect.pack(rootc)


            #or do whatever you like with this

        root = tk.Tk()
        root.title("Question")
        q = tk.Label(root, text="Book")
        q.pack()
        a = tk.Label(root, text="1.) Livro")
        a.pack()
        b = tk.Label(root, text="2.) Laranja")
        b.pack()
        c = tk.Label(root, text="3.) Borboleta")
        c.pack()
        ans = tk.Entry(root, width=40)
        ans.pack()
        #here is the button I want to verify the answer
        sub = tk.Button(root, text="Submit", command=submit)
        sub.pack()


    greet = tk.Label(window, text="Welcome to the EASY Question Quiz.")
    greet.pack()
    startButton = tk.Button(window, command=start, text="Start")
    startButton.pack()
    instr = tk.Button(window, text="Instructions", command=inst)
    instr.pack()
    end = tk.Button(window, text="Exit", command=window.destroy)
    end.pack()

    window.mainloop()
open_easyquiz()

Ответы [ 2 ]

0 голосов
/ 06 июля 2018

Вы должны попробовать использовать разные страницы для разных вопросов:

import Tkinter as tk

class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class Page1(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is question 1")
       label.pack(side="top", fill="both", expand=True)

class Page2(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is question 2")
       label.pack(side="top", fill="both", expand=True)

class Page3(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is question 3")
       label.pack(side="top", fill="both", expand=True)

class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)
        p3 = Page3(self)

        buttonframe = tk.Frame(self)
        container = tk.Frame(self)
        buttonframe.pack(side="top", fill="x", expand=False)
        container.pack(side="top", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        b1 = tk.Button(buttonframe, text="Page 1", command=p1.lift)
        b2 = tk.Button(buttonframe, text="Page 2", command=p2.lift)
        b3 = tk.Button(buttonframe, text="Page 3", command=p3.lift)

        b1.pack(side="left")
        b2.pack(side="left")
        b3.pack(side="left")

        p1.show()

if __name__ == "__main__":
    root = tk.Tk()
    main = MainView(root)
    main.pack(side="top", fill="both", expand=True)
    root.wm_geometry("400x400")
    root.mainloop()

Вы можете сделать это разными способами. Вы можете поместить кнопку на каждой странице, чтобы проверить ответ, указанный в записи, а затем напечатать правильный или неправильный. Таким образом, вы можете задать столько вопросов, сколько захотите, и это даст вам гибкость.

Дайте мне знать, если это работает.

0 голосов
/ 06 июля 2018

Попробуйте использовать root.destroy(), уничтожив окно вопроса и открыв новое.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...