Created
January 28, 2015 13:05
-
-
Save anurag-ks/015c70be863d4e86326f to your computer and use it in GitHub Desktop.
This is my first ever *complete*, simple, amateurish, kinda-useless, desktop application built in Gtk toolkit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gi.repository import Gtk | |
class MainWindow(Gtk.Window): | |
def __init__(self): | |
Gtk.Window.__init__(self) | |
self.connect("delete-event", Gtk.main_quit) | |
self.set_size_request(240, 240) | |
self.set_resizable(False) | |
header = Gtk.HeaderBar() | |
header.props.title = "Calculator" | |
header.props.show_close_button = True | |
self.set_titlebar(header) | |
main = Gtk.Grid() | |
self.add(main) | |
top_box = Gtk.Box() | |
main.attach(top_box, 1, 0, 1, 1) | |
self.entry = Gtk.Entry() | |
self.entry.set_icon_from_stock(Gtk.EntryIconPosition.SECONDARY, | |
Gtk.STOCK_CLOSE) | |
self.entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY, True) | |
self.entry.set_size_request(240, 50) | |
self.entry.connect("icon-press", self.on_clear_icon_click) | |
self.entry.set_icon_tooltip_text(Gtk.EntryIconPosition.SECONDARY, | |
"Click to clear the text box") | |
self.entry.set_icon_sensitive(Gtk.EntryIconPosition.SECONDARY, | |
True) | |
top_box.add(self.entry) | |
number_grid = Gtk.Grid() | |
number_grid.set_column_spacing(2) | |
number_grid.set_row_spacing(2) | |
main.attach(number_grid, 1, 1, 1, 1) | |
self.add_numbers(number_grid) | |
tools_grid = Gtk.Grid() | |
tools_grid.set_column_spacing(2) | |
tools_grid.set_row_spacing(2) | |
main.attach(tools_grid, 1, 3, 1, 1) | |
self.add_tools(tools_grid) | |
def add_numbers(self, container): | |
row_cnt = 1 | |
col_cnt = 0 | |
for i in xrange(10): | |
button = Gtk.Button(str(i)) | |
button.set_size_request(47, 30) | |
button.connect("clicked", self.on_button_click) | |
if(i > 4): | |
container.attach(button, col_cnt, row_cnt+1, 1, 1) | |
col_cnt += 1 | |
else: | |
container.attach(button, i, row_cnt, 1, 1) | |
def add_tools(self, container): | |
plus = Gtk.Button("+") | |
plus.connect("clicked", self.on_button_click) | |
plus.set_size_request(47, 30) | |
container.attach(plus, 0, 0, 1, 1) | |
sub = Gtk.Button("-") | |
sub.connect("clicked", self.on_button_click) | |
sub.set_size_request(47, 30) | |
container.attach(sub, 1, 0, 1, 1) | |
div = Gtk.Button("/") | |
div.connect("clicked", self.on_button_click) | |
div.set_size_request(47, 30) | |
container.attach(div, 2, 0, 1, 1) | |
mul = Gtk.Button("*") | |
mul.connect("clicked", self.on_button_click) | |
mul.set_size_request(47, 30) | |
container.attach(mul, 3, 0, 1, 1) | |
evaluate = Gtk.Button("=") | |
evaluate.connect("clicked", self.on_eval_click) | |
evaluate.set_size_request(47, 30) | |
container.attach(evaluate, 4, 0, 1, 1) | |
open_bracket = Gtk.Button("(") | |
open_bracket.connect("clicked", self.on_button_click) | |
open_bracket.set_size_request(47, 30) | |
container.attach(open_bracket, 0, 1, 1, 1) | |
close_bracket = Gtk.Button(")") | |
close_bracket.connect("clicked", self.on_button_click) | |
close_bracket.set_size_request(47, 30) | |
container.attach(close_bracket, 1, 1, 1, 1) | |
point = Gtk.Button(".") | |
point.connect("clicked", self.on_button_click) | |
point.set_size_request(47, 30) | |
container.attach(point, 2, 1, 1, 1) | |
image = Gtk.Image() | |
image.set_from_stock(Gtk.STOCK_UNDO, True) | |
back_space = Gtk.Button() | |
back_space.connect("clicked", self.on_clear_click) | |
back_space.set_size_request(47, 30) | |
back_space.set_image(image) | |
container.attach(back_space, 3, 1, 1, 1) | |
def on_clear_click(self, button): | |
entry_text = self.entry.get_text() | |
self.entry.set_text(entry_text[0:len(entry_text)-1]) | |
def on_button_click(self, button): | |
entry_text = self.entry.get_text() | |
entry_text += button.get_label() | |
self.entry.set_text(entry_text) | |
def on_eval_click(self, button): | |
try: | |
self.entry.set_text(str(eval(self.entry.get_text()))) | |
except: | |
self.entry.set_text('') | |
def on_clear_icon_click(self, widget, icon_pos, event): | |
self.entry.set_text('') | |
main = MainWindow() | |
main.show_all() | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment