Import tkinter as tk from tkinter import messagebox, ttk



Download 8.87 Kb.
Date29.02.2024
Size8.87 Kb.
#63703
kurs ishi kodi
Livestock-CDE-Test-Bank-Answer-Key, kurs ishi taqtimod

import tkinter as tk
from tkinter import messagebox, ttk
from quiz_data import quiz_data

# Joriy savol va variantlarni ko'rsatish funksiyasi


def show_question():
# Joriy savolni quiz_data faylidan olish
question = quiz_data[current_question]
qs_label.config(text=question["question"])

# Tugmalardagi variantlarni ko'rsatish


choices = question["choices"]
for i in range(4):
choice_btns[i].config(text=choices[i], state="normal") # Qayta tiklash tugmasi holati

# Feedback labelni tozalash va keyingi tugmasini o'chirish


feedback_label.config(text="")
next_btn.config(state="disabled")

# Tanlangan variantni tekshirish va feedbackni ko'rsatish


def check_answer(choice):
# Joriy savolni quiz_data faylidan olish
question = quiz_data[current_question]
selected_choice = choice_btns[choice].cget("text")

# Tanlangan variant to'gri javobga mos kelishini tekshirish


if selected_choice == question["answer"]:
# Balni yangilash va uni ko'rsatish
global score
score += 1
score_label.config(text="Ball: {}/{}".format(score, len(quiz_data)))
feedback_label.config(text="To'g'ri!", foreground="green")
else:
feedback_label.config(text="Noto'g'ri!", foreground="red")
# Barcha variant tugmalarini o'chirish va keyingi tugmani yoqish
for button in choice_btns:
button.config(state="disabled")
next_btn.config(state="normal")

# Keyingi savolga o'tish funksiyasi


def next_question():
global current_question
current_question +=1

if current_question < len(quiz_data):


# Agar ko'proq savollar bo'lsa, keyingi savolni ko'rsating
show_question()
else:
# Agar barcha savollarga javob berilgan bo'lsa,yakuniy ballni ko'rsatish va testni tugatish
messagebox.showinfo("Savollar tugadi",
"Savollar tugadi! Yakuniy ball: {}/{}".format(score, len(quiz_data)))
root.destroy()

# Asosiy oynani yaratish


root = tk.Tk()
root.title("Test tizimi")
root.geometry("600x500")

# Savol labelini yaratish


qs_label = ttk.Label(
root,
anchor="center",
wraplength=500,
padding=10
)
qs_label.pack(pady=10)

# Variantlar tugmasini yaratish


choice_btns = []
for i in range(4):
button = ttk.Button(
root,
command=lambda i=i: check_answer(i)
)
button.pack(pady=5)
choice_btns.append(button)

# Feedback labelni yaratish


feedback_label = ttk.Label(
root,
anchor="center",
padding=10
)
feedback_label.pack(pady=10)

# Hisobni boshlash


score = 0

# Ball labelini yaratish


score_label = ttk.Label(
root,
text="Ball: 0/{}".format(len(quiz_data)),
anchor="center",
padding=10
)
score_label.pack(pady=10)

# Keyingi tugmasini yaratish


next_btn = ttk.Button(
root,
text="Keyingi",
command=next_question,
state="disabled"
)
next_btn.pack(pady=10)

# Joriy savol indeksini ishga tushirish


current_question = 0

# Birinchi savolni ko'rsatish


show_question()

# Ekranda ko'rsatish


root.mainloop()
Download 8.87 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page