Course Content

Course Content

Create MySQL Database Login in python with Tkinter

Hello guys. Welcome to LearnVern.

In the last topic we saw Tkinter GUI where we made a GUI based form, an application in which we saw how to make buttons, how to make text boxes, how to make Labels, etc.

Today we will see Tkinter with Database.

We shall see how Tkinter establishes connectivity & how it inserts data.

This screen here, we made one such screen last time, right?

We will create a table in our database & insert our data accordingly.

Inshort, we have to see how to apply connectivity with Tkinter.

You have to install a software which is called VS Code.

Download it from here.

There are options for Windows, . (dot) deb for linux, and for Mac as well.

We are using Windows right now so I’ll select Windows User Installation.

Click on Download & it will be downloaded.

Click on Agree here, Next. Next, Next.

Tick mark on Create a Desktop Icon.


So what is VS Code?

VS code is 1 type of Editor in which we are going to write a code.

Why am I using this?

Because in Jupyter, it will work very slowly with your database so we switched to VS Code.

VS stands for Visual Studio Code.

This software will be used in Django as well.

I'll launch it.

A screen will appear where you will get all the needed things.


Now what you have to do is…

Go to Help, Welcome and download & install the support of Python.

Whatever file you create in Python will automatically use all the syntaxes of Python.


This icon will be Grey instead of Blue as soon as you install the Python plug in.

Write the name of the folder where you have to put the file, I wrote CMD.

You only have to write Code here.

Code . (dot).

Then your code will open up with the path here.

Click on this symbol “New File”.

Add a new file & name it Tkinter . (dot) PY.

Last time we made a whole screen, right?

I’ll copy that screen here to save time.

I’ll copy my code here.

As soon as I run it, the screen we made last time will appear.

We made Roll No, First Name, Last Name, Email & a button.

Let’s begin with changing the name of the button.

Which is the insert button.

We will run it again & our button’s name is changed to insert.

Now we have to do database connectivity.

Database connectivity.

So to do the database connectivity, we know we have to use PYMySQL here.

First of all, we will import PYMySQL for our database, how we saw it in our database video.

If anybody has not seen that video yet, I urge you all to watch it first so that you will be able to find the connection with Tkinter much easier.

Our database is open in which I have created a database already.

Where I’ll make a table.

Right click & select Create Table.

I’ll make a table & name it Student.

I want the column name Roll No.

We want Integer as its datatype.

Tick mark in Primary Key & write down the length to be 10.

And tick mark on Auto Increment as well.

Then we want Fname that is our First Name.

We’ll define Varchar here. And 50 here.

Next we want Last Name. Varchar. 50.

Email. Varchar. And define 50.

We’ll save it. Yes.

Our table is created here.

Now when I run it...our table is made, right?

Now we have to establish database connectivity here.

This is our code…

First of all, I have to make a database connectivity function.

We have to work here in the database exactly like we did in PYMySQL.

I’ll select Autosave first so that we don’t need to save it over & over again.

I’ll create a function here which is CreateConn.

In which I have to return PYMySQL . (dot) connect.

I want 5 attributes here which I used last time as well in our database video.

If you have not seen that video yet, please watch the database videos once so that you can easily understand what we are going to see today.

Alright, moving on…

We need a host here. Host is our Local Host.

Then we want a database.

What had we named our database? Tkinter.

Then User. User is Root by default.

Password. Our password will remain blank.

Port number, which is 3306.

So our function has been made.

The function we created will establish a connection.

What will it do?

Connection Function.

First we have to insert.

For that, I’ll have to make another function which is InsertData.

I don’t need to pass a parameter here.

Why not? Because we are going to get our parameters from our text boxes.

First, I’ll get the values from text boxes.

We have a Get method to get values from the text boxes.

Which method? Get method.

So what are the names of our text boxes?

ERN for the Roll No.

ERN . (dot) get.

We have to get all the values.

Then we have the EF name.

I’ll write F here EFname . (dot) get.

Next we have an L name so E L Name . (dot) get.

We have email so we took E, EM . (dot) get.


Now we have to check the validations.

For that, we need a pop up message.

To pop up our message, we’ll need to import one more thing which is

Import Tkinter . (dot) message box whose short form I’ll keep M.

Now we will get database connectivity.

As soon as we get Database connectivity, try & catch is compulsory but first of all we have to check if any of my text boxes are blank…

 If any of my text boxes are blank, I’ll let a message be shown that “this specific text box is mandatory”.

How will we do it?

If, we’ll take an If condition here…

If R == blank or if F == blank or if Lname == blank or E== blank.

Suppose all of these things are blank, then we have to show a message.

For that, there is a method called M . (dot) showinfo which will show our message.

First of all you have to give a title for show info & a string.

First of all, Insert Status. I gave the title Insert Status.

Then I’ll write a message here “All Fields are Mandatory”.

So if we keep any field blank, the Insert Status message will pop up.

Now I’ll show you by calling it.

What you have to do is, you have to take an attribute, command & call the method like this.

Now I’ll run it & show it to you…

As soon as I clicked here, see? A message appeared with the title Insert Status.

Saying All Fields are Mandatory.

If I have kept a field blank, it will show us a message saying All Fields are Mandatory.

Now moving ahead…

Next is its Else part.

In Else, if any field is not empty, then we have to insert the data.

But before inserting, if there is an error in database connectivity, then our application shouldn’t crash.

So for that, we will use Try & Except.

Which is an Exception as EE.

I’ll then print it Exception, sorry Insert Exception. EE.

Next is Try & Except.

First of all I have to establish database connectivity.

Firstly, we will make a connection which is Create Connection.

Then we will make a cursor.

I had already told you all about this in our database video.

Do refer to the database video before so that you can understand the topic clearly.

Then we will call the cursor method.

What does the cursor method do?

Cursor method will help in executing the query.

Now we have to bring the arguments.

What are the arguments?

The R, F, L and E.

Let’s pass R, F, L, E.

Now we will write our query.

What is our query? Insert into table.

What is the table name? Student.

Then the field’s names.

Roll No., Fname, Lname & Email.

Then its values. We have to give Dynamic values.

Why? Because it’s coming from a user.

So we’ll use %S, %S, %S & %S.

We used %S here because we want a dynamic input from the user.

Then we have to execute a query with the help of a cursor.

How will we do it?

First of all we will pass the query, then the arguments.

Then we will save it meaning we will commit.

Commit method.

I will print Data Inserted.

Or else we will show through a message.

How will we do it?

M . (dot) show info.

Insert Status. Title.

I’ll write Data Inserted.

Then we will close the connection. Alright.

We closed the connection & our method is ready.

Let’s run it again.

I won’t enter the Roll No because it is going to be auto incremented.

I’ll write here Sunit Jha, S @ Gmail . com.

Click on Insert.

The message popped up saying All Fields are Mandatory.

Why? Because our Roll No is empty.

I’ll pass 1 here.

Then click Insert.

It gave us an error using the password database.


Let’s insert the data.

  1. I’ll write A, B & A @

Click on Insert & a message appeared saying Data Inserted.

So this is how the data is inserted in Tkinter.

And this is how you show database connectivity.

Okay? So see you all in the next video.


MySQL is an Oracle-developed relational database management system (RDBMS) based on structured query language (SQL). A database is a collection of data that is organised. It might be anything from a basic grocery list to a photo gallery or a repository for a corporate network's massive volumes of data.

  • tk. label and tk.
  • The function logintodb is created to login into the MySQL Database. The save query includes the query to be executed on the click of the submit button.
  • X and Y are the parameters given to adjust objects on the Tkinter window.

Users are generated in the database, whereas logins are established on the server. In other words, a login allows you to connect to a SQL Server service (also known as an instance), but database users, not logins, have permissions inside the database.

The below show the complete code to retrieve data from MySQL database login:

  • SELECT * FROM users WHERE user="admin";
#BBD0E0 »

Connecting to MySQL Database:

  • import MySQLdb as mdb # Connect to database server db = mdb.connect("localhost","root","") # Create cursor for database operations cursor = db.cursor()
  • Create a Database Connection Login Form in Tkinter Form Subclass for Requests and Errors Management
  • def loginForm(): global name, password name = "name" password = "password" loginButton() return loginForm()
  • def loginButton(): """ Handle button clicks by calling the appropriate functions in the following order: 1) Attempts to connect 2) If successful, request a new database connection

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere