python 3 calculator with tkinter python 3 calculator with tkinter tkinter tkinter

python 3 calculator with tkinter


Not really the place for code reviews, but I'm going to do it anyway because I'm hardcore procrastinating.

Good code

First up, your code works (I assume), it looks fine. Good code is good code in any language. Some people obsess over code being Pythonic, I'm not one of those people.

GUI structuring

GUI code sucks. It is repetitive, verbose, fiddly and ugly. This isn't a critique of your code, it is a comment on all GUI code, there doesn't seem to be a good solution. We manage this by separating the GUI code from everything else. There are formal approaches to this like Model-View-Controller and principals like the Separation of Concerns. The key is to fully split your ugly fragile GUI code from the real code you care about so we can try and forget how ugly it is.

Your implementation closely ties the GUI with the functionality. This is a trivial level problem so this isn't so bad. However as a learning exercise you should split them and do it the "right" way, start by creating a second class and move the evaluation function across.

eval

The second big thing is the use of the eval function. It is very elegant, convert the input into python compatible math and just ask python to give you the answer. Putting it slightly differently you are taking user input, filtering it a bit and executing it outright. With my security background it gives me the shakes. Not an issue for this program, you are executing locally, the user can't do anything they can't do normally. Don't do anything like this with online code though.

main

Finally, standard practice when combining classes and code like this is to put the code behind a __main__ conditional. This allows you to import the code elsewhere for testing etc. The following link explains it nicely: