Thursday, 15 March 2018

Python function eg - Day 20


  • this will explain how to create a funtion in python and call it,if the funtion doesnot returns any value then it will be treated as none
  • to view the definition of the function you can do -- ctrl + left click

  •   
    def python_food():
        print("Spinach is good for health")
    
    python_food()
    print (python_food())
    
    def center_text():
        width = 50
        text = 'Spinach is good for health'
        leftalignment = (width - len(text))//2
        print(' '*leftalignment,text)
    
    center_text()
    print('='*20)
    
    def center_text1(text):
        width = 50
        leftalignment = (width - len(text))//2
        print(' '*leftalignment,text)
    
    
    
    center_text1('Spinach is Good for Health')
    center_text1('Excecise is goood for body')
    center_text1('Life is worth living')
    center_text1('Love yourself')
     
    
  • the below code is the text behind the print function,we can see only the basic info not the code since this is written in C-language
  •  
     
    def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
        """
        print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
        
        Prints the values to a stream, or to sys.stdout by default.
        Optional keyword arguments:
        file:  a file-like object (stream); defaults to the current sys.stdout.
        sep:   string inserted between values, default a space.
        end:   string appended after the last value, default a newline.
        flush: whether to forcibly flush the stream.
        """
        pass
     
    

    Python tkinter - Calculator GUI - Day 20


  • this is the code for the model calculator,it is just a GUI,funtionality will be implemented in later lessons

  •   
    import tkinter
    
    keys = [[('C','1'),('CE','1')],
            [('7','1'),('8','1'),('9','1'),('+','1')],
            [('4','1'),('5','1'),('6','1'),('-','1')],
            [('1','1'),('2','1'),('3','1'),('*','1')],
            [('0','1'),('=','1'),('/','1')]]
    
    mainWindowPadding = 10
    
    mainWindow = tkinter.Tk()
    mainWindow.title("Calculator")
    mainWindow.geometry("800x480-8+200")
    mainWindow["padx"]= mainWindowPadding
    
    result = tkinter.Entry(mainWindow)
    result.grid(row=0,column=0,sticky = 'nsew')
    
    keypad = tkinter.Frame(mainWindow)
    keypad.grid(row=1,column=0,sticky = 'nsew')
    
    row = 0
    for keyrow in keys:
        column = 0
        for key in keyrow:
            tkinter.Button(keypad,text = key[0]).grid(row=row,column=column,
    columnspan = key[1],sticky ='ew')
            column+=1
        row+=1
    
    
    mainWindow.update()
    mainWindow.minsize(keypad.winfo_width()+mainWindowPadding,result.winfo_height()
    +keypad.winfo_height())
    mainWindow.maxsize(keypad.winfo_width()+50 + mainWindowPadding,result.winfo_height()+50 
    + keypad.winfo_height())
    mainWindow.mainloop()