Tkinter Calculator - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: Tkinter Calculator (/thread-27912.html) |
Tkinter Calculator - menator01 - Jun-26-2020 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 #! /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() RE: Tkinter Calculator - GOTO10 - Jun-27-2020 The interface looks great (on Windows 10), nice and clean. I accidentally caused a syntax error on my first entry because I fat-fingered a key and added an extra decimal point by mistake. This is the first time I've seen partial used, so I had to read up on it to understand how you are using it here.Great job! RE: Tkinter Calculator - menator01 - Jun-27-2020 I use partial to pass variables |