如何重新启动测验并切换到其他调查?

时间:2019-05-30 18:13:25

标签: python-3.x

当我单击第一个按钮(按钮名称为第一个)时,我正在使用python 3测试我的测验代码,但是它正常工作,但是当我再次尝试打开此测验时,它不起作用。而且它不会打开另一个测验,也根本没有任何回调

这在python 3中有效,当我尝试在代码中插入不同的更改时,它给了我不同的错误,并且在和应用程序结束时,它只能一次但不能多次工作。

from tkinter import *
from tkinter import ttk
import tkinter.ttk as ttk
import random
master = Tk()
master.minsize(300,100)
master.geometry("1300x800+0+0")
master.title("QUIZ")

frame1=Frame(master, width=200, height=150, background="Blue")
frame1.grid(row=0, column=0)

frame2=Frame(master, width=200, height=150, background="Red")
frame2.grid(row=1, column=0)

user_answer_tamo = []
indexes_tamo = []
ques_tamo = 1
answers_tamo = [0,0,3 ,2,3,3,0,3,2,3]

user_answer1_lado = []
indexes1_lado = []
ques1_lado = 1
answers1_lado = [3,3,3,3,2,3,0,1,2,3]

class toppotoo():



    k=PhotoImage(file="cover.png")
    ka = k.subsample(1,1)
    b = Label(frame1,image=ka,text=" Q    U   I   Z ",compound="center",fg="red",  height=200, width=1300,font=("Times",30,"bold")) #
    b.pack()




class app():


    def tamo():

        top = Toplevel(frame2)
        top.title("QUIZ FIRST")
        top.geometry("700x600")
        top.config(background="#ffffff")
        top.resizable(0,0)

        questions = [
        "What is the capital of the USA?", 
        "What is the capital of GERMANY?",
        "What is the capital of AUSTRALIA?",
        "What is the capital of CHINA?",
        "What is the capital of SPAIN?",
        "What is the capital of CANADA?",
        "What is the capital of BRAZIL?",
        "What is the capital of RUSSIA?",
        "What is the capital of INDIA?",
        "What is the capital of POLAND?",
        ]


        answers_choice = [
        ["New York","Washington DC","Berlin","Munich",],
        ["Berlin","Munich, Canberra","Sydney","New York",],
        ["Shanghai","Berlin","Beijing","Sydney",],
        ["Barcelona","Washington DC","Beijing","Madrid",],
        ["Toronto","Munich","Ottawa","Madrid",],
        ["Brasilia","Rio De Janeiro","Shanghai","Sydney",],
        ["Brasilia","Beijing","New York","St Petersburg",],
        ["Mumbai","Rio De Janeiro","New Delhi","Moscow",],
        ["Warsaw","Sydney","New Delhi","Krakow",],
        ["Canberra","Munich","Warsaw","Madrid",],
        ]









        def gen():

            global indexes_tamo
            try:
                while(len(indexes_tamo) < 5):

                   x = random.randint(0,9)
                   if x in indexes_tamo:


                       continue


                   else:
                       indexes_tamo.append(x)

            except:
                pass








        def showresult(score):
            lblQuestion.destroy()
            r_tamo_1.destroy()
            r_tamo_2.destroy()
            r_tamo_3.destroy()
            r_tamo_4.destroy()

            labelimage = Label(
                top,
                background = "#ffffff",
                border = 0,
            )
            labelimage.pack(pady=(50,30))
            labelresulttext = Label(
                top,
                font = ("Consolas",20),
                background = "#ffffff",
            )
            labelresulttext.pack()
            if score >= 20:
                img = PhotoImage(file="great_tamo.png")
                labelimage.configure(image=img)
                labelimage.image = img
                labelresulttext.configure(text="Greatest!")

            elif (score >= 10 and score < 20):
                img = PhotoImage(file="ok_tamo.png")
                labelimage.configure(image=img)
                labelimage.image = img
                labelresulttext.configure(text="You can do much more")

            else:
                img = PhotoImage(file="bad_tamo.png")
                #img2=img.subsample(20,5)
                labelimage.configure(image=img)
                labelimage.image = img
                labelresulttext.configure(text="Bad!!!")




        def calc():
            global indexes_tamo,user_answer_tamo,answers_tamo
            x = 0
            score = 0

            for i in indexes_tamo:
                if user_answer_tamo[x] == answers_tamo[i]:
                    score = score + 5
                x += 1


            showresult(score)




        def selected():
            global radiovar,user_answer_tamo
            global lblQuestion,r_tamo_1,r_tamo_2,r_tamo_3,r_tamo_4
            global ques_tamo


            x = radiovar.get()
            user_answer_tamo.append(x)
            radiovar.set(-1)
            if ques_tamo < 5:
                lblQuestion.config(text= questions[indexes_tamo[ques_tamo]])
                r_tamo_1['text'] = answers_choice[indexes_tamo[ques_tamo]][0]
                r_tamo_2['text'] = answers_choice[indexes_tamo[ques_tamo]][1]
                r_tamo_3['text'] = answers_choice[indexes_tamo[ques_tamo]][2]
                r_tamo_4['text'] = answers_choice[indexes_tamo[ques_tamo]][3]
                ques_tamo += 1
            elif ques_tamo >5:
                return

            else:



                calc()









        def startquiz():
            global lblQuestion,r_tamo_1,r_tamo_2,r_tamo_3,r_tamo_4
            lblQuestion = Label(
                top,
                text = questions[indexes_tamo[0]],
                font = ("Consolas", 16),
                width = 500,
                justify = "center",
                wraplength = 400,
                background = "#ffffff",
            )
            lblQuestion.pack(pady=(100,30))


            global radiovar
            radiovar = IntVar()
            radiovar.set(-1)


            r_tamo_1 = Radiobutton(
                top,
                text = answers_choice[indexes_tamo[0]][0],
                font = ("Times", 12),
                value = 0,
                variable = radiovar,
                command = selected,
                background = "#ffffff",
            )
            r_tamo_1.pack(pady=5)

            r_tamo_2 = Radiobutton(
                top,
                text = answers_choice[indexes_tamo[0]][1],
                font = ("Times", 12),
                value = 1,
                variable = radiovar,
                command = selected,
                background = "#ffffff",
            )
            r_tamo_2.pack(pady=5)

            r_tamo_3 = Radiobutton(
                top,
                text = answers_choice[indexes_tamo[0]][2],
                font = ("Times", 12),
                value = 2,
                variable = radiovar,
                command = selected,
                background = "#ffffff",
            )
            r_tamo_3.pack(pady=5)

            r_tamo_4 = Radiobutton(
                top,
                text = answers_choice[indexes_tamo[0]][3],
                font = ("Times", 12),
                value = 3,
                variable = radiovar,
                command = selected,
                background = "#ffffff",
            )
            r_tamo_4.pack(pady=5)



        def startIspressed():
            labelimage.destroy()
            labeltext.destroy()
            lblInstruction.destroy()
            lblRules.destroy()
            btnStart.destroy()
            gen()
            startquiz()










        img1 = PhotoImage(file="student.png")
        #img2=img1.subsample(0,1)
        labeltext = Label(
            top,
            text = "QUIZ FIRST",
            font = ("Comic sans MS",24,"bold"),
            background = "#ffffff",
        )

        labeltext.pack(pady=(0,50))

        labelimage = Label(
            top,
            image = img1,
            background = "#ffffff",
        )
        labelimage.pack(pady=(40,0))



        img2 = PhotoImage(file="start.png")

        btnStart = Button(
            top,
            image = img2,
            text="P L A Y",
            compound="left",
            relief = FLAT,
            border = 0,
            command = startIspressed,
        )
        btnStart.pack()

        lblInstruction = Label(
        top,
        text = "წაიკითხე წესები და დაიწყე თამაში",
        background = "#ffffff",
        font = ("Consolas",14),
        justify = "center",
        )
        lblInstruction.pack(pady=(10,100))

        lblRules = Label(
        top,
        text = "ეს ქვიზი შედგება 10 კითხვისგან.\nშენ გაქვს მხოლოდ 20 წამი კითხვებზე პასუხისთვის.\n კარგად დაფიქრდი სანამ უპასუხებ",
        width = 100,
        font = ("Times",14),
        background = "#000000",
        foreground = "#FACA2F",
        )
        lblRules.pack()
        top.mainloop()

    edo=PhotoImage(file="tamo.png")
    edozoma = edo.subsample(1,1)
    b = Button(frame2,image=edozoma, command=tamo, height=150, width=300,text="QUIZ  \n FIRST",compound="left",bg="gray")
    b.pack(side="left")

class app1():


    def lado():

        top1 = Toplevel(frame2)
        top1.title("Q U I Z     S E C O N D")
        top1.geometry("700x600")
        top1.config(background="#ffffff")
        top1.resizable(0,0)


        questions1 = [
        "What is the capital of BELARUS?",
        "What is the capital of PAKISTAN?",
        "What is the capital of ETHIOPIA?",
        "What is the capital of NEW ZEALAND?",
        "What is the capital of VIETNAM?",
        "What is the capital of CHILE?",
        "What is the capital of JAMAICA?",
        "What is the capital of SWITZERLAND?",
        "What is the capital of FINLAND?",
        "What is the capital of GEORGIA?",]


        answers_choice1 = [
        ["Ottawa","Kiev","Moscow","Minsk",],
        ["Lahore","Kiev","Khartoum","Islamabad",],
        ["Islamabad","Lahore","Sydney","Addis Ababa",],
        ["Auckland","Minsk","Lahore","Wellington",],
        ["Jakarta","Lagos","Hanoi","Vientiane",],
        ["Khartoum","Ottawa","Lima","Santiago",],
        ["Kingston","Georgetown","Islamabad","Wellington",],
        ["Zürich","Bern","Auckland","Sydney",],
        ["Copenhagen","Auckland","Helsinki","Istanbul",],
        ["Paris","London","Washington DC","Tbilisi",],
        ]









        def gen1():

            global indexes_lado
            try:
                while(len(indexes_lado) < 5):

                   x = random.randint(0,9)
                   if x in indexes_lado:


                       continue


                   else:
                       indexes_lado.append(x)
            except:
                pass







        def showresult1(score1):
            lblQuestion1.destroy()
            r_lado_1.destroy()
            r_lado_2.destroy()
            r_lado_3.destroy()
            r_lado_4.destroy()

            labelimage1 = Label(
                top,
                background = "#ffffff",
                border = 0,
            )
            labelimage1.pack(pady=(50,30))
            labelresulttext1 = Label(
                top,
                font = ("Consolas",20),
                background = "#ffffff",
            )
            labelresulttext1.pack()
            if score1 >= 20:
                img_great_lado = PhotoImage(file="great_lado.png")
                labelimage1.configure(image=img_great_lado)
                labelimage1.image = img_great_lado
                labelresulttext1.configure(text="Greatest!")
            elif (score1 >= 10 and score1 < 20):
                img_ok_lado = PhotoImage(file="ok_lado.png")
                labelimage1.configure(image=img_ok_lado)
                labelimage1.image = img_ok_lado
                labelresulttext1.configure(text="You can do much more")
            else:
                img_bad_lado = PhotoImage(file="bad_lado.png")
                #img2=img.subsample(20,5)
                labelimage1.configure(image=img_bad_lado)
                labelimage1.image = img_bad_lado
                labelresulttext1.configure(text="Bad!!!")



        def calc1():
            global indexes_lado,user_answer_lado,answers_lado
            x1 = 0
            score1 = 0

            for i in indexes_lado:
                if user_answer_lado[x1] == answers_lado[i]:
                    score1 = score1 + 5
                x1 += 1

            showresult1(score1)




        def selected1():
            global radiovar1,user_answer_lado
            global lblQuestion1,r_lado_1,r_lado_2,r_lado_3,r_lado_4
            global ques_lado


            x1 = radiovar1.get()
            user_answer_lado.append(x1)
            radiovar1.set(-1)
            if ques_lado < 5:
                lblQuestion1.config(text= questions1[indexes_lado[ques_lado]])
                r_lado_1['text'] = answers_choice1[indexes_lado[ques_lado]][0]
                r_lado_2['text'] = answers_choice1[indexes_lado[ques_lado]][1]
                r_lado_3['text'] = answers_choice1[indexes_lado[ques_lado]][2]
                r_lado_4['text'] = answers_choice1[indexes_lado[ques_lado]][3]
                ques_lado += 1
            elif ques_lado >5:
                return



            else:





                calc1()










        def startquiz1():
            global lblQuestion1,r_lado_1,r_lado_2,r_lado_3,r_lado_4
            lblQuestion1 = Label(
                top1,
                text = questions1[indexes_lado[0]],
                font = ("Consolas", 16),
                width = 500,
                justify = "center",
                wraplength = 400,
                background = "#ffffff",
            )
            lblQuestion1.pack(pady=(100,30))


            global radiovar1
            radiovar1 = IntVar()
            radiovar1.set(-1)


            r_lado_1 = Radiobutton(
                top1,
                text = answers_choice1[indexes_lado[0]][0],
                font = ("Times", 12),
                value = 0,
                variable = radiovar1,
                command = selected1,
                background = "#ffffff",
            )
            r_lado_1.pack(pady=5)

            r_lado_2 = Radiobutton(
                top1,
                text = answers_choice1[indexes_lado[0]][1],
                font = ("Times", 12),
                value = 1,
                variable = radiovar1,
                command = selected1,
                background = "#ffffff",
            )
            r_lado_2.pack(pady=5)

            r3 = Radiobutton(
                top1,
                text = answers_choice1[indexes_lado[0]][2],
                font = ("Times", 12),
                value = 2,
                variable = radiovar1,
                command = selected1,
                background = "#ffffff",
            )
            r_lado_3.pack(pady=5)

            r_lado_4 = Radiobutton(
                top1,
                text = answers_choice1[indexes_lado[0]][3],
                font = ("Times", 12),
                value = 3,
                variable = radiovar1,
                command = selected1,
                background = "#ffffff",
            )
            r_lado_4.pack(pady=5)



        def startIspressed1():
            labelimage1.destroy()
            labeltext1.destroy()
            lblInstruction1.destroy()
            lblRules1.destroy()
            btnStart1.destroy()
            gen1()
            startquiz1()








        img_student = PhotoImage(file="student.png")
        #img2=img1.subsample(0,1)
        labeltext1 = Label(
            top1,
            text = "Quiz  second",
            font = ("Comic sans MS",24,"bold"),
            background = "#ffffff",
        )

        labeltext1.pack(pady=(0,50))

        labelimagestudent1 = Label(
            top1,
            image = img_student,
            background = "#ffffff",
        )
        labelimagestudent1.pack(pady=(40,0))



        img_start = PhotoImage(file="start.png")

        btnStart1 = Button(
            top1,
            image = img_start,
            text="Play",
            compound="left",
            relief = FLAT,
            border = 0,
            command = startIspressed1,
        )
        btnStart1.pack()

        lblInstruction1 = Label(
        top1,
        text = "წაიკითხე წესები და დაიწყე თამაში",
        background = "#ffffff",
        font = ("Consolas",14),
        justify = "center",
        )
        lblInstruction1.pack(pady=(10,100))

        lblRules1 = Label(
        top1,
        text = "ეს ქვიზი შედგება 10 კითხვისგან.\nშენ გაქვს მხოლოდ 20 წამი კითხვებზე პასუხისთვის.\n კარგად დაფიქრდი სანამ უპასუხებ",
        width = 100,
        font = ("Times",14),
        background = "#000000",
        foreground = "#FACA2F",
        )
        lblRules1.pack()

        top1.mainloop()
    lado=PhotoImage(file="lado.png")
    ladozoma = lado.subsample(1,1)
    b = Button(frame2,image=ladozoma, command=lado, height=150, width=300,text="Quiz \n second",compound="left",bg="gray")
    b.pack(side="left")





master.mainloop()

我根本没有回调

0 个答案:

没有答案