Tried my luck with a simple calculator. As always I welcome feedback.
Esc key will clear. numpad keys work, and enter keys to get totals
Esc key will clear. numpad keys work, and enter keys to get totals
#! /usr/bin/env python3 import tkinter as tk from functools import partial import decimal class Calculator: def __init__(self, parent): self.parent = parent self.parent.rowconfigure(0, weight=1) self.parent.columnconfigure(0, weight=1) # Mainframe mainframe = tk.Frame(self.parent) mainframe.grid(column=0, row=0, sticky='news') mainframe.grid_columnconfigure(0, weight=3) mainframe.grid_rowconfigure(0, weight=3) buttonframe = tk.Frame(mainframe) buttonframe.grid(column=0, row=1, sticky='new') buttonframe.grid_columnconfigure(0, weight=3) textframe = tk.Frame(mainframe, padx=3, pady=3, borderwidth=3, relief='groove') textframe.grid(column=0, row=0, sticky='new') textframe.grid_columnconfigure(0, weight=3) self.valid = self.parent.register(self.valid_input) self.entry = tk.Entry(textframe) self.entry.configure(validate='key', validatecommand=(self.valid, '%S')) self.entry['font'] = 'sans 14 normal' self.entry['relief'] = 'sunken' self.entry.focus() self.entry.grid(column=0, row=0, sticky='new') # Do the buttons numbers = [7,8,9,4,5,6,1,2,3,0] self.symbols = ['=', '*', '/', '+', '-', '.'] i = 0 j=0 for number in numbers: btn = tk.Button(buttonframe, text=number, command=partial(self.press, number), padx=35, pady=5) btn['font'] = 'sans 14 normal' btn.grid(column=i, row=j, sticky='news') if i >= 2: i = 0 j += 1 else: i += 1 btn = tk.Button(buttonframe, text=self.symbols[5], padx=35, pady=5, command=partial(self.press, self.symbols[5])) btn['font'] = 'sans 14 normal' btn.grid(column=1, row=3, sticky='news') btn = tk.Button(buttonframe, text=self.symbols[0], padx=35, pady=5, command=partial(self.calculations, None)) btn['font'] = 'sans 14 normal' btn.grid(column=2, row=3, sticky='news') i = 0 for symbol in self.symbols: if symbol != self.symbols[5] and symbol != '=': btn = tk.Button(buttonframe, text=symbol, padx=35, pady=5, command=partial(self.press, symbol)) btn['font'] = 'sans 14 normal' btn.grid(column=3, row=i, sticky='news') i += 1 btn = tk.Button(buttonframe, text='Clear', fg='red', command=partial(self.clear, None)) btn['font'] = 'sans 14' btn.grid(columnspan=4, column=0, row=5, sticky='news') self.parent.bind('<Return>', partial(self.calculations)) self.parent.bind('<KP_Enter>', partial(self.calculations)) self.parent.bind('<Escape>', partial(self.clear)) def clear(self, event): self.entry.delete(0, tk.END) def valid_input(self,num): if num.isalpha() or num == '=': return False else: return True def press(self, num): self.entry.insert(tk.END, num) def calculations(self, event): values = self.entry.get() self.entry.delete(0, tk.END) result = eval(values) if isinstance(result, float): result = decimal.Decimal(result).normalize() else: result = result self.entry.insert(tk.END, result) def main(): root = tk.Tk() root.title('Calculator') root.configure(borderwidth=5, relief='ridge', padx=3, pady=3) Calculator(root) root.mainloop() if __name__ == '__main__': main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts