Skip to content

Commit fbda0eb

Browse files
committed
Added navbar and footer for navigation.
1 parent ef0f993 commit fbda0eb

File tree

2 files changed

+106
-37
lines changed

2 files changed

+106
-37
lines changed

main.py

Lines changed: 103 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,84 @@
33
from tkinter import *
44

55

6-
def testing(parsed):
6+
def disable_item(user_answers, item_id):
7+
user_answers[item_id]['disabled'] = True
8+
9+
10+
def go_to_question(item_id, parsed, nav_buttons, msg, variants_frame, btn_prev, btn_comfirm, btn_next, user_answers):
11+
print('go to question ', item_id)
12+
print('user_answers', user_answers)
13+
# print(nav_buttons)
14+
15+
if item_id > 0:
16+
btn_prev.config(state=NORMAL)
17+
btn_prev.config(command=(lambda: go_to_question(item_id - 1, parsed,
18+
nav_buttons, msg, variants_frame,
19+
btn_prev, btn_comfirm, btn_next, user_answers)))
20+
else:
21+
btn_prev.config(state=DISABLED)
22+
23+
# btn_comfirm.config(state=nav_buttons[item_id]['state'])
24+
# btn_comfirm.config(
25+
# command=(lambda: nav_buttons[item_id].config(bg='green', disabledforeground='black',
26+
# state=DISABLED) or btn_comfirm.config(state=DISABLED)))
27+
if user_answers[item_id]['disabled']:
28+
btn_comfirm.config(state=DISABLED)
29+
else:
30+
btn_comfirm.config(state=NORMAL)
31+
btn_comfirm.config(
32+
command=(lambda: nav_buttons[item_id].config(bg='green')
33+
or disable_item(user_answers, item_id) or btn_comfirm.config(state=DISABLED)))
34+
35+
if item_id + 1 < len(parsed):
36+
btn_next.config(state=NORMAL)
37+
btn_next.config(
38+
command=(
39+
lambda: go_to_question(item_id + 1, parsed, nav_buttons, msg, variants_frame, btn_prev, btn_comfirm,
40+
btn_next, user_answers)))
41+
else:
42+
btn_next.config(state=DISABLED)
43+
44+
item = parsed[str(item_id)]
45+
item_type = item['type']
46+
question = item['question']
47+
question_text = question['text']
48+
49+
variants = question['variants']
50+
answers = item['answers']
51+
52+
msg.config(text=question_text)
53+
54+
return
55+
56+
if item_type == 'entry':
57+
entry = Entry(variants_frame)
58+
entry.config(font=resourses.constants.ENTRY_FONT)
59+
entry.config(width=5)
60+
entry.pack(expand=YES, fill=BOTH, padx=10, pady=10)
61+
elif item_type == 'checkbutton':
62+
for variant_id in map(str, range(len(variants))):
63+
variant = variants[variant_id]
64+
checkbutton = Checkbutton(variants_frame)
65+
checkbutton.config(text=variant)
66+
checkbutton.pack()
67+
68+
if False:
69+
print('id :', item['id'])
70+
print('type :', item['type'])
71+
print('question :', item['question'])
72+
print('answer :', item['answer'])
73+
74+
75+
def testing(root, parsed):
76+
user_answers = {i: {'disabled': False, 'answers': {}} for i in range(len(parsed))}
77+
# user_answers[0]['disabled'] = True
78+
# user_answers[1]['answers'] = {1, 2}
79+
# user_answers[2] = {'disabled': False, 'answers': {3, 4, 5}}
80+
781
win = Toplevel()
8-
win.minsize(width=600, height=500)
82+
win.minsize(width=750, height=500)
83+
win.maxsize(width=750, height=500)
984
root.withdraw()
1085

1186
# print(1)
@@ -16,15 +91,22 @@ def testing(parsed):
1691

1792
nav_frame = Frame(main_frame)
1893
nav_frame.config(bg="gray")
94+
nav_buttons = []
1995
for item_id in range(len(parsed)):
2096
btn = Button(nav_frame)
21-
btn.config(text=str(item_id + 1), font=resourses.constants.button_font)
97+
btn.config(text=str(item_id + 1), font=resourses.constants.BUTTON_FONT)
2298
btn.config(height=1, width=3)
99+
btn.config(overrelief=RAISED, activebackground='#DDD')
100+
btn.config(command=(lambda item_id=item_id: go_to_question(item_id, parsed, nav_buttons, \
101+
msg, variants_frame, btn_prev, btn_comfirm, \
102+
btn_next, user_answers)))
23103
btn.pack(side=LEFT, padx=10, pady=10)
24-
nav_frame.pack(expand=YES, fill=BOTH)
104+
nav_buttons.append(btn)
105+
nav_frame.pack(expand=NO, fill=BOTH)
106+
print(nav_buttons[0] is nav_buttons[1])
25107

26108
msg = Message(main_frame)
27-
msg.config(font=resourses.constants.message_font, justify=CENTER, width=700)
109+
msg.config(font=resourses.constants.MESSAGE_FONT, justify=CENTER, width=700)
28110
msg.config(bg="#FFF")
29111
msg.pack(expand=YES, fill=BOTH)
30112

@@ -33,41 +115,27 @@ def testing(parsed):
33115

34116
footer_frame = Frame(main_frame)
35117
footer_frame.config(bg='gray', height=30)
36-
footer_frame.pack(expand=YES, fill=BOTH)
118+
footer_frame.pack(side=BOTTOM, expand=NO, fill=BOTH)
37119

38120
btn_prev = Button(footer_frame)
39-
btn_prev.config(text='Предыдущий', font=resourses.constants.button_font)
121+
btn_prev.config(text='Предыдущий', font=resourses.constants.BUTTON_FONT)
40122
btn_prev.config(height=1, state=DISABLED)
123+
btn_prev.config(overrelief=RAISED, activebackground='#DDD')
41124
btn_prev.pack(side=LEFT, expand=YES, fill=BOTH, padx=20, pady=20)
42125

43126
btn_comfirm = Button(footer_frame)
44-
btn_comfirm.config(text='Подтвердить ответ', font=resourses.constants.button_font)
127+
btn_comfirm.config(text='Подтвердить ответ', font=resourses.constants.BUTTON_FONT)
45128
btn_comfirm.config(height=1)
129+
btn_comfirm.config(overrelief=RAISED, activebackground='#DDD')
46130
btn_comfirm.pack(side=LEFT, expand=YES, fill=BOTH, padx=20, pady=20)
47131

48132
btn_next = Button(footer_frame)
49-
btn_next.config(text='Следующий', font=resourses.constants.button_font)
133+
btn_next.config(text='Следующий', font=resourses.constants.BUTTON_FONT)
50134
btn_next.config(height=1, state=DISABLED)
135+
btn_next.config(overrelief=RAISED, activebackground='#DDD')
51136
btn_next.pack(side=RIGHT, expand=YES, fill=BOTH, padx=20, pady=20)
52137

53-
for item_id in map(str, range(len(parsed))):
54-
item = parsed[item_id]
55-
item_type = item['type']
56-
question = item['question']
57-
question_text = question['text']
58-
59-
variants = question['variants']
60-
answers = item['answers']
61-
62-
msg.config(text=question_text)
63-
64-
variants_frame
65-
66-
if False:
67-
print('id :', item['id'])
68-
print('type :', item['type'])
69-
print('question :', item['question'])
70-
print('answer :', item['answer'])
138+
go_to_question(0, parsed, nav_buttons, msg, variants_frame, btn_prev, btn_comfirm, btn_next, user_answers)
71139

72140
# win.protocol('WM_DELETE_WINDOW', win.quit)
73141
win.focus_set()
@@ -82,7 +150,7 @@ def testing(parsed):
82150

83151
root = Tk()
84152
root.minsize(width=600, height=500)
85-
# root.maxsize(width=300, height=300)
153+
# root.maxsize(width=600, height=500)
86154
root.title('Тестирование')
87155

88156
main_frame = Frame(root)
@@ -94,22 +162,22 @@ def testing(parsed):
94162
# label.config(font=resourses.constants.label_font)
95163
# label.pack()
96164

97-
msg = Message(main_frame, text=resourses.constants.intro_message)
98-
msg.config(font=resourses.constants.message_font, justify=CENTER)
99-
msg.config(bg="#FFF")
100-
msg.pack(expand=YES, fill=BOTH, padx=50, pady=20)
165+
welcome_msg = Message(main_frame, text=resourses.constants.intro_message)
166+
welcome_msg.config(font=resourses.constants.MESSAGE_FONT, justify=CENTER)
167+
welcome_msg.config(bg="#FFF")
168+
welcome_msg.pack(expand=YES, fill=BOTH, padx=50, pady=20)
101169

102170
footer_frame = Frame(main_frame)
103171
footer_frame.config(bg="#FFF")
104172
footer_frame.pack(expand=YES, fill=BOTH)
105173

106-
btn_start = Button(footer_frame, text='Начать тестирование', command=(lambda: testing(parsed)))
107-
btn_start.config(font=resourses.constants.button_font, bg="#BBB")
174+
btn_start = Button(footer_frame, text='Начать тестирование', command=(lambda: testing(root, parsed)))
175+
btn_start.config(font=resourses.constants.BUTTON_FONT, bg="#BBB")
108176
btn_start.config(width=15, height=1)
109177
btn_start.pack(side=LEFT, expand=YES, fill=BOTH, padx=20, pady=20)
110178

111179
btn_quit = Button(footer_frame, text='Завершить', command=footer_frame.quit)
112-
btn_quit.config(font=resourses.constants.button_font, bg='#BBB')
180+
btn_quit.config(font=resourses.constants.BUTTON_FONT, bg='#BBB')
113181
btn_quit.config(width=15, height=1)
114182
btn_quit.pack(side=RIGHT, expand=YES, fill=BOTH, padx=20, pady=20)
115183

resourses/constants.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
message_font = ('consolas', 20, 'roman')
2-
button_font = ('times', 20, '')
1+
MESSAGE_FONT = ('consolas', 20, 'roman')
2+
BUTTON_FONT = ('times', 20, '')
3+
ENTRY_FONT = ('times', 20, '')
34

45
intro_message = 'Добро пожаловать в тестирующую систему!\n' \
56
'Вам предстоит пройти нелегкое испытание, в ходе которого Вас проверят на знание информационных технологий.\n' \

0 commit comments

Comments
 (0)