Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
1
Indexable
Never
import sys

try:
    import gi

    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
except ImportError:
    print('GTK not available')
    sys.exit(1)


class Calculator:
    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_from_file("calc.glade")
        self.builder.connect_signals(self)
        self.displayText = self.builder.get_object("display_text")
        self.window = self.builder.get_object("calc_window")

    def on_calc_window_destroy(self, widget):
        Gtk.main_quit()

    def on_Clear_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text[:-1])

    def on_Zero_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "0")

    def on_One_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "1")

    def on_Two_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "2")

    def on_Three_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "3")

    def on_Four_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "4")

    def on_Five_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "5")

    def on_Six_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "6")

    def on_Seven_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "7")

    def on_Eight_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "8")

    def on_Nine_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "9")

    def on_Division_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "/")

    def on_Multipilcation_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "*")

    def on_Subtraction_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "-")

    def on_Addition_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + "+")

    def on_Dot_clicked(self, widget):
        text = self.displayText.get_text()
        self.displayText.set_text(text + ".")

    def on_Result_clicked(self, widget):
        text = self.displayText.get_text()
        try:
            result = eval(text)
            self.displayText.set_text(str(result))
        except:
            self.displayText.set_text("error")

    def run(self):
        self.window.show_all()
        Gtk.main()


if _name_ == "_main_":
    calculator = Calculator()
    calculator.run()