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!






Monday, 23 December 2019

Introduction to Tkinter


yeah I know I am late but I have a pretty good excuse for now that I am having exams right now so I can't keep up with all this stuff. but anyway let's get started!

so today what basically I am going to teach you guys is how to start working in Tkinter...

Note: We will be doing all the stuff in a new file
          and anywhere I put a "#" it means I am explaining about it, as you know
          python don't read docstrings and stuff after #

Format and Explanation!

if you guys are familiar with python you must be knowing how to call a library! right?
so the first thing to set up is our working environment so we must call the library :

from tkinter import *                    #well here it means from the Tkinterlibrary
                                                      #import everything
                                                      # * = = all  

root = Tk()                                        #here we are creating a variable and 
                                                          #storingTk() as this is going to 
                                                          #be used to invoke the working of the
                                                          #module , and to get rid of

                                                          #writing Tk() again and again. you can call 
                                                          #it anything. here I am using it as root!

--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------we will write our code!------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------

root.mainloop()                           #This is our ending code! 
                                                    #Sometimes your code may work without 
                                                    #using mainloop() but most of the time
                                                    #if you don't add this to your code the canvas
                                                    #might go instantly without being shown to    
                                                    #you as come-on you asked python to 
                                                    #invoke the program not to stay like that 
                                                    #for hours it basically makes it stay there
                                                    #until you close it yourself!


Example:

here in the first picture, I have run the above code in the new shell!



After you run the code this is our desired result!

look here in the new shell a file opened as tk but its totally empty. hmmm, it's because all we have done till now is started the Tkinter canvas where we will be working-on in the coming future or in one word it's our GUI!

Well, it's okay for today, we shall be continuing this tour. So follow my blog if you want to and comment on it about anything that you didn't understand or something I said wrong as I am also a learner like you all and I make a lot of mistakes! 

Tuesday, 10 December 2019

Intro!

I did post a GUI based space fight game lately which was written in python using tkinter library.
#if you don't know about the game then click here

But when i started to learn python (which was about a year ago) #yeah i am newbie!
i didn't know a lot bout game creation or working with GUI development stuff. #still don't know a lot..😅
So if you're a beginner too! then this blog might help you learn and understand the functions and keywords used in tkinter library!
.
hmmm okay this was just the introduction to my upcoming posts!

So what i am going to do is :
i am going to post about a new keyword or a module of (tkinter ofcourse) everyday, which would be containing the meaning, the use and how to do stuff...
.
Plus there would be 1-2 examples #which you could perform yourself!

That's it for today!

😊😄HAVE A NICE DAY!😊😄