In this video, we will discuss how to create MySQL Database Login in Python with Tkinter.
Tkinter is a Python interface to the Tk GUI toolkit. It is used for creating graphical user interfaces in Python and is part of the standard library. Tkinter provides a number of widgets that are commonly used in GUI applications, such as buttons, scrollbars, and checkboxes. It also has support for creating menus and toolbars. Tkinter can be used both with X Window System and OS X.
A Python Tkinter database is a data storage tool in the form of a Python program that uses the Tkinter GUI library. It allows you to store and retrieve data in a graphical user interface.
A Python Tkinter database is often used for storing and retrieving data on the fly. It is especially useful for programs that need to collect information from users or where there are multiple screens with different functions. The main advantage of using this kind of database is that it makes it easier to provide feedback to users and allows them to change their input at any time.
There are many types of Python Tkinter databases. Some of them are:
- SQLite: A relational database management system (RDBMS) that is implemented as a single file in memory that can be accessed via a simple SQL interface. It is the most common type of database used in Python.
- PostgreSQL: An object-relational database management system (ORDBMS) with features of both types. It supports SQL, and it can also store JSON documents and Unicode objects. It is commonly used by companies for their websites and web applications, as well as for scientific computing, data analytics, and machine learning applications.
- MySQL: MySQL is the world's most popular open source relational database management system (RDBMS) and one of the most popular Web servers.
- Microsoft SQL Server: A relational database management system (RDBMS) that is generally used for enterprise data storage and retrieval. It is often paired with other Microsoft products, such as Microsoft Excel or Microsoft Access.
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.
Install.
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.
Clear?
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.
Alright.
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.
Clear?
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.
Password.
Let’s insert the data.
I’ll write A, B & A @ gmaiil.com.
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.
See More
Share a personalized message with your friends.