0%
Loading ...

Python Tutorials

This Category contains all the tutorials related Tutorials

Python Tkinter CheckButton

In this tutorial, we will learn about Python Tkinter CheckButton. The Python Tkinter CheckButtons are used to show options to the user to choose. We can add any number of CheckButtons according to the requirement of the application. The important thing to note about checkbox is the user can choose multiple checkboxes from given options. If you want to user choose the only single option you can use the radio button. Syntax to add Python Tkinter CheckButton: w = Checkbutton ( master, option, … ) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example Program: from tkinter import * mainWindow = Tk() #setting size of main window mainWindow.geometry(“200×200”) cb1 = Checkbutton(mainWindow, text = “Python”, \ onvalue = 1, offvalue = 0, height=5, \ width = 20) cb2 = Checkbutton(mainWindow, text = “Java”, \ onvalue = 1, offvalue = 0, height=5, \ width = 20) cb1.pack() cb2.pack() mainWindow.mainloop() Various possible options in Python Tkinter CheckButton: Option Description activebackground This option helps us to set background color of widget when on cursor hover. activeforeground This option helps us to set foreground color of widget when on cursor hover. bg This option helps us to set normal background color of label and indicator. bd This option helps us to set the border size around indicator. fg This option helps us to set normal foreground colour of widget. height This option helps us to set height of widget. command This option helps us to mention a function to every time when checkbutton will change its state. cursor This option helps us to set the style of cursor like an arrow, dot etc font This option helps us to set the style of font. justify This helps us to automatically organize the text in multiple lines. padx This option helps us to set space left and right between text and checkbox. pady This option helps us to set space above and below. relief This helps us to set the style of the border by which is default Flat. width This helps us to set the width of the widget. Python Tkinter CheckButton Methods: After learning about various available options in Python Tkinter CheckButton. Its time to check out some available methods for Python Tkinter CheckButton widget. METHOD DESCRIPTION deselect() This method called to deselect the CheckButton. flash() This method is used to flash CheckButton between normal and active state. invoke() This method helps us to invoke the associated method with CheckButton. select() This method is used to select the CheckButton. toggle() This method helps us to toggle between different CheckButton. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter CheckButton Read More »

Python Tkinter Canvas

In this tutorial, we will learn about Python Tkinter Canvas. Python Tkinter Canvas allows us to add graphics in our Python application. With the help of Tkinter Canvas, we can draw lines, circles, add images and other widgets. Furthermore, we can draw graphs and plots with the help of Tkinter Canvas. Let’s start with a small example Here we will try to draw a line in =Tkinter Canvas. We will provide a total of four coordinates mean a total of four integer values like this(x1,y1) and (x2,y2). Syntax to add Python Tkinter Canvas: w = Canvas (master, options) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example to Draw a line in Tkinter Canvas from tkinter import * mainWindow = Tk() #Setting height and width of canvas canvasWindow = Canvas(mainWindow, width=200, height=200) canvasWindow.pack() #Getting mid of canvas vertically screenMid = int(200 / 2) # creating line in cavas using create_line method canvasWindow.create_line(0, screenMid, 200, screenMid, fill=”#333″) mainloop() Here you can see how we can create a simple line using create_line method in Tkinter Canvas. Here we draw a horizontal line. I will recommend you to draw a vertical line by yourself. It will help you to understand this create_line method. Let’s move to the next example. In the next example, we will learn how we can create a rectangle in our Tkinter Canvas. Example to Draw a rectangle in Python Tkinter Canvas from tkinter import * mainWindow = Tk() #Setting height and width of canvas canvasWindow = Canvas(mainWindow, width=300, height=200) canvasWindow.pack() # creating line in cavas using create_line method canvasWindow.create_rectangle(50, 50, 250, 150, fill=”red”) mainloop() Various Possible Options in Python Tkinter Canvas Option Description bd This option we can use to set border width of the canvas in pixels. By default width is 2px. bg This option we can use to set the background colour of the canvas. confine This option we can use to make canvas un scrollable. height This option we can use to set the height of the canvas. relief This option we can use to set border type. The possible types are SUNKEN, RAISED, GROOVE, and RIDGE. width This option we can use to set width of canvas. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter Canvas Read More »

Python Tkinter Button

In this tutorial, we will learn about Python Tkinter Button. This Python Tkinter widget is used to add different kinds of buttons in our Python application. You can set configurations of the button according to the requirements of your application. Furthermore, we can set functionality to our button that what will happen when this button will get pressed. In this tutorial, we will see different Tkinter button options that how we can customize our Button. Syntax to add Python Tkinter Button: w = Button (master, options) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example program of Tkinter button: Without wasting time I would like to start with this example program that how we can add Python Tkinter Button in our application. from tkinter import * #setting up main window mainWindow = Tk() #setting size of main window mainWindow.geometry(“200×100”) #Creating new button myButton = Button(mainWindow,text = “Submit”) myButton.pack() mainWindow.mainloop() Example program of Button click action in Python: In this example program, we will learn how we can perform some action when our button will get clicked. from tkinter import * from tkinter import messagebox #setting up main window mainWindow = Tk() #setting size of main window mainWindow.geometry(“200×100”) # defining a function def show(): messagebox.showinfo(“Info”, “Hello, You Clicked this Button”) #Creating new button myButton = Button(mainWindow,command = show, text = “Submit”) myButton.pack() mainWindow.mainloop() Various possible options in Tkinter button: Option Description activebackground This option helps us to set background color of button on mouse hover. activeforeground This option helps us to set font color of button on mouse hover. Bd This option helps us to set border width in pixel. Bg This option helps us to set the background colour button. Fg This option helps us to set foreground color button. Font This option helps us to set font of button. Height This option helps us to set height of button. Highlightcolor This option helps us to set Highlight color when button will get focus. Image This option helps us to set image displayed on button. Padx This option helps us to set horizontal padding in button. Pady This option helps us to set vertical padding in button. Relief This option helps us to set type of border on button like SUNKEN, RAISED, GROOVE, and RIDGE.. State This option helps us to set button disable. Underline This option helps us to set button text underline. Width This option helps us to set width of button Tkinter Button Methods: After learning about various available options in Python Tkinter Button. Its time to check out some available methods for Tkinter Button widget. METHOD DESCRIPTION flash() This method helps us to flash widget many times between active and normal colours. invoke() This method helps us to invoke the associated method with Python Tkinter Button. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter Button Read More »

Tkinter in Python

In this tutorial, we will learn Tkinter in Python. Tkinter is a library which allows us to create Graphical User Interface. We can create desktop-based applications with the help of Tkinter. In this Tkinter tutorial series, we will learn various widgets in Tkinter along with Example programs. Steps to use Tkinter in your Application: Import Tkinter in your program Create the main application window. Add the widgets such as button, label in your main application window Add actions to different widgets in your applications Example Program of Tkinter in Python: #importing tkinter lib from tkinter import * #creating the main window. mainWindow = Tk() #event main loop mainWindow.mainloop() Widgets in Tkinter: Widget Description Button This widget is used to add button in Python application. Canvas This widget is used to make a canvas in Python application. Check Button This widget is used to show options with checkboxes in python application. Entry This widget is used to get a single line entry from the user. Frame This widget is used for grouping of other Tkinter Widgets. Label This widget is used to show caption with other widgets. LabelFrame Tkinter LabelFrame acts as a container and used to create a border around child widgets. ListBox This widget helps up to show list of items in application. MenuButton This widget used to show the drop-down menu. Menu This widget helps us to add a menu in the application. Message Python Tkinter Message works same as Lable expect it can automatically wrap up text. MessageBox Tkinter MessageBox widget is used to show the message box in the application. PanedWindow PanedWindow is a widget which is used as a container. A PanedWindow can contain one or more child Panes. RadioButton This widget is used to show multiple options to the user. Scale Tkinter Scale widget is used to the graphical slider. ScrollBar ScrollBar widget is used to make the content of other widgets scroll-able. Text The Tkinter Text widget is used to show multi-line text in the application TopLevel TopLevel is used to show top-level screen and which directly managed by the window manager. Spinbox Spinbox is an entry widget which is used to ask the user to choose one from fixed values. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Tkinter in Python Read More »

Exception Handling in Python

In this tutorial, we will learn about Exception Handling in Python. Before going into the depth we have a very important question, “What is Exception?”. And the answer to this question is very simple. In the normal way we know exception means the abnormal situation. But in python, Exception is an event which occurs while execution of the program and it breaks the normal flow of the program. This thing happens because sometimes our Python script stuck in a situation that it cannot cope with, and it just throws an exception. The exception is a Python object which use to show an error. What is Exception Handling in Python? Exception Handling in Python means to protract your Python code from these Exceptions. By handling these exceptions you can save code from falling in these exceptions. How we can do that? Suppose you have written code and you think there is some code which may throw an exception while execution. So in this situation, you can place your suspicious code in try: block. After that, we will add except block. In this block, we will write code which will run in case of exception occur and this code will help us to handle this abnormal situation. List of Standard Exceptions in Python? Here is a list of some common Standard Exceptions in Python which you will normally see while working in Python. Exception Description Exception This is base class of all other Exception classes. SystemExit This Exception is occur by sys.exit(). StopIteration when next() method of iteration doesn’t point to any object. ArithmeticError ArithmeticError in base class of all the numeric calculation errors OverflowError This exception occurs when normal calculation exceeds maximum limit of numeric type. FloatingPointError This exception occurs There is some error in calculation of floating numbers. ZeroDivisionError This exception occurs when we try to divide a numeric by zero. ImportError This exception occurs import statement get fail. IndexError This exception occurs when there is any problem while finding index of a squence. NameError This exception occurs when there is any problem finding a identifier. IOError This exception occurs when there is any I/O operation get fail. SyntaxError This exception occurs when use wrong Python Syntax. NotImplementedError This exception occurs when we forget to implement a abstract method. NotImplementedError This exception occurs when we forget to implement a abstract method. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Exception Handling in Python Read More »

Directory in Python

In this tutorial, we would learn about the Directory in Python. In Directories, we can store files and subdirectories. Directories help us to manage a large amount of files of our project. There is an os module in Python which provides us with methods to work with Directories. Get Current Directory: Here will we will learn how we can get the current working directory. There is .getcwd() method in the os module which helps us to get the current working directory. Follow this example to understand these methods. import os print(os.getcwd()) Changing Directory: We can change our current working directory using chdir() method. We have to mention the new path in this method. we can separate path elements using backslash or forward slash. import os print(os.getcwd()) os.chdir(‘C:\\Python’) print(os.getcwd()) Check complete list of Directories and files: In Python os module provide us method called listdir(). which return list of all the available subdirectories and files in the current directory. import os print(os.getcwd()) os.listdir() Create a Directory: In Python os module provide us method called mkdir() which helps us to create new directory. In this method, we provide full path where we want to create a directory in case we don’t provide full path it will create Directory in the current directory. import os os.mkdir(‘test’) os.listdir(); Renaming a Directory: We can rename a directory using rename() method. This rename method takes two arguments first one is the old name of the directory and the second one in the new name of the directory. Check this example below. import os os.rename(‘test’,’My_Directory’) os.listdir(); Removing a Directory: We can remove the existing file using method call remove() and can remove a directory using rmdir() method. These methods take only one parameter which is the directory/filename which you want to delete. Check this example. import os os.rmdir(‘My_Directory’) os.listdir();   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Directory in Python Read More »

Functions in Python

In this tutorial, we would learn about Functions in Python. A function is a block of code which allow us to reuse code. Python provides us rich collection of in-built functions like print(), mkdir(), rmdir() etc. which we can use by just calling them. In Python, we can create our own functions. This kind of functions known as user-defined functions. Here we will learn How to create a function, how to call it. return statement etc. Defining a function in Python Defining a function in Python is so easy. we use def keyword in Python to define a function. Check this example to understand it. def my_function(): print (“Hello This is my_function”) return Here you can see we used def keyword to define a function then we wrote function name followed by (). After we wrote: symbol to state body of the function. At the end we wrote a return statement which is optional we will write a return statement without parameter this is equal to return none. Calling a function in Python After defining a function we can call our function any number of time in our code. Check this example. def my_function(): print (“Hello This is my_function”) return #calling our function Here my_function()   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Functions in Python Read More »

Looping Statements in Python

In this tutorial, we would learn about Looping Statements in Python. Looping statements help us to run a block of code for several times. For example, you want to show “Hello World” a hundred times. It means you have to write print(“Hello World”) statement a hundred times. This will be soo boring and time-consuming. But with the help of Loops, we can do this in a few moments and we have to write print(“Hello World”) only once rest of 99 loop will automatically run this line. In Python, there are three types of loops. We can use a looping statement according to our program requirement. We will learn about each looping statement in detail. Available three looping statements in Python are as follow: for loop while loop nested loop For Loop: For loop is used to iterate sequences such as lists, tuple, string and other iterable objects. By default, the loop will only stop from iterating if give sequence will be exhausted. Syntax of for loop: for val in sequence: Body statements The val in syntax is just a variable (You can write here any other variable name) which will be replaced with the value which for loop will take from the sequence after every iteration. Example program: languages = [‘Java’,’Python’,’Kotlin’] #List for fav in languages: print ‘Favourite:’, fav Output: Favourite : Java Favourite : Python Favourite : Kotlin For Loop with if-else: We can also use if-else and other Conditional statements in loop. Here I will show to check all the even numbers from between 1 to 25. Numbers = list(range(0,25)) for num in Numbers: if(num%2==0): print(num) As you can see in the above example I have used range function to make a list of number from 0 to 25. While Loop: While loop iterates the body of loop if the given condition will be true. we can give condition to while loop in form of expression or any non zero value. While loop will stop iterating if the condition becomes false. Syntax of while loop: while (condition): body of while loop Example program: Here we will write code to show number from 1 to 10 using while loop. num = 1 while(num<=10): print(num) num+=1 Nested Loop: When we use a loop inside another loop it is known as Nested Loop. Check out Syntax examples: for val in sequence: for val in sequence: body statements body statements Another example with while loop: while condition: while condition: statement statement   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Looping Statements in Python Read More »

Conditional Statements in Python

In this tutorial, we would learn about conditional statements in Python. We use conditional statements to control the flow of execution of our program. That’s why conditional statements are also known as control flow. Here I would like to explain conditional statements with an example. “Suppose you have a list with 10 integer elements and you want to separate all the elements with the value under 50 in another list in this kind of situation you can use a conditional statement”. We have several conditional statements in Python like if statement, if-else statement, elif statement. We will about all these statements one by one. If statement in Python: We use if statement in case we want to run a specific piece of code only if the certain condition would true. To add if statement in our code we have if keyword then we add a condition in parenthesis and then the body of if in curly brackets {}. Here is the syntax of if statement in Python. if( # condition Here ): # body of if statement For example, a person can only code if his/her age is above 18 years. Check this example. age=19 if(age>=18): print(‘you are eligible for vote’) If-else statement in Python: In the last example, we learned about if statement and we learned that if statement only works if a certain condition will true. It means, if statement will not have a control in case condition will false. But in the case of the if-else statement, we can run a piece of code in case condition would true and can run a different piece of code in case condition would false. Here is the syntax of the if-else statement. if( # condition Here ): # piece of code (in case condition would true) else: # piece of code (in case condition would false) Here to understand the if-else statement in Python we would see a simple example to find if a given number is even or odd. number=10 if(number%2==0): print(‘number id even’) else: print(‘number id odd’) elif statement in Python: We use elif(short form of else if) statement. In case we have multiple conditions. for example you want to assign Grades to students according to their marks. It means we have multiple conditions like students who got marks between 100 to 80 will in A Grade and those who got 80 to 60 will in B Grade like this. Let’s follow this example in Python code. marks=85 if(marks<=100 and marks>80): print(‘Grade A’) elif(marks<=80 and marks>60): print(‘Grade B’) elif(marks<=60 and marks>40): print(‘Grade C’) else: print(‘Fail’)   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Conditional Statements in Python Read More »

Comments in Python

In this tutorial, we will learn about Comments in Python. Comments are not used to perform any function in the program but they really useful for developers. Developers can write comments with variables and functions to remember their working (purpose) in the program. One important thing to mention comments doesn’t put any impact on the execution of program because interpreter simply ignores them. There are two kinds of comments in Python: Single-Line Comments Multi-Line Comments Single-Line Comments: Single line comments are used to add one line comments with variable and functions. We id hash sign(#) to create a single-line comment in Python. Please check this example to understand it more wisely. Example Program: #This is sigle line comment print(“Welcome to Owlbuddy”) Multi-Line Comments: The multi-line comments are used to write comments of more then one line in program. we have two options to add multi line comments in Python. First one is using hash sign(#) second one is triple quotes(”’). Multi-Line Comments using # #This is sigle line comment #using hash sign print(“Welcome to Owlbuddy”) Multi-Line Comments using ”’ ”’This is sigle line comment #using hash sign”’ print(“Welcome to Owlbuddy”) Here Triple Quote Comments are working because Python simply ignores all the string literal which are not assigned to any variable. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Comments in Python Read More »

Scroll to Top
×