簡體   English   中英

如何在第二幀上制作 tkinter gui 多項選擇題?

[英]How to make tkinter gui multiple choice questions on Second frame?

我正在嘗試在問題框架的問題 label 下創建多項選擇題,有人知道如何創建這些問題,這將有很大幫助,謝謝,它為我進行學校評估以創建資本測驗以詢問有關首都國家的問題如果他們得到正確的分數,用戶可以單擊並跟蹤分數的多項選擇,謝謝。 到目前為止,我的 CapitalQuiz 代碼如下所示。 我想做一個資本測驗,在用戶輸入他們的姓名和年齡后,他們點擊下一個單選按鈕,將他們帶到問題框架,在那里他們將看到多項選擇題供他們回答並跟蹤分數記錄另一個框架謝謝。

class CapitalQuiz:
    def __init__(self,parent):
        
        
        self.Welcome = Frame(parent)
        self.Welcome.grid(row=0, column=0)
        
        self.TitleLabel = Label(self.Welcome, text = "Welcome to Capital Quiz",
                                bg = "black", fg = "white", width = 20, padx = 30, pady = 10, font = ("Time", '14', "bold italic"))
        self.TitleLabel.grid(columnspan = 2)
            
        self.NextButton = ttk.Button(self.Welcome, text = 'Next', command = self.show_Questions)
        self.NextButton.grid(row = 8, column = 1)
        
        self.Questions = Frame(parent)
        
        self.QuestionsLabel = Label(self.Questions, text = "Quiz Questions",
                                    bg = "black", fg = "white", width = 20, padx = 30, pady = 10,
                                    font = ("Time", '14', "bold italic"))
        self.QuestionsLabel.grid(columnspan = 2)
        
        self.HomeButton = ttk.Button(self.Questions, text = 'Home', command = self.show_Welcome)
        self.HomeButton.grid(row = 8, column = 1)
        
        
    def show_Welcome(self):
        self.Questions.grid_remove()
        self.Welcome.grid()
        
    def show_Questions(self):
        self.Welcome.grid_remove()
        self.Questions.grid()

代碼的最后一部分我無法在此處鍵入代碼,因此我提供了當前測驗運行的圖像,在代碼的最后添加此代碼以在定義函數之后運行測驗。

代碼的最后一部分

試試這個代碼。

from tkinter import *
from tkinter import ttk
class CapitalQuiz:
    def __init__(self,parent):
        
        self.parent = parent
        self.Welcome = Frame(self.parent)
        self.Welcome.pack(fill=BOTH,expand=1)
        
        self.TitleLabel = Label(self.Welcome, text = "Welcome to Capital Quiz",
                                bg = "black", fg = "white", font = ("Time", '14', "bold italic"))
        self.TitleLabel.pack(side=TOP,fill=X)
            
        self.NextButton = ttk.Button(self.Welcome, text = 'Next', command = self.show_Questions)
        self.NextButton.place(x=20,y=60)
        
        self.Questions = Frame(self.parent)
        
        self.QuestionsLabel = Label(self.Questions, text = "Questions",
                                    bg = "black", fg = "white", width = 20, padx = 30, pady = 10,
                                    font = ("Time", '14', "bold italic"))
        self.once_done=False
        self.QuestionsLabel.pack(side=TOP,fill=X,anchor="w")
        
        Label(self.Questions, text = "Q1.  What is the capital of United States?",font=("arial",12,"bold")).place(x=10,y=50)
        self.capital_one=StringVar()
        self.capital_one.set("hellow")
        Radiobutton(self.Questions,text="Washington DC",font=("arial",12),variable=self.capital_one,value="Washington DC").place(x=20,y=80)
        Radiobutton(self.Questions,text="London",font=("arial",12),variable=self.capital_one,value="London").place(x=20,y=110)
        Radiobutton(self.Questions,text="Delhi",font=("arial",12),variable=self.capital_one,value="Delhi").place(x=20,y=140)
        Radiobutton(self.Questions,text="Tokyo",font=("arial",12),variable=self.capital_one,value="Tokyo").place(x=20,y=170)
        Label(self.Questions, text = "Q2.  What is the capital of Russia?",font=("arial",12,"bold")).place(x=10,y=200)
        self.capital_two=StringVar()
        self.capital_two.set("hellow")
        Radiobutton(self.Questions,text="Moscow",font=("arial",12),variable=self.capital_two,value="Moscow").place(x=20,y=230)
        Radiobutton(self.Questions,text="Islamabad",font=("arial",12),variable=self.capital_two,value="Islamabad").place(x=20,y=260)
        Radiobutton(self.Questions,text="Delhi",font=("arial",12),variable=self.capital_two,value="Delhi").place(x=20,y=290)
        Radiobutton(self.Questions,text="Tokyo",font=("arial",12),variable=self.capital_two,value="Tokyo").place(x=20,y=320)
        Label(self.Questions, text = "Q3.  New Delhi is the capital of which country?",font=("arial",12,"bold")).place(x=10,y=350)
        self.capital_three=StringVar()
        self.capital_three.set("hellow")
        Radiobutton(self.Questions,text="India",font=("arial",12),variable=self.capital_three,value="India").place(x=20,y=380)
        Radiobutton(self.Questions,text="Canada",font=("arial",12),variable=self.capital_three,value="Canada").place(x=20,y=410)
        Radiobutton(self.Questions,text="Japan",font=("arial",12),variable=self.capital_three,value="Japan").place(x=20,y=440)
        Radiobutton(self.Questions,text="Spain",font=("arial",12),variable=self.capital_three,value="Spain").place(x=20,y=470)
        Label(self.Questions, text = "Q4.  What is the capital of Canada?",font=("arial",12,"bold")).place(x=10,y=500)
        self.capital_four=StringVar()
        self.capital_four.set("hellow")
        Radiobutton(self.Questions,text="Ottawa",font=("arial",12),variable=self.capital_four,value="Ottawa").place(x=20,y=530)
        Radiobutton(self.Questions,text="Paris",font=("arial",12),variable=self.capital_four,value="Paris").place(x=20,y=560)
        Radiobutton(self.Questions,text="Madrid",font=("arial",12),variable=self.capital_four,value="Madrid").place(x=20,y=590)
        Radiobutton(self.Questions,text="Beijing",font=("arial",12),variable=self.capital_four,value="Beijing").place(x=20,y=620)
        Label(self.Questions, text = "Q5.  State True or False: Beijing is the capital of China?",font=("arial",12,"bold")).place(x=10,y=650)
        self.capital_five=StringVar()
        self.capital_five.set("hellow")
        Radiobutton(self.Questions,text="True",font=("arial",12),variable=self.capital_five,value="True").place(x=20,y=680)
        Radiobutton(self.Questions,text="False",font=("arial",12),variable=self.capital_five,value="False").place(x=20,y=710)
        self.HomeButton = ttk.Button(self.Questions, text = 'Home', command = self.show_Welcome)
        self.HomeButton.place(x=30,y=760)
        self.SubmitButton = ttk.Button(self.Questions, text = 'Submit', command = self.submit)
        self.SubmitButton.place(x=130,y=760)
            
    def submit(self):
        count=0
        if self.capital_one.get()=="Washington DC":
            count+=1
        if self.capital_two.get()=="Moscow":
            count+=1
        if self.capital_three.get()=="India":
            count+=1
        if self.capital_four.get()=="Ottawa":
            count+=1
        if self.capital_five.get()=="True":
            count+=1
        Label(self.Questions,font=("arial",40,"bold"),text=f"You scored: {str(count)}/5").place(x=500,y=50)
        self.c=count
        self.capital_five.set("hellow")
        self.capital_four.set("hellow")
        self.capital_three.set("hellow")
        self.capital_two.set("hellow")
        self.capital_one.set("hellow")
        self.once_done=True
        self.SubmitButton.config(state=DISABLED)
        
    def show_Welcome(self):
        if self.once_done==True:
            for i in self.Questions.winfo_children():
                i.destroy()
            self.Questions.pack_forget()
        else:
            self.Questions.pack_forget()
        self.Welcome.pack(fill=BOTH,expand=1)
        
    def show_Questions(self):
        self.Welcome.pack_forget()
        if self.once_done==True:
            self.Questions.pack(fill=BOTH,expand=1)
            Label(self.Questions,font=("arial",40,"bold"),text=f"You scored: {str(self.c)}/5").place(x=500,y=50)
            Label(self.Questions,font=("arial",30,"bold"),text="You can only attempt the quiz once.").place(x=500,y=160)
        else:
            self.Questions.pack(fill=BOTH,expand=1)
if __name__ =="__main__":
    root = Tk()

    frames = CapitalQuiz(root)
    root. title("Quiz")
    root . mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM