import random
try:
import Tkinter as tk
except:
import tkinter as tk
class Test():
def __init__(self):
self.root = tk.Tk()
self.root.geometry("1920x1080")
self.root.attributes('-fullscreen', True)
label = tk.Label(self.root, text="Computer Science Quizzes",
font=("times", 20))
label.pack()
button = tk.Button(self.root, text = 'Quit',
command=self.quit, width=10, bg="red", fg="white",
font=("times", 16, "bold"))
button.place(x=1400, y=10)
label1 = tk.Label(self.root, text="Select your desired Module!",
font=("times", 20))
label1.place(x=450, y=370)
self.optvar = tk.StringVar(self.root)
self.optvar.set("Modules")
self.questions = {"1821": questions1811_list,
"1811": questions1811_list,
"1765": questions1765_list,
"1179": questions1179_list}
w = tk.OptionMenu(self.root, self.optvar, *self.questions)
w.place(x=780, y=373)
button = tk.Button(self.root, text = 'Go', command=self.run_test)
button.pack()
self.root.mainloop()
def run_test(self):
# if self.optvar.get() not in self.questions: Message(no selection)
questions = self.questions[self.optvar.get()]
qlist = []
for prompt, answer in questions:
qlist.append(Question(prompt, answer))
random.shuffle(qlist)
self.root.withdraw()
score = 0
for question in qlist:
print(question.prompt)
answer = input("Answer: ")
if answer == question.answer:
print("Correct!\n")
score += 1
else:
print("Wrong! The correct answer is " + question.answer + "\n")
print("\nEnd of the test.")
print("You got " + str(score) + " out of " + str(len(qlist)) + " questions correct")
input("Hit enter to return to menu.")
self.root.deiconify()
def quit(self):
self.root.destroy()
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
questions1811_list = [
["This term refers to an individual term in a list? \n(a) Element\n(b) Term\n(c) Shell\n(d) Item", "a"],
["What is the correct syntax to output Hello World in python?\n(a) print('Hello World')\n(b) echo(Hello World)"
"\n(c) echo Hello World\n(d) p(Hello World)", "a"],
["How would you create a variable with a value of 5?\n(a) x = 5\n(b) x = int(5)\n(c) x == 5"
"\n(d) All of the above", "b"],
["How would you display a line in the text?\n(a) say()\n(b) echo()\n(c) print()\n(d) write", "c"],
["How would you start a while loop?\n(a) x > y\n(b) while xy\n(c) while x > y;\n(d) while x > y:", "d"]
]
questions1821_list = [
["Process Models, data models and behavioural models are all models\ncreated under the Object Oriented Analysis"
" and Design approach.\n(a) True\n(b) False\n(c) Partly True\n(d) Only a selected few", "a"],
["Which of the following methodologies aim to have a working system\nwithin 60-90 days with an accelerated movement"
" from prototype to production?\n(a) Scrum\n(b) Rapid Application Development (RAD)\n(c) Extreme Programming (XP)"
"\n(d) Rational Unified Process (RUP)", "b"],
["The Rational Unified process provides a ___\n(a) Poorly documented system\n(b) Higher level of reuse\n(c) Totally"
" organised form of software development\n(d) Week off every other month", "b"],
["When updating records in a table, what clause must be used to specify which record(s) should be updated?"
"\n(a) UPDATE\n(b) DISTINCT\n(c) SET\n(d) WHERE", "d"],
["Which one of these is NOT part of the ERD Development Process?\n(a) Draw an entity model\n(b) Identify the"
" entities\n(c) Establish the data flow between the entities\n(d) Select the primary key for each entity", "c"]
]
questions1179_list = [
["The first three terms of an arithmetic series are 4 8 12. \nIdentify the arithmetic difference and the sum of"
" the first five terms \n(a) AD 3 Sum 11 \n(b) AD 4 Sum 12 \n(c) AD 4 Sum 60 \n(d) AD 3 Sum 10", "c"],
["Simplify (6+6i) x (6+8i) \n(a)-12+84i \n(b)34+84i \n(c)24+84i \n(d)48+98i", "b"],
["Solve these simultaneous equations for x and y z+y=2 and z-y=3 \n(a) x=2.5, y=-0.5 \n (b)x=2 y=0.5 \n (c)x=2 y=1"
" \n (d)x=5 y=1", "a"],
["For the complex 5x+4y calculate the r and the theta for the\npolar form of the number \n(a)r=0, theta=0.1 rad"
"ians \n(b)r=6.40, theta=0.67 radians \n(c)r=4.00 theta=0.5 radians \n(d)r=0, theta=0.1 radians", "b"],
["Find the inverse of the function f(z)=10x+9\n(a) -0.10 * -0.90\n(b) 0.90+10x\n(c) -0.10x+9.00"
"\n(d)=0.10+0.20", "a"]
]
questions1765_list = [
["How do you find the IP from the command prompt?\n(a) ipconfig\n(b) /ipconfig\n(c) ipinfo\n(d) info_ip.get", "a"],
["What is the aspect ratio of a monitor capable of displaying "
"an image of 1920x1080?\n(a) 10:5\n(b) 4:3\n(c) 16:9\n(d) 2.39:1", "c"],
["What is the correct metric prefix of a pico meter?\n(a) 10^2\n(b) 10^-12\n(c) 10^-15\n(d) 10^-9", "b"],
["Which transmission system is a telephone call "
"part of?\n(a) Simplex\n(b) Full Duplex\n(c) Continued\n(d) Half Duplex", "b"],
["___ are tools that allow the user to find specific document "
"through key words or menu choices.\n(a) HTTP\n(b) URL\n(c) E-mails\n(d) Search Engines", "d"]
]
app = Test()
Welcome Joshua.
It seems people more knowledgable than me are not here at the moment, so I will try to help you on your way.
Indeed frames are the way to go. The tkinter interface to tk has a frame which I have used.
The code I wrote:
import tkinter as tk
class SelectFrame(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
self.choice_label = tk.Label(self, text="Choose a set")
self.choice_label.grid(row=0, column=0, sticky="W")
self.choosen_questions = tk.StringVar(master=self)
self.choice_field = tk.Entry(self, textvariable=self.choosen_questions)
self.choice_field.grid(row=0, column=1, sticky="W")
self.go_ = tk.Button(self, command=self.show_questions, text="Go!")
self.go_.grid(row=1, column=1, sticky="SE")
self.grid(row=0, column=0, sticky="NEWS")
def show_questions(self):
root.switch_to_questions(self.choosen_questions.get())
class QuestionFrame(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
self.question_label = tk.Label(self,
text="Why is Python a great language?")
self.question_label.grid(row=0, column=0, sticky="W")
self.answer = tk.StringVar(master=self)
self.question_field = tk.Entry(self, textvariable=self.answer)
self.question_field.grid(row=0, column=1, sticky="W")
self.select_ = tk.Button(self, command=root.show_selecting, text="Other set?")
self.select_.grid(row=1, column=1, sticky="SE")
class RootWindow(tk.Tk):
def __init__(self):
super().__init__()
self.select_frame = SelectFrame(self)
self.question_frame = QuestionFrame(self)
def switch_to_questions(self, question):
self.select_frame.grid_forget()
self.question_frame.grid(row=0, column=0, sticky="NESW")
def show_selecting(self):
self.question_frame.grid_forget()
self.select_frame.grid(row=0, column=0, sticky="NESW")
if __name__ == "__main__":
root = RootWindow()
root.mainloop()
Some remarks:
- You use
pack
andplace
to create your layout. Do not mix these. They seem ordinary functions, but on first use they install a layout manager and you will run into trouble using 2 layout managers that are unaware of each other. In my example code I usegrid
, butpack
is also generally liked.place
though is discouraged. - I have shown a method in which the frames are created at startup and re-used. If you want to, you can also destroy the frame and re-create it as needed. When the data on the frame is complex that may be advantageous.
- For organization I use different classes for items in the User Interface. I find it makes the code easier to structure.
- The trying to import
Tkinter
and on failure trytkinter
is no longer advised. Python2 is gone for some time, just use the Python3 idiom.