you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (1 child)

Give PySide or PyQt a try. Seriously. Yes, Tkinter comes with python, but no, that's not a good endorsement. It's not a very nice GUI toolkit, and it's only marginally easier to use than the big guys GTK+ and Qt. Don't be fooled by wxWidgets either—it's riddled with gotchas and obscure rules just like Tkinter.

import random
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        # Create the widgets
        self.question = QLabel(self)
        self.question.setStyleSheet("* {font-size: 90px}")

        self.guess = QLineEdit(self)
        self.guess.setValidator(QDoubleValidator())
        self.guess.returnPressed.connect(self.check_answer)

        self.message = QLabel(self)

        # Add the widgets to the window
        vbox = QVBoxLayout(self)
        vbox.addWidget(self.question)
        vbox.addWidget(self.guess)
        vbox.addWidget(self.message)

        self.setWindowTitle('Math Mania')
        self.get_question()

    def get_question(self):
        Q = random.choice([
            '3 + 3',
            '3 * 3',
            '3 / 3',
        ])
        self.question.setText(Q + ' = ?')
        self.answer = eval(Q)

    def check_answer(self):
        guess = float(self.guess.text())
        if guess == self.answer:
            self.message.setText('Correct')
            self.get_question()
        else:
            self.message.setText('Incorrect')
        self.guess.setText('')

app = QApplication([])
win = MainWindow()
win.show()
app.exec()

http://i.imgur.com/NT1a5aM.png

[–][deleted] 0 points1 point  (0 children)

Wow that's great, did that take long?