C
C
CImanov2015-09-15 01:00:00
Python
CImanov, 2015-09-15 01:00:00

Please give advice on how to improve the program code?

Hello, I would like you to take a look at the code below, I wrote a small calculator, I will immediately say that I took the mathematical functions from one video, so I do not fully understand their work, but basically the code is mine and I would like you to give me advice and recommendations and more would point out my mistakes in the code, I'm new to python, I can only stir up a cool interface, and I haven't studied the functions yet, see if you can somehow implement it differently or better,
Thank you very much in advance.

from tkinter import *
from tkinter import ttk

def iCalc(source, side):
    storeObj = Frame(source)
    storeObj.pack(side=side, expand=True, fill='both')
    return storeObj

def button(source, side, text, command=None):
    storeObj = Button(source, relief='flat',  text=text, command=command, font='Simple 16')
    storeObj.pack(side=side, expand=True, fill='both')
    return storeObj

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        StrVar = StringVar()

        self.title('Calculator')
        self.overrideredirect(True)
        self.minsize(width=256, height=384)
        self.geometry('384x384+192+192')
        self.attributes('-topmost', True)
        self.config(background='#2f2f2f')

        self.frmCaption = Frame(self, height=40, bg='#2f2f2f')
        self.frmCaption.propagate(False)
        self.frmCaption.pack(side='top', fill='both')

        self.frmFooter = Frame(self, height=20, bg='#f2f2f2')
        self.frmFooter.propagate(False)
        self.frmFooter.pack(side='bottom', fill='both')

        self.btnMove = Button(self.frmCaption, relief='flat', bg='#f2f2f2', cursor='fleur')
        self.btnMove.bind('<ButtonPress-1>', self.TeMotion)
        self.btnMove.bind('<ButtonRelease-1>', self.FeMotion)
        self.btnMove.bind('<B1-Motion>', self.GoMotion)
        self.btnMove.bind('<Double-Button-1>', self._dty)
        self.btnMove.place(x=2, y=2, width=36, height=36)
        self.entInput = Entry(self.frmCaption, relief='flat', textvariable=StrVar, justify='center', insertwidth=1, font='Simple 16')
        self.entInput.bind('<Return>', lambda e, s=self, storeObj=StrVar: s.calc(storeObj), '+')
        self.entInput.place(x=40, y=2, relwidth=1, width=-42, height=36)
        self.sizeGrip = ttk.Sizegrip(self.frmFooter).place(x=-20, relx=1, y=0, width=18, height=18)
        
        self.frmPack = Frame(self, bg='#fff')
        self.frmPack.propagate(False)
        self.frmPack.place(x=2, y=40, width=-4, height=-62, relwidth=1, relheight=1)
        for clearBut in ("C"):
            erase = iCalc(self.frmPack, TOP)
            for ichar in clearBut:
                button(erase, LEFT, ichar, lambda storeObj=StrVar, q=ichar: storeObj.set(''))

        for NumBut in ('789/', '456*', '123-', '0.+'):
            FunctionNum = iCalc(self.frmPack, TOP)
            for iEquals in NumBut:
                button(FunctionNum, LEFT, iEquals, lambda storeObj=StrVar, q=iEquals: storeObj.set(storeObj.get() + q))
        EqualsButton = iCalc(self.frmPack, TOP)
        for iEquals in '=':
            if iEquals == '=':
                btniEquals = button(EqualsButton, LEFT, iEquals)
                btniEquals.bind('<ButtonRelease-1>', lambda e, s=self, storeObj=StrVar: s.calc(storeObj), '+')
            else:
                btniEquals = button(EqualsButton, LEFT, iEquals, lambda storeObj=StrVar, s=' %s '%iEquals: storeObj.set(storeObj.get()+s))
    def calc(self, StrVar):
        try:
            StrVar.set(eval(StrVar.get()))
        except:
            StrVar.set('')

    def TeMotion(self, event):
        self.x = event.x
        self.y = event.y
    def FeMotion(self, event):
        self.x = None
        self.y = None
    def GoMotion(self, event):
        AxisX = event.x - self.x
        AxisY = event.y - self.y
        x = self.winfo_x() + AxisX
        y = self.winfo_y() + AxisY
        self.geometry('+%s+%s' % (x, y))
    def _dty(self, event):
        self.destroy()

if __name__ == '__main__':
    self = App().mainloop()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bromzh, 2015-09-15
@CImanov

You are suffering bullshit. First learn how to work with strings and numbers, lists, tuples, sets, dictionaries, iterators and generators. Then study functions, how they are defined and called in python, how to pass arguments, why single and double asterisks are needed in function parameters. Then study OOP, what it is in python, how it differs from other implementations. Why and when classes are needed at all, and when not. Then all sorts of Python tricks regarding classes, what is a class variable, class method, static method, property. Then you can learn decorators and other functional pieces of python. At the same time, you will understand what kind of lambdas are in your code, what are closures and variable visibility contexts. Then study the standard library: working with dates, with json, with the network, files and sockets, asynchronous I / O (I hope you study the 3rd branch).
Along the way, memorize pep8 . Well, all this is natural in practical examples. There are examples on all sorts of codeacademy and other similar sites.
Then you can choose a direction and study the libraries and frameworks that people often use. If it is, for example, a web, then it is django, tornado, etc.
And thoughtless copying of the code from the video. which you yourself don't understand, and sheets of rather similar tkinter code won't help you in learning this beautiful language.

A
Anton Konovalov, 2015-09-15
@akonovalov

Utilities pep8 , pylint , pep257 or their derivatives like flake8 will tell you what is wrong in your code. It is also recommended to look at the code (but without fanaticism) of popular sources, such as Twisted .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question