Skip to content

Instantly share code, notes, and snippets.

@MikeTheWatchGuy
Created October 12, 2018 17:42
Show Gist options
  • Save MikeTheWatchGuy/95519de5dab70fe57d6481b16bc69f42 to your computer and use it in GitHub Desktop.
Save MikeTheWatchGuy/95519de5dab70fe57d6481b16bc69f42 to your computer and use it in GitHub Desktop.
import PySimpleGUI as sg
import random
# The callback functions
def answer_correct():
print('answer is correct')
words = [('1', 'one'), ('2', 'two'), ('3', 'three'), ('4', 'four'), ('5', 'five'), ('6', 'six')]
answer_1 = words[0][1]
answer_2 = words[1][1]
answer_3 = words[2][1]
answer_4 = words[3][1]
answer_5 = words[4][1]
answer_6 = words[5][1]
pick_a_pair = random.choice(words)
to_translate, correct_answer = pick_a_pair
print(to_translate, '<- need to transale this one')
print(correct_answer, '<- correct answer')
# Layout the design of the GUI, write text (answers) on top the buttons
layout = [[sg.Text('', font=('Helvetica', 23), auto_size_text=True, key='_totranslate_')],
[sg.ReadButton(answer_1, font=('Helvetica', 20))],
[sg.ReadButton(answer_2, font=('Helvetica', 20))],
[sg.ReadButton(answer_3, font=('Helvetica', 20))],
[sg.ReadButton(answer_4, font=('Helvetica', 20))],
[sg.ReadButton(answer_5, font=('Helvetica', 20))],
[sg.ReadButton(answer_6, font=('Helvetica', 20))],
[sg.Button('EXIT', button_color=(sg.YELLOWS[0], sg.GREENS[0]))],
[sg.Quit()]
]
window = sg.Window('Button callback example').Layout(layout).Finalize()
# Event loop. Read buttons, make callbacks
while True:
pick_a_pair = random.choice(words)
to_translate, correct_answer = pick_a_pair
print('New try.......')
window.FindElement('_totranslate_').Update(value=str(to_translate))
button, value = window.Read()
# NOTE: I think I need to use: window.ReadNonBlocking()
# button, values = window.ReadNonBlocking()
if button is None or button == 'EXIT':
break
if button == correct_answer:
answer_correct()
else:
sg.PopupOK('Correct answer was: ', correct_answer)
sg.PopupOK('Bye ;-) ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment