How can I display thesubmit buttoninstead of next button in only Question 5th in my code? I have tried to do this, but this is not being. I have made two files respectively main.py and data.json file. Help me to do this.
- 主文件. py**
from tkinter import *
from tkinter import messagebox as mb
import json
from tkinter import ttk
import tkinter as tk
from tkinter.constants import *
class Quiz:
def __init__(self):
# set question number to 0
self.q_no=0
# assigns ques to the display_question function to update later.
self.display_title()
self.display_question()
# opt_selected holds an integer value which is used for
# selected option in a question.
self.opt_selected=IntVar()
self.opts=self.radio_buttons()
# display options for the current question
self.display_options()
# displays the button for next and exit.
self.buttons()
# no of questions
self.data_size=len(question)
# keep a counter of correct answers
self.correct=0
def display_result(self):
# calculates the wrong count
wrong_count = self.data_size - self.correct
correct = f"Correct: {self.correct}"
wrong = f"Wrong: {wrong_count}"
# calcultaes the percentage of correct answers
score = int(self.correct / self.data_size * 100)
result = f"Score: {score}%"
# Shows a message box to display the result
mb.showinfo("Result", f"{result}\n{correct}\n{wrong}")
def check_ans(self, q_no):
# checks for if the selected option is correct
if self.opt_selected.get() == answer[q_no]:
# if the option is correct it return true
return True
def next_btn(self):
if self.check_ans(self.q_no):
self.correct += 1
# Moves to next Question by incrementing the q_no counter
self.q_no += 1
# checks if the q_no size is equal to the data size
if self.q_no==self.data_size:
# if it is correct then it displays the score
self.display_result()
# destroys the GUI
root.destroy()
else:
# shows the next question
self.display_question()
self.display_options()
def buttons(self):
# The first button is the Next button to move to the
# next Question
next_button = Button(root, text="NEXT",command=self.next_btn,
width=10,bg="#F2A30F",fg="white",font=("ariel",16,"bold"))
# placing the button on the screen
next_button.place(x=350,y=380)
# This is the second button which is used to Quit the GUI
quit_button = Button(root, text="Quit", command=root.destroy,
width=5,bg="black", fg="white",font=("ariel",16," bold"))
quit_button.place(x=1445,y=50)
def display_options(self):
val=0
# deselecting the options
self.opt_selected.set(0)
for option in options[self.q_no]:
self.opts[val]['text']=option
val+=1
# This method shows the current Question on the screen
def display_question(self):
# setting the Question properties
q_no = Label(root, text=question[self.q_no], width=60,
font=( 'ariel' ,16, 'bold' ), anchor= 'w' )
#placing the option on the screen
q_no.place(x=70, y=100)
# This method is used to Display Title
def display_title(self):
title = Label(root, text="Your has been started.",
width=90, bg="#F2A30F",fg="white", font=("ariel", 20, "bold"))
title.place(x=0, y=2)
def radio_buttons(self):
# initialize the list with an empty list of options
q_list = []
# position of the first option
y_pos = 150
# adding the options to the list
while len(q_list) < 4:
# setting the radio button properties
radio_btn = Radiobutton(root,text=" ",variable=self.opt_selected,
value = len(q_list)+1,font = ("ariel",14))
# adding the button to the list
q_list.append(radio_btn)
# placing the button
radio_btn.place(x = 100, y = y_pos)
# incrementing the y-axis position by 40
y_pos += 40
return q_list
def start():
global hours, minutes, seconds
if hours == 4:
return
seconds -=1
if seconds == 00:
minutes -= 1
seconds = 60
if minutes == 00 and seconds == 00:
hours = hours+1
clock.config(text=f'{hours:02}:{minutes:02}:{seconds:02}')
root.after(1000, start)
root = Tk()
root.geometry('1920x1080')
root.title("EXAM")
clock = tk.Label(root, font=("bold",20), text="00:00:00")
clock.place(x=1300,y=55)
hours,minutes,seconds=0,0,60
start()
with open('data.json') as f:
data = json.load(f)
question = (data['question'])
options = (data['options'])
answer = (data[ 'answer'])
quiz = Quiz()
root.after(60000,lambda:root.destroy())
root.mainloop()
- 数据. json**
{
"question": [
"Q1. Which of the following is not a keyword in Python language?",
"Q2. Which character is used in Python to make a single line comment?",
"Q3. Which one of the following is the correct extension of the Python file?",
"Q4. In which language is Python written?",
"Q5. Who developed the Python language?"
],
"answer": [
1,
3,
4,
3,
2
],
"options": [
["a. val",
"b. raise",
"c. try",
"d. with"
],
["a. /",
"b. //",
"c. #",
"d. !"
],
["a. python",
"b. .p",
"c. Both a and b",
"d. .py"
],
["a. English",
"b. PHP",
"c. C",
"d. All of the above"
],
["a. Zim Den",
"b. Guido van Rossum",
"c. Niene Stom",
"d. Wick van Rossum"
]
]
}
我还想显示上一个按钮,但我不知道,我该怎么做?
1条答案
按热度按时间dba5bblo1#
首先,创建一个文本跟踪器变量,该变量包含要更改的文本。在按钮代码中,将初始按钮声明从
text="NEXT"
更改为text=self.TRACK_VAR_NAME
为此,在类中为按钮文本添加一个属性(例如:
self.TRACK_VAR_NAME = 'Next'
)检查您尝试所有问题的时间时,添加更改跟踪器变量的语句。如果是,请将
self.TRACK_VAR_NAME
更改为"Submit",然后使用next_button
的configure
方法更改文本和。(self.next_button.configure(text=self.TRACK_VAR_NAME)
)TL; DR:使用
next_button
的configure
方法更改文本。