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! 

No comments:

Post a Comment