GUI Lessons (full course) with codes.

1. Intro to GUI and 

Tkinter in Python?

GUI (Graphical User Interface) helps us to interact with a program written in python. It helps us to get relief from the black and white boring console and gives us a colorful and interactive output. 

There are multiple ways to program a GUI :

1. Tkinter GUI
2. PyQT GUI
3. KIVY GUI
4. WxPython GUI
5. PySide GUI
6. PySimpleGUI
7. PyGUI
8. Pyforms GUI
9. Wax Python GUI
10. Libavg


The best python GUI known is Tkinter, It is very easy to learn and can create a beautiful program easily. In this lesson, we will be learning Tkinter in a very fun and Interactive way and you can easily create programs like Calculator, Sign in software, and much more. In this lesson, you will also get some codes of Tkinter with specified examples. You can also run these code in Visual studio code, Pycharm, Spyder, or at any Python IDLE, and also in android apps like Pyroid 3


2.Code of our first GUI

from tkinter import *
win=Tk()
win.title('sonal')
win.mainloop()


Explanation 


 import tkinter #At first we will import tkinker for using the properties of tkinter.

win=tkinter.Tk() #then we will write a variable the most popular variable used here are root, window, and win you can use any variable but here I have used win

#In the variable we write "tkinker.tk( )" because 'tk()' mostly represent the main window of an application and by writing 'win=tkinter.Tk()', win will become our screen in tkinter.

win.title('GUI') #then we write the variable name (win) and then write title.We use win here because in the 3rd line we termed it as our GUI window.

#with the help of this line we can now change the title of our window.

win.mainloop() #at last I will call the main loop of tkinter to end the program.


3. Adding labels :

from tkinter import *
win=Tk()
l=Label(text='hello')#lable is just a output / text shown on a window.

win.mainloop()

Pack

Now we have to mention that where we want to show the text / Label through pack.

from tkinter import *
win=Tk()
win.title('sonal') #title

l=Label(text='hello').pack()#lable is just an output shown on a window.
#pack will print the label in between

win.mainloop() 



Grid

Now I will tell you we can also mention that where we want to show the text / Label with the help of the grid.

Imagine the window as a spreadsheet filled with rows and columns it will look like a grid
with the help of a grid, you can make your GUI look better than using the pack

from tkinter import *
win=Tk()
win.title('sonal') #title

Label(text='hello').grid(row=0,column=0) 
# Label (It will write hello in the corner) means in column=0 and in row=0.

win.mainloop()

4. Adding syles in a 

label

Adding font style and font size


from tkinter import *
win=Tk()
win.title('sonal') #title

Label(win,text='hello' ,font=('Arial bold',50)).pack() #font=..... change the font style to bold as it has written bold. And the number which has written after font style is actually a font size here the font size is 50.

win.mainloop()


Adding font style and font size in a diffrent way

from tkinter import *
win=Tk()
win.title('sonal') 

Label(win,text='hello' ,font=('Arial bold 50')).pack() #in this just we have to put a space between font type  and font size

win.mainloop()


5. Setting window 

measurement


Setting window geometry

from tkinter import *
win=Tk()

win. geometry('450x300') #It will change the length and breadth of our window screen.

win.mainloop()


Fixing our window

from tkinter import *
win=Tk()

win. resizable(0,0) #the size became fixed and now no one can change the screen by streching.

win.mainloop()

6. Adding buttons to

 our screen


Now we are only using the most preffered way to make a GUI using python:

from tkinter import *
win=Tk()
Button(text='enter').pack()#this will add a button to our screen name'enter'.
win.mainloop()


7.Maximum and 

Minimizing size of the 

screen


from tkinter import *
win=Tk()
win.maxsize(200,200) #the maximum lenght that a screen can be strech

win.minsize(100,100)#the minimum lenght that a screen can be strech

win.mainloop()

8.Important options

 of label

LABLE OPTIONS: 

1.text - adds the text
2.bg - background
3.fg - foreground
4.padx - padding
5.pady - padding
7.relief-border,styling=SUNKEN,RAISED,GROOVE,RIDGE,FLAT,SOLID 

6.font - sets the font
a.font= ('arialbold' ,19, 'bold')
b.font= ('Arialbold 19 bold')
Code of various label options :

from tkinter import *
win=Tk()
s=Label(text='This is a exceptional story of how pluto become a draft planet...', bg="red", fg='white', padx=100,pady=200,font= ('Arialbold 19 bold'),borderwidth=10,relief=GROOVE)
s.pack()
win.mainloop()

9.Important 

Options of Pack

Pack options :


SIDE=LEFT,TOP,RIGHT,BOTTOM
ANCHOR=N,S,W,E,NW,SW,NE,SE,NS,EW,NSEW,CENTER
FILL=X,Y,NONE,BOTH 
padx, pady
Code of various pack options :
from tkinter import *
win=Tk()
s=Label(text='hello',bg='red')
s.pack(side=LEFT,fill=Y,padx=50,pady=50)
win.mainloop()

10. Combobox

from tkinter import *
from tkinter import ttk #we will import ttk from tkinter to use Combobox.
win=Tk()

v=['PYTHON','C++','JAVA','HTML'] #name any variable and then write what we want to show in our combobox.
ttk.Combobox(values=v).grid(column=0,row=0) #then we will make our Combobox using ttk in the 'values' we will write that name of the variable in which we have written what we want to show in our Combobox.

win.mainloop()

11. Adding Images

It is very fun to add images to our GUI to make our GUI look very stylish let's find out how to do so πŸ˜„...

from tkinter import *
win=Tk()

photo=PhotoImage(file='flower.png') #at first we take any variable(photo) and then write 'photoimage' class because we want to display image in the screen.

# then we write in the bracket the name of the image inside the file.

# Remember that the image name has to be PGM, PPM, GIF, and PNG format and no other format of thee will be displayed like jpg image will not be displayed but if we want to display it we must have to use 'import pillow'...

Label(image=photo ,width=500,height=500).pack()
#and then we have to tell in the label that where we want to display the along with the height and width and the pack() it or grid(column=...,row=... ) it then the image will be shown to us on the screen.

win.mainloop()


12. Function of the button

Now really we are going to do something very interesting now we will learn to make a code to print something on the screen after clicking the button and this is a very important part of learning GUI.

from tkinter import *
win=Tk()

def action(): #we will declare a function named 'action' or anything you want to keep then in the function we will declare that what to do after pressing the button.
    print('HELLO EVERYBODY') #I have written print('HELLO.....') SO AFTER PRESSING THE BUTTON IT WILL PRINT('HELLO........')

Button(text='enter',command=action).grid(row=3,column=0)#then in the button we will link the function named "action" by writting command=action (function name).

win.mainloop()



Hurray!!! now we know the basics of GUI and now we can easily make simple as well as complex programs of GUI πŸ˜„πŸ˜„πŸ˜„.

Now it's time to make some GUI projects in Python and really you will learn a lot making these projects and showing to your friends making them quite jealous πŸ˜…πŸ˜…



Making A working Calculator





from tkinter import *
win=Tk()
win.resizable(0,0)
win.title('Priyanshi Calculator')

Label(text='CALCULATOR',font=('arial bold',30)).grid(column=0,row=0)
Label(text='Enter your equation',font=20,pady=20).grid(column=0,row=1)

Equation=StringVar() #Sting var by the name itself it is showing the sting variable means the equation variable is a variable that contains sting.

Entry(textvariable=Equation,font=('Lucida',20)).grid(column=0,row=2) # In the entry we have written text variable=equation means what we enter in the entry will directly go to the Equation variable because we have written string_var class in it and we have already mentioned that in the entry, we will write 'textvariable=Equation' so that the entry will store in the Equation variable as a sting variable...

def solve(): #Then we will declare a  function and in the function, we have to write what thing we have to perform after clicking the button solve.
    
    try:
        a=eval(Equation.get())# here we want to take out the answer of the equation so will use eval function here and then we will write Equation.get because as mentioned earlier that the entry will store in the Equation variable as a string variable so we will get the Equation but via solving it through using 'eval'...

        return Label(text=a ,font=('arialbold',20) ,bg='deeppink', fg='white') .grid(column=0, row=4,   sticky=EW) #now we will return the answer after solving it

    except:
        return Label(text='ERROR' ,font=('arialbold',20) ,bg='deeppink',fg='white') .grid(column=0,row=4,sticky=EW)
 
Button(text='SOLVE',command=solve,bg='purple',fg='pink',font=(100)).grid(column=0,row=3,sticky=EW)


win.mainloop()

OUTPUT :




It is a bit difficult so if you can't able to understand it doesn't worry to study it again and again because it took me five days to understand it...


Making A Sign in software



from tkinter import *
win=Tk()

win.geometry('150x95')
win.resizable(0,0)


#creating label
Name_Label=Label(text='Name')
Name_Label.grid(column=0,row=0)

Age_Label=Label(text='Age')
Age_Label.grid(column=0,row=2,sticky=W)

Email_Label=Label(text='Email')
Email_Label.grid(column=0,row=3,sticky=W)


#entry box and a storing variable class( StringVar() ) to store all the entries.
Name_entry=StringVar()

#what we enter in the entry will directly go to the Name_entry variable because we have written string_var class and in the name itself it is written sting variable and sting_var class is capable to store the entries as a sting...
#and in the entry, we will write 'textvariable=Name_entry' so that the entry will store in the Name_entry variable as a sting variable...
Entry(width=16, textvariable=Name_entry).grid(column=1,row=0)

#like Name_entry, Age_entry will also store the entries as a sting variable by writing in the entry 'textvariable=Age_entry'
Age_entry=StringVar()
Entry(width=16,textvariable=Age_entry).grid(column=1,row=2)

#like Name_entry and Age_entry Email_entry will also store the entries as a sting variable by writing in the entry 'textvariable=Email_entry'
Email_entry=StringVar()
Entry(width=16,textvariable=Email_entry).grid(column=1,row=3)


#button (sign in)

def action():

    a=Name_entry.get()
    b=Age_entry.get()
    c=Email_entry.get()
    print(f'Username is {a}, Age is {b} and email is {c}.')
Button(text='Sign in',command=action).grid(column=1,row=4,sticky=EW)

win.mainloop()
Output

If you want to see more GUI projects Click Here


Now we have finished learning GUI in a really very interesting way. Write in the comment your experience studying this lesson and don't forget to follow my blog and I will soon add a full course of turtle graphics also.😊😊😊

Comments

Popular posts from this blog

Python Projects (GUI)

Turtle Graphics (full Course) with codes