Saturday, 28 December 2019

Labels

Well, so today we will be diving deep into our course of learning Tkinter GUI based stuff!
hmmm so for today I will be introducing you guys with creating Labels!

But before we start let me tell you about Labels, like what it really is? why you would like to know this stuff and blah blah!!!

Well, labels are actually a space created by a programmer where the users can give some inputs!

To be more clear let's take an example:
so we all use facebook in our day to day life. yeah! so the first thing you do while logging into your account is, you give your username and password. right!
So the text that signifies what the user must write is called the label!

So I am guessing that you all are smart enough to relate to its necessity in your GUI based program!

As some great person said: IF YOU ARE MAKING A GUI BASED APP AND YOU 
                                            DON'T USE USERS INPUTS THEN WHAT'S THE
                                            POINT OF MAKING GUI STUFF!

so I am done talking. Now it's time for us to learn the actual coding!

Code!

from Tkinter import *                             #why? (check Explanation)

root=Tk()                                                #why? (check Explanation)


label1=Label(root, text="Username")      # So here we are assinging the Label 
                                                                  # function to a variable (which can be  
                                                                  # anything). I simply use the function  
                                                                  # names in my variables bcz when i
                                                                  # look back to my code it's easy for me 
                                                                  #to remember why I used that variable!

                                                                   #Here I did use two parameters to 
                                                                   #the function. First is "root" it means
                                                                   #for which part of the window I want
                                                                   #to make this stuff part of which
                                                                   #certainly means here for the Tkinter
                                                                   #screen!

                                                                   #And the second parameter is TEXT
                                                                   #on which we assign what we want to
                                                                   #show before the space! like you see
                                                                   #"Email" written before the space
                                                                   #where you write your email address!
                                                                   #As you are guessing yeah it tells the
                                                                   #user what to fill in the space
                                                                   #provided! 



label1.pack()                          #Okay so what is this! give a sec to explain...here
                                               #in TKINTER we need to pack things up before we
                                               #run our program. It can be explained in this
                                               #way that it is a function which adds functionality
                                               #to the function assigned above...and it must be
                                               #used after every other function we use!
                                                                 
                                               #Wait there's more to it! It not only tells the Tkinter
                                               #To make the function work but also signifies it's
                                               #position!


                                               #WELL to be clear about this positioning stuff don't
                                               #stress-out too much, as it basically put
                                               #things(functions) into spaces which are default
                                               #from Tkinter like if you are PACK ing a text and
                                               #after that again packing another text variable it
                                               #would put it in the 1st row and 1 column and then
                                               #after to the 2nd row,1 column...or maybe to the
                                               #mid of the canvas(canvas means the screen
                                               #basically)!


NOTE: pack() MUST BE USED AFTER EVERY VARIABLE ASSIGNED FUNCTION LIKE HERE WE CREATED A VARIABLE NAMED label1 WHICH USES "LABEL" FUNCTION ASSIGNED TO IT, LIKE-WISE IF YOU ADD ANY OTHER LINE OF CODE AND USED ANY VARIABLE ASSIGNED WITH THAT FUNCTION IT MUST HAVE A pack() FUNCTION AT THE END TO MAKE IT HAVE IT'S EFFECT ON THE PROGRAM!

NOTE2: HMM TO ADD pack() ANY FUNCTION YOU MUST FIRST WRITE THE VARIABLE YOU ARE USING AND AFTER THAT .pack() MUST BE GIVEN! 
LIKE I USED WITH label1.pack()


NOTE3: Well I will be posting a special blog about this pack function and there's a nice alternative to it with more flexibility! Well, that's a different story let's leave that for another day...


root.mainloop()                                                #why? (check Explanation)

Example:

1) here we created a label named username!

      !!!                   !!!            


2) Result! (see that small box with written username! That's our LABEL!)

  

3) SEE WHAT HAPPEN'S IF YOU LEAVE YOUR FUNCTION UN-PACKED!!!





4) SEE THERE'S NO NEW LABEL CREATED! AS IF YOU DON'T PACK YOUR FUNCTION THEN TKINTER WON'T ADD THAT TO YOUR PACKAGE!


 


5)LOOK AS WE PACKED OUR LABEL2 TKINTER STARTED TO ADD IT IN THE PACKAGE! (SO WE MUST PACK EVERY VARIABLE FUNCTION TO MAKE IT WORK IN TKINTER!)





Well, that's it for today! I tried my best to make you guys understand about labels through this presentation!

See you all in our next session!
Tihsrah!






1 comment: